Pagini recente » Cod sursa (job #1153156) | Cod sursa (job #1637229) | Cod sursa (job #1251829) | Cod sursa (job #2028634) | Cod sursa (job #2714334)
#include<fstream>
#include<queue>
#include <iostream>
using namespace std;
const int N = 2e6 + 1;
int pred[N], ultim[N];
ifstream in("multiplu.in");
ofstream out("multiplu.out");
void numar(int x)
{
if (x==-1) return;
numar(pred[x]);
out<<ultim[x];
}
int cmmdc(int x, int y)
{
while (y != 0)
{
int r = x % y;
x = y;
y = r;
}
return x;
}
int cmmmc(int x, int y)
{
return x / cmmdc(x, y) * y;
}
void bfs(int m)
{
queue<int> q;
q.push(1);
pred[1]=-1;
ultim[1]=1;
while(!q.empty())
{
int x=q.front();
q.pop();
for(int i=0; i<2; i++)
{
int y=(x*10+i)%m;
if (pred[y] == 0)
{
ultim[y]=i;
pred[y]=x;
if(y==0) return;
q.push(y);
}
}
}
}
int main()
{
int a,b,m;
in>>a>>b;
in.close();
m = cmmmc(a, b);
bfs(m);
numar(0);
out.close();
return 0;
}