Cod sursa(job #2299203)

Utilizator radugheoRadu Mihai Gheorghe radugheo Data 9 decembrie 2018 00:47:10
Problema Multiplu Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.96 kb
#include <bits/stdc++.h>

using namespace std;

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

const int NMAX = 2000000;

bool used[NMAX+2];
int pre[NMAX+2], tip[NMAX+2];
string sol;

int main()
{
    int A,B,M;
    in >> A >> B;
    if( A * B == 1 ) {
        out << "1\n";
        return 0;
    }
    M = A / __gcd(A,B) * B;
    queue<int> Q;
    Q.push(1);
    used[1] = 1;
    tip[1] = 1;
    pre[1] = -1;
    int gasit = 0;
    while( !Q.empty() && !gasit ) {  /// meh
        int nod = Q.front();
        Q.pop();
        for( int cif : {0, 1} ) {
            int x = (10 * nod + cif) % M;
            if( used[x] ) continue;
            used[x] = 1;
            pre[x] = nod;
            tip[x] = cif;
            Q.push(x);
        }
    }
    while( gasit != -1 ) {
        sol.push_back('0' + tip[gasit]);
        gasit = pre[gasit];
    }
    reverse(sol.begin(), sol.end());
    out << sol << '\n';
    return 0;
}