Cod sursa(job #1414850)

Utilizator AndreosAndrei Otetea Andreos Data 3 aprilie 2015 09:43:55
Problema Multiplu Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.41 kb
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
struct MULTIPLU
{
    bool c;
    int r,t;
};
MULTIPLU q[2000001];
bool f[2000001];
bool sol[2000001];
int cmmdc(int x,int y)
{
    if(y==0)
        return x;
    return cmmdc(y,x%y);
}
int cmmmc(int a,int b)
{
    return a*b/cmmdc(a,b);
}
int main()
{
    freopen("multiplu.in","r",stdin);
    freopen("multiplu.out","w",stdout);
    int a,b,k,p,u,cont=1,i,caut;
    bool ok=0;
    MULTIPLU temp;
    scanf("%d%d",&a,&b);
    k=cmmmc(a,b);
    temp.c=1;
    temp.r=1;
    temp.t=0;
    p=u=1;
    q[1]=temp;
    while(ok==0)
    {
        temp.c=0;
        temp.r=(q[p].r*10+0)%k;
        temp.t=p;
        if(f[temp.r]==0)
        {
            q[++u]=temp;
            if(temp.r==0)
            {
                ok=1;
                break;
            }
            f[temp.r]=1;
        }
        temp.c=1;
        temp.r=(q[p].r*10+1)%k;
        temp.t=p;
        if(f[temp.r]==0)
        {
            q[++u]=temp;
            if(temp.r==0)
            {
                ok=1;
                break;
            }
            f[temp.r]=1;
        }
        p++;
    }
    caut=u;
    sol[1]=q[caut].c;
    while(q[caut].t!=0)
    {
        caut=q[caut].t;
        sol[++cont]=q[caut].c;
    }
    for(i=cont;i>=1;i--)
        printf("%d",sol[i]);
    printf("\n");
    return 0;
}