Pagini recente » Cod sursa (job #1469041) | Cod sursa (job #2659716) | Cod sursa (job #3245775) | Cod sursa (job #646486) | Cod sursa (job #645343)
Cod sursa(job #645343)
#include <fstream>
#include <queue>
using namespace std;
ifstream in("multiplu.in");
ofstream out ("multiplu.out");
int a,b,m,nr[2000001],pred[2000001],sol;
queue <int> q;
int cmmdc (int a,int b){
for(int c; b; c= a%b,a=b,b=c);
return a;
}
int cmmmc (int a,int b) {
return a * b / cmmdc (a,b);
}
void citire () {
in >> a >> b;
m = cmmmc (a,b);
for (int i = 0 ; i < m; ++i){
nr[i] =-1;
}
}
void reconstruct(int y){
if (y == 1){
out << nr[y];
return;
}
reconstruct(pred[y]);
out << nr[y];
}
void bfs(){
if (m == 1){
out << "1\n";
return;
}
int x,y;
q.push(1);
nr[1] =1;
pred[1] = 0;
while (!q.empty()){
x = q.front();
q.pop();
y = (x * 10) % m;
if (nr[y] == -1){
q.push(y);
nr[y] = 0;
pred[y] = x;
}
if (y == 0){
break;
}
y = (x*10+1)%m;
if (nr[y] == -1){
q.push(y);
nr[y] = 1;
pred[y] = x;
}
if (y == 0) {
break;
}
}
reconstruct(y);
}
int main () {
citire();
bfs();
return 0;
}