Cod sursa(job #1021213)

Utilizator Mihai22eMihai Ionut Enache Mihai22e Data 3 noiembrie 2013 15:03:41
Problema Multiplu Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.12 kb
#include <fstream>
#include <vector>
#include <queue>
using namespace std;

const int MAX_M = 2000002;

int A, B;
int Prev[MAX_M];
queue < int > Q;
vector < char > ans;
char c[MAX_M];

inline int gcd(int a, int b) {
    int r;
    while(b) {
        r = a % b;
        a = b;
        b = r;
    }
    return a;
}

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

    f >> A >> B;

    int M = (A*B)/gcd(A, B);
    Q.push(1%M);
    Prev[1%M] = 1;
    c[1%M] = '1';
    while(!Q.empty()) {
        int x = Q.front();
        Q.pop();

        int y = (x*10)%M;
        if(c[y] == 0) {
            Prev[y] = x;
            c[y] = '0';
            Q.push(y);
        }

        y = (x*10 + 1)%M;
        if(c[y] == 0) {
            Prev[y] = x;
            c[y] = '1';
            Q.push(y);
        }
    }

    int x = 0;
    while(x != 1) {
        ans.push_back(c[x]);
        x = Prev[x];
    }
    ans.push_back('1');

    for(int i = ans.size() - 1; i >= 0; --i)
        g << ans[i];
    g << "\n";

    f.close();
    g.close();

    return 0;
}