Cod sursa(job #2711701)

Utilizator MiniaturePif123Ghica Alexandru Darius MiniaturePif123 Data 24 februarie 2021 16:49:28
Problema Multiplu Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.42 kb
#include <fstream>
using namespace std;
ifstream in("multiplu.in");
ofstream out("multiplu.out");
const int NMAX=2000001;
struct MULTIPLU
{
    bool c;
    int r,pred;
};
int cmmmc(int a, int b)
{
    int r,cmmmcx,ca,cb,cmmdc;
    ca=a;
    cb=b;
    while(b!=0)
    {
        r=a%b;
        a=b;
        b=r;
    }
    cmmdc=a;
    a=ca;
    b=cb;
    cmmmcx=a/cmmdc*b;
    return cmmmcx;
}
MULTIPLU q[NMAX];
bool viz[NMAX],sol[NMAX];
int main()
{
    int a,b,m,p,u,i,k,ok;
    in>>a>>b;
    m=cmmmc(a,b);
    MULTIPLU temp1,temp2;
    p=1;u=0;
    temp1.c=1;temp1.r=1;
    temp1.pred=0;
    viz[1]=1;
    q[++u]=temp1;
    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++;
    }
    k=0;
    while(u!=0)
    {
        sol[++k]=q[u].c;
        u=q[u].pred;
    }
    for(i=k;i>=1;i--)
    {
        out<<sol[i];
    }
    return 0;
}