Cod sursa(job #645345)

Utilizator voicuraduVoicu Radu voicuradu Data 9 decembrie 2011 12:36:43
Problema Multiplu Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.44 kb
#include<cstdio>
#include<vector>
#include<queue>
using namespace std;
int a,b,x,y,m,pr[2000010],nr[2000010];
queue<int> q;
void read()
{
    int i;
    freopen("multiplu.in","r",stdin);
    freopen("multiplu.out","w",stdout);
    scanf("%d%d",&a,&b);
    for(i=1;i<=2000010;i++)
        nr[i]=-1;
}

int cmmdc(int a,int b)
{
    int r;
    do
    {
        r=a%b;
        a=b;
        b=r;
    }while(b);

    return a;
}

int cmmmc(int a, int b)
{
    return a*b/cmmdc(a,b);
}

void reconstr(int y)
{

    if(y==1)
    {
        printf("%d",nr[y]);
        return;
    }
    reconstr(pr[y]);
    printf("%d",nr[y]);
}

void bfs()
{
    nr[0]=-1;
    if(m==1)
    {
        printf("1\n");
        return;
    }
    nr[1] = 1;
    q.push(1);
    while(!q.empty())
    {
        x=q.front();
        q.pop();
        y=(x*10)%m;
        if(nr[y]==-1)
            {
                q.push(y);
                nr[y]=0;
                pr[y]=x;
            }
        if(y==0)
            break;
        ////////////////////
        y=(x*10+1)%m;
        if(nr[y]==-1)
            {
                q.push(y);
                nr[y]=1;
                pr[y]=x;
            }
        else
            continue;
        if(y==0)
            break;
    }
    reconstr(0);
}

void rez()
{
    m=cmmmc(a,b);
    bfs();
    printf("\n");
}

int main()
{
    read();
    rez();
    return 0;
}