Cod sursa(job #2401342)

Utilizator Andrei_CotorAndrei Cotor Andrei_Cotor Data 9 aprilie 2019 17:04:48
Problema Multiplu Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.91 kb
#include<fstream>
#include<algorithm>
#include<queue>
using namespace std;
ifstream fi("multiplu.in");
ofstream fo("multiplu.out");
int n,Rez[2000005],nrez,i,m,vf,P[2000005],C[2000005],a,b,val,nod;
deque<int> Q;
int main()
{
    fi>>a>>b;
    m=(a*b)/__gcd(a,b);
    P[1]=-1;
    C[1]=1;
    Q.push_back(1);
    while(!Q.empty())
    {
        vf=Q.front();
        Q.pop_front();
        for(i=0; i<=1; i++)
        {
            val=(vf*10+i)%m;
            if(P[val]==0)
            {
                C[val]=i;
                P[val]=vf;
                Q.push_back(val);
            }
            if(val==0)
                break;
        }
        if(val==0)
            break;
    }
    nod=0;
    while(nod!=-1)
    {
        Rez[++nrez]=C[nod];
        nod=P[nod];
    }
    for(i=nrez; i>=1; i--)
        fo<<Rez[i];
    fo<<"\n";
	fi.close();
    fo.close();
    return 0;
}