Cod sursa(job #2718386)

Utilizator codrut86Coculescu Ioan-Codrut codrut86 Data 8 martie 2021 18:27:06
Problema Multiplu Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.05 kb
#include <fstream>
#include <bitset>
#include <queue>

using namespace std;

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

const int N = 2e6 + 1;
int pred[N];
bitset <N> ultim;

void M(int n)
{
    if(n == -1) return;
    M(pred[n]);
    out << ultim[n];
}

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

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

void bfs(int m)
{
    queue <int> q;
    q.push(1);
    pred[1] = -1;
    ultim[1] = 1;

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

        for(int i=0; i<2; i++)
        {
            int y = (x*10 + i) % m;
            if(pred[y] == 0)
            {
                ultim[y] = i;
                pred[y] = x;
                if(y == 0) return;
                q.push(y);
            }
        }
    }
}

int main()
{
    int a, b, m;
    in >> a >> b;

    m = cmmmc(a, b);
    bfs(m);

    M(0);

    return 0;
}