Cod sursa(job #926609)

Utilizator Vladinho97Iordan Vlad Vladinho97 Data 25 martie 2013 11:59:45
Problema Multiplu Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.21 kb
#include <cstdio>
#include <fstream>
using namespace std;
int cmmdc(int a,int b)
{
    int r;
    while(r!=0)
    {
        r=a%b;
        a=b;
        b=r;
    }
    return a;
}
int a,b;
struct coada
{
    bool cifra;
    int rest;
    int tata;
};
char ras[2000001];
coada q[2000001];
bool resturi[20000001];
int main()
{
    freopen("multiplu.in","r",stdin);
    freopen("multiplu.out","w",stdout);
    int a,b;
    scanf("%d%d",&a,&b);
    int cm=(a*b)/cmmdc(a,b),p,u,i,j,c;
    p=u=1;
    coada aux;
    aux.cifra=1;
    aux.tata=0;
    aux.rest=(aux.cifra)%cm;
    q[p]=aux;
    while(p<=u)
    {
        if(q[p].rest==0)
            break;
       for(c=0;c<=1;c++)
         {
           aux.cifra=c;
           aux.rest=(q[p].rest*10+c)%cm;
           aux.tata=p;
           if(resturi[aux.rest]==false)
               {
                   resturi[aux.rest]=true;
                   u++;
                   q[u]=aux;
               }
         }
       p++;
    }
    i=1;
    while(p!=0)
    {
       ras[i]=q[p].cifra;
       i++;
       p=q[p].tata;
    }
    ofstream g("multiplu.out");
    for(j=i-1;j>=1;j--)
    {
        g<<ras[j];
    }
    return 0;
}