Cod sursa(job #2786348)

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

using namespace std;

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

int a, b;
queue<int> coada;
struct elem
{
    int unu, zero;
};
elem w[2002002];
int prv[2002002];

void sol(int val)
{
    if(val == 1)
    {
        fout << val;
        return ;
    }
    sol(prv[val]);
    if(w[val].unu != w[prv[val]].unu)
        fout << 1;
    if(w[val].zero != w[prv[val]].zero)
        fout << 0;
}

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