Pagini recente » Cod sursa (job #3361163) | Cod sursa (job #3361171) | Cod sursa (job #3361149) | Cod sursa (job #3361150) | Cod sursa (job #3361148)
#include <iostream>
#include <fstream>
#include <algorithm>
#include <queue>
#include <unordered_map>
#define NMAX 2'000'001
using namespace std;
ifstream fin("multiplu.in");
ofstream fout("multiplu.out");
int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
bool viz[NMAX];//viz[i]=> am calculat deja un nr care % cmmmc este i
int ant[NMAX];//ant[i]=> de unde ne-am luat raspunsul
bool digit[NMAX];//digit[i]=> daca pe pozitia i avem i sau 0
void solve(int a,int b) {
int cmmc = a/ gcd(a, b) *b;;
queue<long long>q;
long long rest=1;
q.push(rest);
viz[rest] = true;
digit[rest] = 1;
ant[rest] = -1;
while (true)
{
rest = q.front();
q.pop();
if (rest == 0) {
return ;
}
else {
for (int i = 0; i <= 1; ++i) {
int nxtRest = (rest * 10 + i)%cmmc;
if (!viz[nxtRest]) {
viz[nxtRest] = 1;
q.push(nxtRest);
ant[nxtRest] = rest;
digit[nxtRest] = i;
}
}
}
}
}
int main()
{
int a, b;
fin >> a >> b;
solve(a, b);
//reconstruim raspunsul
string rez;
int restCrt = 0;
while (restCrt !=-1)
{
rez.push_back(digit[restCrt]+'0');
restCrt = ant[restCrt];
}
reverse(rez.begin(), rez.end());
fout << rez;
return 0;
}