Cod sursa(job #1226534)

Utilizator diana97Diana Ghinea diana97 Data 5 septembrie 2014 22:55:07
Problema Multiplu Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.51 kb
#include <iostream>
#include <fstream>
#include <queue>

using namespace std;

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

const int MMAX = 2000000;

int m, x;
pair <int, int> rest[MMAX]; //first: cifra, second: rest anterior
queue < pair <int, int> > q;

inline int cmmdc(int a, int b) {
    if (b == 0) return a;
    return cmmdc(b, a % b);
}

inline void scrie(int k) {
    if (k != x) scrie(rest[k].second);
    //cout << rest[x].second << ' ';
    g << rest[k].first;
}


void rezolva() {
    x = 1 % m;
    //cout << x;
    rest[x].first = 1;
    rest[x].second = 0;
    q.push(rest[x]);
    bool done = false;
    int r;
    pair <int, int> p;
    while (!done && !q.empty()) {
        p = q.front();
        r = (p.first + p.second * 10) % m;
        //cout << r << ' ';
        q.pop();
        if (r == 0) done = true;
        else {
            if (rest[r * 10 % m].second == 0 && r * 10 % m != x) {
                rest[r * 10 % m].first = 0;
                rest[r * 10 % m].second = r;
                q.push(rest[r * 10 % m]);
                if (r * 10 % m == 0) done = true;
            }
            if (rest[(r * 10 + 1) % m].second == 0 && (r * 10 + 1) % m != x) {
                rest[(r * 10 + 1) % m].first = 1;
                rest[(r * 10 + 1) % m].second = r;
                q.push(rest[(r * 10 + 1) % m]);
                if ((r * 10 + 1) % m == 0) done = true;
            }
        }
    }
}

int main () {
    int a, b;
    f >> a >> b;
    m = a * b / cmmdc(a, b);
    rezolva();
    scrie(0);
}