Cod sursa(job #2711693)

Utilizator Andrei_TudorAndrei Tudor Andrei_Tudor Data 24 februarie 2021 16:45:51
Problema Multiplu Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.3 kb
#include <fstream>

using namespace std;
ifstream cin("multiplu.in");
ofstream cout("multiplu.out");
const int NMAX = 2000001;
bool viz[NMAX], sol[NMAX];
struct MULTIPLU {
    bool c;
    int r, pred;
};
MULTIPLU q[NMAX];

int main()
{
    int a, b, M, ca, cb, r, p, u, ok, k;
    cin >> a >> b;
    if(a == 1 && b == 1){
        cout << 1;
        return 0;
    }
    ca = a;
    cb = b;
    while(b != 0) {
        r = a % b;
        a = b;
        b = r;
    }
    M = ca * cb / a;
    MULTIPLU temp1, temp2;
    p = 1; u = 0;
    temp1.c = 1;
    temp1.r = 1;
    temp1.pred = 0;
    viz[1] = 1;
    q[++ u] = temp1;
    ok = 0;
    while(p <= u && ok == 0){
        temp1 = q[p];
        temp2.c = 0;
        temp2.r = (temp1.r * 10 + 0) % M;
        temp2.pred = p;
        if(viz[temp2.r] == 0) { viz[temp2.r] = 1; q[++ u] = temp2; }
        if(temp2.r == 0) { ok = 1; break; }
        temp2.c = 1;
        temp2.r = (temp1.r * 10 + 1) % M;
        temp2.pred = p;
        if(viz[temp2.r] == 0) { viz[temp2.r] = 1; q[++ u] = temp2; }
        if(temp2.r == 0) { ok = 1; break; }
        p ++;
    }
    k = 0;
    while(u != 0){
        sol[++ k] = q[u].c;
        u = q[u].pred;
    }
    for(int i = k; i >= 1; i --){
        cout << sol[i];
    }
    return 0;
}