Cod sursa(job #3252441)

Utilizator Tudor567Voica Tudor Tudor567 Data 29 octombrie 2024 17:02:26
Problema Multiplu Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.14 kb
#include<iostream>
#include<vector>
#include<fstream>
#include<climits>
#include<queue>

using namespace std;

ifstream fin("multiplu.in");
ofstream fout("multiplu.out");
int a,b,cmmc;
vector<bool>nr;
int pre[2000001];
queue<int> q;


int cmmmc(int a, int b){
    int x = a*b;
    int r = a % b;
    while(r != 0){
    a = b;
    b = r;
    r = a % b;
    }
    return x/b;
}

void recon(){

}

void multiplu(){
    int x = q.front();
    q.pop();
    while(x%cmmc != 0){
        int y = (10*x)%cmmc ,z = (10*x+1)%cmmc;

        if(y != x && pre[y] == 0){
        pre[y] = x;
        q.push(y);

        }

        if(z != x && pre[z] == 0){
        pre[z] = x;
        q.push(z);
        }

        x = q.front();

        q.pop();
    }

    pre[cmmc] = pre[x];
    x = cmmc;

    while(x != -1){
        nr.push_back((x - 10*pre[x])%cmmc);
        x = pre[x];
    }

    }


int main(){
    fin>>a>>b;
    cmmc = cmmmc(a,b);
    if(cmmc == 1)fout<<1;
    else{
    q.push(1 % cmmc);
    pre[1 % cmmc] = -1;
    multiplu();
    for(int i = nr.size()-1; i >= 0; i--)fout<<nr[i];
    }
}