Cod sursa(job #2969283)

Utilizator andreea_chivuAndreea Chivu andreea_chivu Data 22 ianuarie 2023 19:45:31
Problema Multiplu Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.46 kb
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>

using namespace std;

#define PRODMAX 2000001

bool vf[PRODMAX];
queue <int> q;
int parinte[PRODMAX], cif[PRODMAX];
int m;
vector <int> rez;


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

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

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

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

        if(!vf[rest_1]){
            vf[rest_1] = true;
            q.push(rest_1);
            parinte[rest_1] = x;
            cif[rest_1] = 1;
            if(rest_1 == 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();

    rez.push_back(cif[0]);
    int p = parinte[0];
    while(p != -1){
        rez.push_back(cif[p]);
        p = parinte[p];
    }

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


    fin.close();
    fout.close();
    return 0;
}