Cod sursa(job #2786357)

Utilizator pielevladutPiele Vladut Stefan pielevladut Data 20 octombrie 2021 19:39:45
Problema Multiplu Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.17 kb
#include <bits/stdc++.h>

using namespace std;

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

int a, b;
queue<int> coada;
bool w[2002002];
bool vis[2002002];
int prv[2002002];

void sol(int val)
{
    if(val == 1)
    {
        fout << val;
        return ;
    }
    sol(prv[val]);
    fout << w[val];
}

int main()
{
    fin >> a >> b;
    int lcm = a * b / __gcd(a, b);
    w[1] = 1;
    vis[1] = true;
    coada.push(1);
    while(!coada.empty())
    {
        int nod = coada.front();
        coada.pop();
        int newnod = nod * 10;
        int newrest = newnod % lcm;
        if(!vis[newrest])
        {
            coada.push(newrest);
            vis[newrest] = true;
            w[newrest] = 0;
            prv[newrest] = nod;
            if(newrest == 0)
                break;
        }
        newnod = nod * 10 + 1;
        newrest = newnod % lcm;
        if(!vis[newrest])
        {
            coada.push(newrest);
            vis[newrest] = true;
            w[newrest] = 1;
            prv[newrest] = nod;
            if(newrest == 0)
                break;
        }
    }
    sol(0);
    return 0;
}