Cod sursa(job #2966659)

Utilizator Theodor17Pirnog Theodor Ioan Theodor17 Data 18 ianuarie 2023 07:48:14
Problema Multiplu Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.32 kb
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

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

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

queue <unsigned long long> q;
unsigned 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);
    min_num[1] = 1;

    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;
        min_num[rm] = x;
        //cout << x << " cu restul " << rm << "\n";

        if(rm == 0)
            return;

        rm *= 10;
        rm %= cmmmc;

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

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

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

    }

}

int main(){

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

    if(x == y){

        cout << x;
        return 0;

    }

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

    bfs(1);
    cout << min_num[0];

}