Cod sursa(job #2524361)

Utilizator StanCatalinStanCatalin StanCatalin Data 15 ianuarie 2020 16:20:26
Problema Multiplu Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.42 kb
#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;

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

const int dim = 2000005;

int a,b,cmmmc,q[dim],viz[dim];
int st = 0;
int dr = -1;

pair <int,int> drum[dim];

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

void BFS()
{
    q[++dr] = 1;
    viz[1] = 1;
    int ok = 0;
    while (ok == 0)
    {
        int x = q[st++];
        int now = (x*10)%cmmmc;
        if (!viz[now])
        {
            drum[now].first = x;
            drum[now].second = 0;
            if (now == 0)
            {
                ok = 1;
            }
            else
            {
                q[++dr] = now;
                viz[now] = 1;
            }
        }
        now = (x*10+1)%cmmmc;
        if (!viz[now])
        {
            drum[now].first = x;
            drum[now].second = 1;
            if (now == 0)
            {
                ok = 1;
            }
            else
            {
                q[++dr] = now;
                viz[now] = 1;
            }
        }
    }
}

int main()
{
    in >> a >> b;
    cmmmc = (a*b)/cmmdc(a,b);
    BFS();
    int acm = 0;
    string s = "";
    while (acm != 1)
    {
        s += (drum[acm].second + '0');
        acm = drum[acm].first;
    }
    s += '1';
    reverse(s.begin() , s.end());
    out << s;
    return 0;
}