Cod sursa(job #798724)

Utilizator alexclpAlexandru Clapa alexclp Data 17 octombrie 2012 00:22:33
Problema Multiplu Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.44 kb
#include <fstream>
#include <queue>

#define N 2000002

using namespace std;

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

int nr, a, b;
int u[N], pred[N], viz[N];


queue <int> coada;

int cmmmc(int x, int y)
{
    int r;
    int prod = x * y;
    
    r = x % y;
    while (r) {
        x = y;
        y = r;
        r = x % y;
    }
    
    return prod/y;
}

void recupereaza(int pozitie)
{
    int cifra = u[pozitie];
    
    if (pred[pozitie] == 0) {
        out << cifra;
        return;
    }
    recupereaza(pred[pozitie]);
    out << cifra;
}

void solve()
{
    if (nr == 1)  {
        out << "1";
        return;
    }
    u[1] = 1;
    viz[1] = 1;
    coada.push(1);
    
    while (!coada.empty()) {
        int restCurent = coada.front();
        if (restCurent == 0) {
            recupereaza(0);
            return;
        }
        
        int restNou;
        coada.pop();
        restNou = (restCurent * 10) % nr;
        
        if (viz[restNou] == 0) {
            viz[restNou] = 1;
            u[restNou] = 0;
            pred[restNou] = restCurent;
            coada.push(restNou);
        }
        
        restNou = (restCurent * 10 + 1) % nr;
        
        if (viz[restNou] == 0) {
            viz[restNou] = 1;
            u[restNou] = 1;
            pred[restNou] = restCurent;
            coada.push(restNou);
        }
    }
    
}

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