Cod sursa(job #3306076)

Utilizator petric_mariaPetric Maria petric_maria Data 7 august 2025 13:05:46
Problema Multiplu Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.26 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("multiplu.in");
ofstream g("multiplu.out");

int a, b, v[2000005], x, cif[2000005], ult[2000005];
queue <int> q;
stack <int> st;

int cmmmc (int a, int b) {
    int n = a, m = b, r;
    if (n < m)
        swap (n, m);
    while (m) {
        r = n % m;
        n = m;
        m = r;
    }
    return a * b / n;
}

void afisare (int k) {
    while (k != 1) {
        st.push (cif[k]);
        k = ult[k];
    }
    g << 1;
    while (!st.empty()) {
        g << st.top();
        st.pop();
    }
}

void solve () {
    q.push (1);  v[1] = 1;
    bool ok = true;
    while (ok) {
        int k = q.front ();  q.pop();
        if (k == 0) {
            afisare (k);
            return;
        }
        else {
            int aux;
            aux = k * 10 % x;
            if (v[aux] == 0) {
                q.push (aux);  v[aux] = 1;
                cif[aux] = 0;  ult[aux] = k;
            }

            aux = (k * 10 + 1) % x;
            if (v[aux] == 0) {
                q.push (aux);  v[aux] = 1;
                cif[aux] = 1;  ult[aux] = k;
            }
        }
    }
}

int main()
{
    f >> a >> b;
    x = cmmmc (a, b);
    solve ();
    return 0;
}