Cod sursa(job #2967533)

Utilizator Razvan_GabrielRazvan Gabriel Razvan_Gabriel Data 19 ianuarie 2023 18:53:23
Problema Multiplu Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.4 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

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

const int VMAX = 2000001;

bool vf[VMAX];
queue <int> q;
int parinte[VMAX], cif[VMAX];

int m;

void bfs()
{
    q.push(1);
    parinte[1]= -1;
    cif[1] = 1;
    vf[1] = true;

    while(!q.empty())
    {
        int x = q.front();
        q.pop();
        int f1 = (x * 10 + 1) % m;
        int f0 = (x * 10) % m;

        if(!vf[f0])
        {
            vf[f0]=  true;
            q.push(f0);
            parinte[f0] = x;
            cif[f0] = 0;
            if(f0 == 0)
                return;
        }

        if(!vf[f1])
        {
            vf[f1] = true;
            q.push(f1);
            parinte[f1] = x;
            cif[f1] = 1;
            if(f1 == 0)
                return;
        }

    }
}

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

    int a, b;
    fin >> a >> b;
    m = a * b / cmmdc(a, b);

    bfs();

    vector <int> rasp;
    int p = parinte[0];
    rasp.push_back(cif[0]);
    while(p != -1)
    {
        rasp.push_back(cif[p]);
        p = parinte[p];
    }

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


    return 0;
}