Cod sursa(job #2940993)

Utilizator tudor_costinCostin Tudor tudor_costin Data 16 noiembrie 2022 21:29:21
Problema Multiplu Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.36 kb
#include <iostream>
#include <fstream>
#include<queue>
using namespace std;
ifstream fin("multiplu.in");
ofstream fout("multiplu.out");
const int Nmax=2000005;
int a,b,n;
queue<int> q;
int prv[Nmax];
bool visited[Nmax];
int cmmdc(int a,int b)
{
    int r=a%b;
    while(r>0)
    {
        a=b;
        b=r;
        r=a%b;
    }
    return b;
}
void reconst(int rest)
{
    if(rest==1)
    {
        fout<<1;
        return;
    }
    reconst(prv[rest]);
    if(prv[rest]*10%n==rest)
    {
        fout<<0;
    }
    else
    {
        fout<<1;
    }
}
int main()
{
    fin>>a>>b;
    n=a*b/cmmdc(a,b);
    q.push(1);
    visited[1]=true;
    if(n==1)
    {
        fout<<n;
        return 0;
    }
    while(!q.empty())
    {
        int rest=q.front();
        q.pop();
        int next_rest = (rest*10)%n;
        if(!visited[next_rest])
        {
            q.push(next_rest);
            visited[next_rest] =true;
            prv[next_rest]=rest;
        }
        if(next_rest==0)
        {
            break;
        }
        next_rest = (rest*10+1)%n;
        if(!visited[next_rest])
        {
            q.push(next_rest);
            visited[next_rest]=true;
            prv[next_rest]=rest;
        }
        if(next_rest==0)
        {
            break;
        }
    }
    reconst(0);
    return 0;
}