Pagini recente » Cod sursa (job #573259) | Cod sursa (job #415110) | Cod sursa (job #1394558) | Cod sursa (job #2051828) | Cod sursa (job #2967533)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
int cmmdc(int a, int b)
{
while(b > 0)
{
int r = a % b;
a = b;
b = r;
}
return a;
}
const int VMAX = 2000001;
bool vf[VMAX];
queue <int> q;
int parinte[VMAX], cif[VMAX];
int m;
void bfs()
{
q.push(1);
parinte[1]= -1;
cif[1] = 1;
vf[1] = true;
while(!q.empty())
{
int x = q.front();
q.pop();
int f1 = (x * 10 + 1) % m;
int f0 = (x * 10) % m;
if(!vf[f0])
{
vf[f0]= true;
q.push(f0);
parinte[f0] = x;
cif[f0] = 0;
if(f0 == 0)
return;
}
if(!vf[f1])
{
vf[f1] = true;
q.push(f1);
parinte[f1] = x;
cif[f1] = 1;
if(f1 == 0)
return;
}
}
}
int main()
{
ifstream fin("multiplu.in");
ofstream fout("multiplu.out");
int a, b;
fin >> a >> b;
m = a * b / cmmdc(a, b);
bfs();
vector <int> rasp;
int p = parinte[0];
rasp.push_back(cif[0]);
while(p != -1)
{
rasp.push_back(cif[p]);
p = parinte[p];
}
for(int i = rasp.size() - 1; i >= 0; i--)
fout << rasp[i];
return 0;
}