Cod sursa(job #855285)

Utilizator stoicatheoFlirk Navok stoicatheo Data 14 ianuarie 2013 20:40:13
Problema Multiplu Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1 kb
#include <fstream>
using namespace std;
ifstream f("multiplu.in");
ofstream g("multiplu.out");
int a, b, c[2000010], t[2000010], i, j, p, u, cif[2000010], ok, m, viz[2000010];
 
int cmmdc(int x, int y){
    int r;
    while(y!=0)
    {
        r=x%y;
        x=y;
        y=r;
    }
    return x;
}
 
void rec(int x){
    if(x!=0)
    {
        rec(t[x]);
        g<<cif[x];
    }
}
 
int main(){
    f>>a>>b;
    f.close();
    j=cmmdc(a, b);
    m=(a/j)*b;
    p=u=1;
    c[p]=1;viz[1] = 1;
    cif[1]=1;
    while(p<=u)
    {
        for (i=0;i<=1;i++)
            if (viz[(c[p]*10 + i)%m] == 0){
                c[++u]=(c[p]*10 + i)%m;
                cif[u]=i;
                t[u]=p;
                viz[c[u]] = 1;
                if(c[u]==0)
                {
                    ok=1;
                    break;
                }
            }
        p++;
        if(ok==1)
            break;
    }
    rec(t[u]);
    g<<cif[u]<<"\n";
    g.close();
    return 0;
}