Cod sursa(job #2736467)

Utilizator vansJos da pa perete vans Data 3 aprilie 2021 14:59:05
Problema Multiplu Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <bits/stdc++.h>

using namespace std;

const int NMAX = 2e6;

int a, b, cmmmc, t[NMAX + 1];
bitset<NMAX + 1> v;

inline int Cmmmc(const int X, const int Y) {
    return X / __gcd(X, Y) * Y;
}

void bfs() {
    queue<int> q;
    q.push(1), t[1] = -1, v[1] = 1;
    while(!q.empty()) {
        const int cx = q.front();
        q.pop();

        if(!cx) return;

        for(int i = 0; i < 2; ++i) {
            const int ncx = (cx * 10 + i) % cmmmc;
            if(!t[ncx])
                t[ncx] = cx,
                v[ncx] = i;
                q.push(ncx);
        }
    }
}

inline void afis(const int X = 0) {
    if(X == -1) return;
    afis(t[X]);
    cout<<v[X];
}

int main()
{
    freopen("multiplu.in", "r", stdin);
    freopen("multiplu.out", "w", stdout);
    scanf("%d%d", &a, &b), cmmmc = Cmmmc(a, b);
    bfs(), afis();
    return 0;
}