Pagini recente » Cod sursa (job #2791275) | Cod sursa (job #58868) | Cod sursa (job #2040024) | Cod sursa (job #2600332) | Cod sursa (job #2966660)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream cin("multiplu.in");
ofstream cout("multiplu.out");
const int NMAX = 2e6;
const unsigned long long inf = 2e18;
queue <unsigned long long> q;
unsigned long long min_num[NMAX + 1], cmmmc;
int cmmdc(int x, int y){
int r = 0;
while(y){
r = x % y;
x = y;
y = r;
}
return x;
}
void bfs(int x){
q.push(x);
min_num[1] = 1;
while(!q.empty()){
unsigned long long x = q.front();
q.pop();
// ma duc fie in rest r, fie in rest r + 1
int rm = x % cmmmc;
min_num[rm] = x;
//cout << x << " cu restul " << rm << "\n";
if(rm == 0)
return;
rm *= 10;
rm %= cmmmc;
if(min_num[rm] > x * 10){
min_num[rm] = x * 10;
q.push(x * 10);
}
rm = rm * 10 + 1;
rm %= cmmmc;
if(min_num[rm] > x * 10 + 1){
min_num[rm] = x * 10 + 1;
q.push(x * 10 + 1);
}
}
}
int main(){
int x = 0, y = 0;
cin >> x >> y;
if(x == y){
cout << x;
return 0;
}
cmmmc = x * y / cmmdc(x, y);
for(int i = 0; i < cmmmc; i++)
min_num[i] = inf;
bfs(1);
cout << min_num[0];
}