Cod sursa(job #2785353)

Utilizator Tudor_PascaTudor Pasca Tudor_Pasca Data 18 octombrie 2021 16:47:54
Problema Multiplu Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.51 kb
#include <iostream>
#include <fstream>
#include <queue>

using namespace std;

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

int A, B, multiple;
string ans;
pair<int, bool> father[2000005];

int cmmmc(int a, int b)
{
    int prod = a * b;

    while(b)
    {
        int r = a % b;
        a = b;
        b = r;
    }

    return prod / a;
}

void bfs()
{
    queue<int> q;
    q.push(1);

    while(!q.empty())
    {
        int node = q.front();
        q.pop();

        if(node == 0)
            return;

        int r0 = (node * 10) % multiple, r1 = (node * 10 + 1) % multiple;

        if(father[r0].first == 0)
        {
            father[r0] = make_pair(node, 0);
            q.push(r0);
        }

        if(father[r1].first == 0)
        {
            father[r1] = make_pair(node, 1);
            q.push(r1);
        }
    }
}

void retrace()
{
    int start = 0;

    while(father[start].first != 0)
    {
        //cout << start << ' ' << father[start].first << ' ' << father[start].second << ' ';

        if(father[start].second)
            ans += "1";
        else
            ans += "0";

        start = father[start].first;
    }

    ans += "1";

    //for(int i = ans.size() - 1; i >= 0; i--)
    //    out << ans[i];
}

int main()
{
    in >> A >> B;

    multiple = cmmmc(A, B);

    bfs();

    retrace();

    for(int i = ans.size() - 1; i >= 0; i--)
        out << ans[i];

    out << '\n';

    return 0;
}