Cod sursa(job #2714331)

Utilizator Tudor_1808Tudor Ioan Popescu Tudor_1808 Data 1 martie 2021 18:12:53
Problema Multiplu Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.96 kb
#include <fstream>
#include <queue>

using namespace std;
int a[101][101], n;
int v[101], pred[101], ultim[101];

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

void nr(int x){
    if(x==-1)return;
    nr(pred[x]);
    out<<ultim[x];
}

int cmmdc(int x, int y){
    while(y!=0){
        int r=x%y;
        x=y;
        y=r;
    }
    return x;
}

int cmmmc(int x, int y){
    return x / cmmdc(x,y)*y;
}

void bfs(int n){
    queue<int> q;
    q.push(1);
    pred[1]=-1;
    ultim[1]=1;
    while(!q.empty()){
        int x=q.front();
        q.pop();
        for(int i=0; i<2; i++){
            int y=(x*10+i)%n;
            if(pred[y]==0)
            {
                ultim[y]=i;
                pred[y]=x;
                if(y==0)return;
                q.push(y);
            }
        }
    }
}

int main()
{
    int a, b, x, n;
    in>>a>>b;
    n = cmmmc(a,b);
    bfs(n);
    nr(0);
    return 0;
}