Pagini recente » Cod sursa (job #117710) | Cod sursa (job #2605181) | Cod sursa (job #1879334) | Cod sursa (job #1333204) | Cod sursa (job #2714329)
#include<fstream>
#include<queue>
#include <iostream>
using namespace std;
int pred[101], ultim[101];
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;
}