Pagini recente » Cod sursa (job #1162975) | Cod sursa (job #2670275) | Cod sursa (job #777475) | Cod sursa (job #2427510) | Cod sursa (job #2965782)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream cin("multiplu.in");
ofstream cout("multiplu.out");
const int NMAX = 2e6;
const int inf = 1e9;
queue <int> q;
int 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[x] = 1;
while(!q.empty()){
int x = q.front();
q.pop();
// ma duc fie in rest r, fie in rest r + 1
int rm = x % cmmmc;
//cout << x << " cu restul " << rm << "\n";
if(rm == 0)
return;
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;
cmmmc = x * y / cmmdc(x, y);
for(int i = 0; i < cmmmc; i++)
min_num[i] = inf;
bfs(1);
cout << min_num[0];
}