Cod sursa(job #2965792)

Utilizator Theodor17Pirnog Theodor Ioan Theodor17 Data 16 ianuarie 2023 10:13:51
Problema Multiplu Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.14 kb
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

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

const int NMAX = 2e6;
const long long inf = 2e18;

queue <long long> q;
long long min_num[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);

    while(!q.empty()){

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

        // ma duc fie in rest r, fie in rest r + 1

        int rm = x % cmmmc;

        if(rm == 0)
            return;

        rm *= 10;
        rm %= cmmmc;

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

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

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

    }

}

int main(){

    long long 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);
    cout << min_num[0];

}