Pagini recente » Cod sursa (job #2310058) | Cod sursa (job #878363) | Cod sursa (job #2551005) | Cod sursa (job #2176100) | Cod sursa (job #2711696)
#include <fstream>
using namespace std;
ifstream cin("multiplu.in");
ofstream cout("multiplu.out");
const int NMAX=2000001;
struct MULTIPLU
{
bool c;
int r,pred;
};
MULTIPLU q[NMAX];
bool viz[NMAX],sol[NMAX];
int cmmmc(int a,int b)
{
int ca = a, cb = b;
int r;
while(b!=0)
{
r=a%b;
a=b;
b=r;
}
return ca * cb / a;
}
int main()
{
int A,B,M,p,u,ok,i,n;
n=0;
cin>>A>>B;
if(A==1&&B==1)
{
cout<<1;
return 0;
}
M=cmmmc(A,B);
MULTIPLU temp1,temp2;
p=1; u=0;
temp1.c=1; temp1.r=1; temp1.pred=0;
viz[1]=1;
q[++u]=temp1;
ok=0;
while(p<=u&&ok==0)
{
temp1=q[p];
temp2.c=0;
temp2.r=(temp1.r*10)%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++;
}
while(u!=0)
{
sol[++n]=q[u].c;
u=q[u].pred;
}
for(i=n;i>=1;i--)
cout<<sol[i];
return 0;
}