Cod sursa(job #2218992)

Utilizator miruna1224Floroiu Miruna miruna1224 Data 6 iulie 2018 18:00:02
Problema Multiplu Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.17 kb
#include <iostream>
#include <fstream>

using namespace std;

ofstream out("multiplu.out");

int *u,*pred;

void functie ( int x)
{
    if(x != 1)
        functie(pred[x]);
    out<<u[x];
}

int cmmdc( int a, int b)
{
    int r;
    while(b != 0)
    {
        r = b;
        b = a%b;
        a = r;
    }
    return a;
}

void BF(int n)
{
    int x,y;
    int st = 0;
    int dr = -1;
    int q[n+1];
    q[++dr] = 1;
    while(st <= dr)
    {
        x = q[st++];
        y = (x*10)%n;
        if(u[y] == -1)
        {
            u[y] = 0;
            pred[y] = x;
            q[++dr] = y;
        }
        y = (x*10 + 1)%n;
        if(u[y] == -1)
        {
            u[y] = 1;
            pred[y] = x;
            q[++dr] = y;
        }
        if( u[0] != -1){
            return;
        }
    }
}

int main()
{
    int a,b,m,i;
    ifstream in("multiplu.in");
    in>>a>>b;
    in.close();

    m = a/ cmmdc(a,b) * b;
    //cout<<m;
    u = new int[m+1];
    pred = new int[m+1];
    for(i=0; i<m; i++)
    {
        u[i] = -1;
    }
    u[1] = 1;
    BF(m);
    functie(0);

    out.close();
    return 0;
}