Cod sursa(job #2786356)

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

using namespace std;

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

int a, b;
queue<int> coada;
int w[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);
    for(int i = 0; i <= lcm; i ++)
    {
        w[i] = -1;
    }
    w[1] = 1;
    coada.push(1);
    while(!coada.empty())
    {
        int nod = coada.front();
        coada.pop();
        int newnod = nod * 10;
        int newrest = newnod % lcm;
        if(w[newrest] == -1)
        {
            coada.push(newrest);
            w[newrest] = 0;
            prv[newrest] = nod;
            if(newrest == 0)
                break;
        }
        newnod = nod * 10 + 1;
        newrest = newnod % lcm;
        if(w[newrest] == -1)
        {
            coada.push(newrest);
            w[newrest] = 1;
            prv[newrest] = nod;
            if(newrest == 0)
                break;
        }
    }
    sol(0);
    return 0;
}