Pagini recente » Cod sursa (job #1933910) | Cod sursa (job #3131161) | Cod sursa (job #2752365) | Cod sursa (job #968581) | Cod sursa (job #1226536)
#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, r2;
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 {
r2 = r * 10 % m;
if (rest[r2].second == 0 && r2 != x) {
rest[r2].first = 0;
rest[r2].second = r;
q.push(rest[r2]);
if (r2 == 0) done = true;
}
r2 = (r * 10 + 1) % m;
if (rest[r2].second == 0 && r2 % m != x) {
rest[r2].first = 1;
rest[r2].second = r;
q.push(rest[r2]);
if (r2 == 0) done = true;
}
}
}
}
int main () {
int a, b;
f >> a >> b;
m = a * b / cmmdc(a, b);
rezolva();
scrie(0);
return 0;
}