Cod sursa(job #1097299)

Utilizator ForrestGumpMihai S ForrestGump Data 3 februarie 2014 12:00:26
Problema Multiplu Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.18 kb
#include <cstdio>


using namespace std;

struct QUEUE
{
  bool c;
  int r, pred;
};

QUEUE q[2000005];

bool viz[2000001];

bool numar[2000001];

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



void reconstituire_sol(int u)
{
    int k=0;

    while(u)
    {
    numar[++k]=q[u].c;
    u=q[u].pred;
    }
    for(int i=k ; i>=1; i--)
    printf("%d", numar[i]);
}

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

    int A, B, M, i, u, p, r;

    QUEUE temp;

    scanf("%d %d", &A, &B);

M=(A*B)/cmmdc(A,B); //pas 1

    p=u=1;

    q[p].c=1;
    q[p].r=1;
    q[p].pred=0;

    while(p<=u)
    {
        
    for(i=0; i<=1; i++)
    {
        r=(q[p].r*10+i)%M;
        if(viz[r]==0)
        {
            temp.c=i;
            temp.r=r;
            temp.pred=p;
            viz[r]=1;
            q[++u]=temp;
        }


        if(r==0)
         {  reconstituire_sol(u);
            return 0;
         }
        }//for
        p++;
    }//ies cand primul rest = 0 (break)



    return 0;
}