Pagini recente » Cod sursa (job #3290386) | ONIS 2014, Clasament | Cod sursa (job #631737) | Cod sursa (job #721180) | Cod sursa (job #798436)
Cod sursa(job #798436)
#include<fstream>
#include<queue>
using namespace std;
ifstream in ("multiplu.in");
ofstream out("multiplu.out");
queue <int > coada;
int a , b, nr;
const int N = 2000002;
int u[N],pred[N],viz[N];
int cmmc(int a,int b)
{
int c=0;
int prod=a*b;
while (b) {
c = a % b;
a = b;
b = c;
}
return prod/a;
}
void recupereaza()
{
int nrCurent=0;
int put=1;
int poz=0;
while(true)
{
nrCurent=nrCurent+u[poz]*put;
if(pred[poz]==0)
break;
poz=pred[poz];
put=put*10;
}
out<<nrCurent;
}
void solve()
{
u[1]=1;
coada.push(1);
while(!coada.empty())
{
int curentRest=coada.front();
if(curentRest==0)
{
recupereaza();
return;
}
int newRest;
coada.pop();
//adaugam un 1
newRest=(curentRest*10+1)%nr;
if(viz[newRest]==0)
{
viz[newRest]=1;
u[newRest]=1;
pred[newRest]=curentRest;
coada.push(newRest);
}
newRest=(curentRest*10)%nr;
if(viz[newRest]==0)
{
viz[newRest]=1;
u[newRest]=0;
pred[newRest]=curentRest;
coada.push(newRest);
}
}
}
int main()
{
in>>a>>b;
nr=cmmc(a,b);
solve();
return 0;
}