Cod sursa(job #2967635)

Utilizator Theodor17Pirnog Theodor Ioan Theodor17 Data 19 ianuarie 2023 21:17:28
Problema Multiplu Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.26 kb
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

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

const int NMAX = 2e6;
const int inf = 1e9;

queue <int> q;
int min_num[NMAX + 1], go_back[NMAX + 1], cmmmc;

int cmmdc(int x, int y){

    int r = 0;
    while(y){

        r = x % y;
        x = y;
        y = r;

    }

    return x;
}

void bfs(int x){

    q.push(x);
    min_num[1] = 1;
    go_back[1] = -1;

    while(!q.empty()){

        int x = q.front();
        q.pop();

        int rm = x;

        if(rm == 0)
            return;

        rm *= 10;
        rm %= cmmmc;

        if(min_num[rm] == inf){
            
            min_num[rm] = 0;
            go_back[rm] = x;
            q.push(rm);
        
        }

        rm = x * 10 + 1;
        rm %= cmmmc;

        if(min_num[rm] == inf){
            
            min_num[rm] = 1;
            go_back[rm] = x;
            q.push(rm);
        
        }

    }

}

void afisare(int p){

    if(p == -1)
        return;

    afisare(go_back[p]);
    cout << min_num[p];

}

int main(){

    int x = 0, y = 0;
    cin >> x >> y;

    cmmmc = x * y / cmmdc(x, y);
    
    for(int i = 0; i < cmmmc; i++)
        min_num[i] = inf;

    bfs(1);
    
    int p = 0;
    afisare(p);

}