Cod sursa(job #3164603)

Utilizator markjouhMark Zhou markjouh Data 3 noiembrie 2023 20:10:15
Problema Multiplu Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.83 kb
#include <bits/stdc++.h>

using namespace std;

int main() {
    freopen("multiplu.in", "r", stdin);
    freopen("multiplu.out", "w", stdout);
    cin.tie(0)->sync_with_stdio(0);

    int a, b;
    cin >> a >> b;
    
    const int m = lcm(a, b);

    vector<pair<int, int>> from(m, {-1, 0});
    queue<int> q;
    from[1] = {-1, 0};
    q.push(1);
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        for (int i = 0; i < 2; i++) {
            int v = (u * 10 + i) % m;
            if (from[v].first == -1) {
                from[v] = {u, i};
                q.push(v);
            }
        }
    }
    vector<bool> res;
    for (int v = 0; v != 1; v = from[v].first) {
        res.push_back(from[v].second);
    }
    res.push_back(1);
    reverse(res.begin(), res.end());
    for (int x : res) {
        cout << x;
    }
}