Pagini recente » Cod sursa (job #2650100) | Cod sursa (job #1192211) | Cod sursa (job #2342180) | Cod sursa (job #1939978) | Cod sursa (job #2711677)
#include <fstream>
using namespace std;
const int NMAX=2000005;
struct MULTIPLU
{
bool c;
int r,pred;
};
MULTIPLU q[NMAX];
bool viz[NMAX],sol[NMAX];
int main()
{
ifstream fin("multiplu.in");
ofstream fout("multiplu.out");
int a,b,c,a1,b1,m,p,u;
fin>>a>>b;
a1=a;
b1=b;
if(a==1&&b==1)
{
fout<<"1"<<'\n';
}
else
{
while(b!=0)
{
c=a%b;
a=b;
b=c;
}
m=(a1*b1)/a;
MULTIPLU temp1,temp2;
p=1;
u=0;
temp1.c=1;
temp1.r=1;
temp1.pred=0;
viz[1]=1;
q[++u]=temp1;
int ok=0;
while(p<=u&&ok==0)
{
temp1=q[p];
temp2.c=0;
temp2.r=(temp1.r*10+0)%m;
temp2.pred=p;
if(viz[temp2.r]==0)
{
viz[temp2.r]=1;
q[++u]=temp2;
}
if(temp2.r==0)
{
ok=1;
break;
}
temp2.c=1;
temp2.r=(temp1.r*10+1)%m;
temp2.pred=p;
if(viz[temp2.r]==0)
{
viz[temp2.r]=1;
q[++u]=temp2;
}
if(temp2.r==0)
{
ok=1;
break;
}
p++;
}
int poz=u,poz1=0;
while(poz!=0)
{
sol[poz1]=q[poz].c;
poz1++;
poz=q[poz].pred;
}
for(int i=poz1-1; i>=0; i--)
{
fout<<sol[i];
}
fout<<'\n';
}
return 0;
}