Cod sursa(job #2263140)

Utilizator TavinciStefanescu Octavian Tavinci Data 18 octombrie 2018 11:45:30
Problema Multiplu Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.27 kb

#include <bits/stdc++.h>
#define NMAX 2000002
using namespace std;

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

int pre[NMAX], up[NMAX];
bool verif[NMAX];
//string sol;

void afis(int g)
{
    if(g!=-1)
    {
        afis(pre[g]);
        fout<<up[g];
    }
}

queue<int> Q;
int main()
{
    int a,b,m;
    fin>>a>>b;
    if( a * b == 1 )
    {
        fout << "1";
    }
    else
    {
        m = a / __gcd(a,b) * b;

        Q.push(1);
        verif[1] = 1;
        up[1] = 1;
        pre[1] = -1;
        int gasit = 0;
        while( !Q.empty() && gasit==0)
        {
            int nod = Q.front();
            Q.pop();
            for(int i=0; i<=1; i++)
            {
                int x = (10 * nod + i) % m;
                if(verif[x]==0)
                {
                    verif[x] = 1;
                    pre[x] = nod;
                    up[x] = i;
                    Q.push(x);
                }
            }
        }
        afis(gasit);
        /*while( gasit!=-1)
        {
            sol.push_back('0' + up[gasit]);
            fout<<up[gasit];
            gasit = pre[gasit];
        }
        reverse(sol.begin(), sol.end());
        fout << sol << '\n';*/
    }
    return 0;
}