Cod sursa(job #2285153)

Utilizator AlexandraCristeaAlexandraCristea AlexandraCristea Data 18 noiembrie 2018 11:15:43
Problema Multiplu Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.31 kb
#include <fstream>

using namespace std;
ifstream in("multiplu.in");
ofstream out("multiplu.out");
const int NMAX=2000000;
struct MULTIPLU
{
    bool c;
    int r,t;
};
int cmmmc (int A,int B)
{
    int r,ca,cb;
    ca=A;
    cb=B;
    while(B)
    {
        r=A%B;
        A=B;
        B=r;
    }
    return (ca*cb)/A;
}
MULTIPLU q[NMAX+5];
bool f[NMAX+5],y[NMAX+5];
int main()
{
    int A,B,M,p,u,l=0,ok,r,i;
    in>>A>>B;
    M=cmmmc(A,B);
    MULTIPLU temp;
    temp.c=1;
    temp.r=1;
    temp.t=0;
    p=u=1;
    q[u]=temp;
    ok=0;
    while(p<=u && !ok)
    {
        r=(q[p].r*10+0)%M;
        if(f[r]==0)
        {
            f[r]=1;
            temp.c=0;
            temp.r=r;
            temp.t=p;
            q[++u]=temp;
            if(r==0)
            {
                ok=1;
                continue;
            }
        }
        r=(q[p].r*10+1)%M;
        if(f[r]==0)
        {
            f[r]=1;
            temp.c=1;
            temp.r=r;
            temp.t=p;
            q[++u]=temp;
            if(r==0)
            {
                ok=1;
                continue;
            }
        }
        p++;
    }

    while(u!=0)
    {
        y[++l]=q[u].c;
        u=q[u].t;
    }
    for(i=l;i>=1;i--)
        out<<y[i];
    return 0;
}