Cod sursa(job #2801519)

Utilizator puica2018Puica Andrei puica2018 Data 16 noiembrie 2021 14:32:05
Problema Multiplu Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.2 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("multiplu.in");
ofstream fout("multiplu.out");

int a,b,m;
int last[2000005],pred[2000005];
queue <int> q;
bool vis[2000005];

int gcd(int a,int b)
{
    if(b==0)
        return a;
    return gcd(b,a%b);
}

stack <int> st;

void Afisare(int v)
{
    st.push(last[v]);
    while(pred[v]>0)
    {
        v=pred[v];
        st.push(last[v]);
    }
    while(!st.empty())
    {
        fout<<st.top();
        st.pop();
    }
    fout<<"\n";
}

int main()
{
    fin>>a>>b;
    if(a==1 && b==1)
    {
        fout<<"1\n";
        return 0;
    }
    m=a*b/gcd(a,b);
    q.push(1);
    vis[1]=1;
    last[1]=1;
    while(!q.empty())
    {
        int x=q.front();
        if(x==0)
        {
            Afisare(x);
            return 0;
        }
        q.pop();
        int t1=x*10%m,t2=(x*10+1)%m;
        if(vis[t1]==0)
        {
            q.push(t1);
            vis[t1]=1;
            last[t1]=0;
            pred[t1]=x;
        }
        if(vis[t2]==0)
        {
            q.push(t2);
            vis[t2]=1;
            last[t2]=1;
            pred[t2]=x;
        }
    }
    return 0;
}