Cod sursa(job #2809386)

Utilizator cristia_razvanCristia Razvan cristia_razvan Data 26 noiembrie 2021 21:04:24
Problema Multiplu Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.15 kb
#include <bits/stdc++.h>
using namespace std;

#define push_back pb
using ll = long long;

const string fn = "multiplu";

ifstream fin(fn + ".in");
ofstream fout(fn + ".out");

const int mxn = 2000000;
int viz[mxn + 5];
int pre[mxn + 5];
queue<int> q;

int a, b, m, n;

void reconst(int r)
{
    cout << r << " ";
    if (r == 1)
        fout << 1;
    else {
        reconst(pre[r]);
        if ((pre[r] * 10) % m == r)
            fout << 0;
        else fout << 1;
    }
}

int main() {

    fin >> a >> b;
    m = (a * b) / __gcd(a, b);

    q.push(1);
    viz[1] = 1;

    while (true)
    {
        n = q.front();
        q.pop();
        int val = n * 10 % m;
        if (!viz[val])
        {
            pre[val] = n;
            viz[val] = 1;
            if (val == 0)
                break;
            q.push(val);
        }
        val = (n * 10 + 1) % m;
        if (!viz[val])
        {
            pre[val] = n;
            viz[val] = 1;
            if (val == 0)
                break;
            q.push(val);
        }
    }

    reconst(0);

    fin.close();
    fout.close();
    return 0;
}