Cod sursa(job #3160406)

Utilizator iusty64Iustin Epanu iusty64 Data 23 octombrie 2023 21:42:55
Problema Multiplu Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.22 kb
#include <fstream>
#include <vector>
#include <queue>
#include <climits>
using namespace std;
ifstream fin("multiplu.in");
ofstream fout("multiplu.out");
const int Vmax = 2000005;
int a, b;
queue<int> Q;
vector<int> dist(Vmax, INT_MAX);
long long cat[Vmax*100];

int cmmmc(int a, int b){
    int x = a;
    int y = b;
    while(x!=y){
        if(x>y)
            x-=y;
        else
            y-=x;
    }
    return (a*b)/x;
}

long long bfs(int rez_cmmmc){
    Q.push(1);
    cat[1]=0;
    dist[1]=0;
    while(!Q.empty()){
        int rest_nod = Q.front();
        Q.pop();
        for(int i=0;i<2;i++){
            long long vec = (cat[rest_nod]*rez_cmmmc+rest_nod)*10+i;
            long long rest_vec = vec%rez_cmmmc;
            if(rest_nod==rest_vec)
                continue;
            cat[rest_vec] = vec/rez_cmmmc;
            if(rest_vec==0)
                return vec;
            else
                if(dist[rest_vec]==INT_MAX){
                    dist[rest_vec]=dist[rest_nod]+1;
                    Q.push(rest_vec);
                }
        }
    }
}

int main()
{
    fin>>a>>b;
    int rez_cmmmc;
    rez_cmmmc = cmmmc(a, b);
    fout<<bfs(rez_cmmmc);
    return 0;
}