Pagini recente » Cod sursa (job #1489792) | Cod sursa (job #445835) | Cod sursa (job #2369900) | Cod sursa (job #3041009) | Cod sursa (job #2263896)
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
#include <queue>
using namespace std;
int a, b;
struct Solution {
int rest;
int binary;
};
queue<Solution> q;
bool inQueue[2000002];
int cmmdc(int a, int b) {
if (b == 0) {
return a;
}
return cmmdc(b, a % b);
}
void print(int x) {
if (x == 0) {
return;
}
print(x / 2);
printf("%d", x % 2);
}
int main() {
freopen("multiplu.in", "r", stdin);
freopen("multiplu.out", "w", stdout);
scanf("%d %d", &a, &b);
int m = a * b / cmmdc(a, b);
inQueue[1] = true;
q.push({ 1, 1 });
Solution nv[2];
int sol = 0;
while (!q.empty()) {
auto x = q.front();
nv[0] = { x.rest * 10 % m, x.binary << 1 };
nv[1] = { (x.rest * 10 + 1) % m, (x.binary << 1) + 1 };
for (int i = 0; i < 2; ++i) {
auto nx = nv[i];
if (nx.rest == 0) {
sol = nx.binary;
break;
}
if (!inQueue[nx.rest]) {
q.push(nx);
inQueue[nx.rest] = true;
}
}
q.pop();
}
print(sol);
return 0;
}