Cod sursa(job #899328)

Utilizator mads2194FMI - Andrei Stroe mads2194 Data 28 februarie 2013 13:58:46
Problema Multiplu Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.24 kb
#include<cstdio>
#include<vector>
#include<queue>

using namespace std;

#define N 2000002

//int sol;
int a,b;
int P[N];
bool V[N];

//int d[N],F[N],P[N];

queue <int> q;
vector <int> sol;

int cmmdc(int x,int y)
{
    if(y) return cmmdc(y,x%y);
    return x;
}

int cmmmc(int x,int y)
{
    return x*y/cmmdc(x,y);
}

void run()
{
    int m,x,y;
    m=cmmmc(a,b);

    q.push(1);
    V[1]=1;
    P[1]=-1;
    while(!q.empty())
        {
            if(P[0]) break;
            x=q.front();
            q.pop();

            y=(x*10)%m;
            if(P[y]==0)
                {
                    V[y]=0;
                    P[y]=x;
                    q.push(y);
                }

            y=(x*10+1)%m;
            if(P[y]==0)
                {
                    V[y]=1;
                    P[y]=x;
                    q.push(y);
                }
        }
    int pos=0;
    while(P[pos]!=-1)
    {
        sol.push_back(V[pos]);
        pos=P[pos];
    }
    sol.push_back(1);
    for(size_t i=sol.size();i;--i)
        printf("%d",sol[i-1]);
}

int main()
{
	freopen("multiplu.in","r",stdin);
	freopen("multiplu.out","w",stdout);

	scanf("%d %d",&a,&b);

	run();

	return 0;
}