Pagini recente » Cod sursa (job #2633170) | Monitorul de evaluare | Cod sursa (job #1636359) | Cod sursa (job #3316476) | Cod sursa (job #3306076)
#include <bits/stdc++.h>
using namespace std;
ifstream f("multiplu.in");
ofstream g("multiplu.out");
int a, b, v[2000005], x, cif[2000005], ult[2000005];
queue <int> q;
stack <int> st;
int cmmmc (int a, int b) {
int n = a, m = b, r;
if (n < m)
swap (n, m);
while (m) {
r = n % m;
n = m;
m = r;
}
return a * b / n;
}
void afisare (int k) {
while (k != 1) {
st.push (cif[k]);
k = ult[k];
}
g << 1;
while (!st.empty()) {
g << st.top();
st.pop();
}
}
void solve () {
q.push (1); v[1] = 1;
bool ok = true;
while (ok) {
int k = q.front (); q.pop();
if (k == 0) {
afisare (k);
return;
}
else {
int aux;
aux = k * 10 % x;
if (v[aux] == 0) {
q.push (aux); v[aux] = 1;
cif[aux] = 0; ult[aux] = k;
}
aux = (k * 10 + 1) % x;
if (v[aux] == 0) {
q.push (aux); v[aux] = 1;
cif[aux] = 1; ult[aux] = k;
}
}
}
}
int main()
{
f >> a >> b;
x = cmmmc (a, b);
solve ();
return 0;
}