Cod sursa(job #2711686)

Utilizator uncle_sam_007IOAN BULICA uncle_sam_007 Data 24 februarie 2021 16:41:36
Problema Multiplu Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.7 kb

#include <fstream>

using namespace std;
const int NMAX=2000005;
struct MULTIPLU
{
    bool c;
    int r,pred;
};
MULTIPLU q[NMAX];
bool viz[NMAX],sol[NMAX];
int main()
{
    ifstream fin("multiplu.in");
    ofstream fout("multiplu.out");
    int a,b,c,a1,b1,m,p,u;
    fin>>a>>b;
    a1=a;
    b1=b;
    if(a==1 && b==1)
    {
        fout<<"1"<<'\n';
    }
    else
    {
        while(b!=0)
        {
            c=a%b;
            a=b;
            b=c;
        }
        m=(a1*b1)/a;
        MULTIPLU temp1,temp2;
        p=1;
        u=0;
        temp1.c=1;
        temp1.r=1;
        temp1.pred=0;
        viz[1]=1;
        q[++u]=temp1;
        int ok=0;
        while(p<=u&&ok==0)
        {
            temp1=q[p];
            temp2.c=0;
            temp2.r=(temp1.r*10+0)%m;
            temp2.pred=p;
            if(viz[temp2.r]==0)
            {
                viz[temp2.r]=1;
                q[++u]=temp2;
            }
            if(temp2.r==0)
            {
                ok=1;
                break;
            }
            temp2.c=1;
            temp2.r=(temp1.r*10+1)%m;
            temp2.pred=p;
            if(viz[temp2.r]==0)
            {
                viz[temp2.r]=1;
                q[++u]=temp2;
            }
            if(temp2.r==0)
            {
                ok=1;
                break;
            }
            p++;
        }
        int poz1=u,poz2=0;
        while(poz1!=0)
        {
            sol[poz2]=q[poz1].c;
            poz2++;
            poz1=q[poz1].pred;
        }
        for(int i=poz2-1; i>=0; i--)
        {
            fout<<sol[i];
        }
        fout<<'\n';
    }
    return 0;
}