Cod sursa(job #123942)

Utilizator bogdanhm999Casu-Pop Bogdan bogdanhm999 Data 17 ianuarie 2008 19:33:11
Problema Multiplu Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.28 kb
//111 101 000 010 010 000 000
// 18 446 744 073 709 551 616

#include <stdio.h>
long rest[2000003];
long A,B,cmmmc,i,p,q,m,x;
bool mark[2000003],cif[2000003];
long up[2000003];

void write(int x){
     if (up[x]!=-1)
        write (up[x]);
     printf("%ld",cif[x]);
}

int main(){
    freopen("multiplu.in","r",stdin);
    freopen("multiplu.out","w",stdout);
    
    scanf("%ld %ld",&A,&B);
    
    cmmmc=A*B;
    while (A!=B)
          if (A>B){
             A=A%B;
             if (A==0)A==B;
          }
          else{
               B=B%A;
               if (B==0)B=A;
          }
    cmmmc/=A;
    if (cmmmc==0)return 0;
    
    rest[1]=1;up[1]=-1;
    mark[1]=1;cif[1]=1;
    p=1;q=1;
    while (!mark[0]&&q<=2000000){
          x=(rest[p]*10)%cmmmc;
          if (!mark[x]){
             q++;rest[q]=x;
             mark[x]=1;
             up[q]=p;
             cif[q]=0;
             if (!x)break;
          }
          x=(rest[p]*10+1)%cmmmc;
          if (!mark[x]){
             q++;rest[q]=x;
             mark[x]=1;
             up[q]=p;
             cif[q]=1;
             if (!x)break;
          }
          p++;
          //printf("%ld\n",p);
    }
    
    
    write (q);
    printf("\n");
    
return 0;
}