Cod sursa(job #2711741)

Utilizator mariamirabella2Bucur-Sabau Maria-Mirabela mariamirabella2 Data 24 februarie 2021 17:32:34
Problema Multiplu Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.59 kb
#include <fstream>

using namespace std;

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

const int NMAX = 2000001;
struct MULTIPLU
{
    bool c;
    int r, pred;
};
MULTIPLU q[NMAX];
bool viz[NMAX], sol[NMAX];

int cmmdc(int a, int b)
{
    int r;
    while (b != 0)
    {
        r = a % b;
        a = b;
        b = r;
    }
    return a;
}

int cmmmc(int a, int b)
{
    int c;
    c = cmmdc(a, b);
    return a * b / c;
}

int main()
{
    int A, B, M, p, u, ok, i, n;
    n = 0;
    fin >> A >> B;
    if (A == 1 && B == 1)
    {
        fout << 1;
        return 0;
    }
    M = cmmmc(A, B);
    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) % 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++;
    }
    while (u != 0)
    {
        sol[++n] = q[u].c;
        u = q[u].pred;
    }
    for (i = n; i >= 1; i--)
        fout << sol[i];
    return 0;
}