Pagini recente » Cod sursa (job #2533273) | Cod sursa (job #2704131) | Cod sursa (job #937022) | Cod sursa (job #2295179) | Cod sursa (job #3160297)
#include <fstream>
#include <queue>
#include <string>
#include <vector>
#include <algorithm>
#include <climits>
using namespace std;
ifstream f("multiplu.in");
ofstream g("multiplu.out");
const int nmax = 2000005;
vector<int> poz(nmax, INT_MAX);//anterioara cifra
int pre[nmax];// poz anerior
string s;
int a, b;
int main(){
f >> a >> b ;
int x = a, y = b;
int m ;
while(b != 0){
int r = a % b;
a = b;
b = r;
}
m = x * y / a;
//g << m << '\n';
if(m == 1){
g << "1\n";
return 0;
}
queue<int> q;
q.push(1);
pre[1] = -1;
poz[1] = 1;
int ultima_cifra;
while(!q.empty()){
int nr = q.front();
q.pop();
int x = (nr * 10) % m;
if(poz[x] > poz[nr] + 1){
poz[x] = poz[nr] + 1;
pre[x] = nr;
q.push(x);
}
if(x == 0){
ultima_cifra = 0;
break;
}
x = (nr * 10 + 1) % m;
if(poz[x] > poz[nr] + 1){
poz[x] = poz[nr] + 1;
pre[x] = nr;
q.push(x);
}
if(x == 0){
ultima_cifra = 1;
break;
}
}
// g << poz[0] << ' ' << ultima_cifra;
if(ultima_cifra == 1){
s.push_back('1');
}
else{
s.push_back('0');
}
// g << s << '\n';
int z = pre[0];
for(int i = 1; i < poz[0]; i++){
int cifra = 0;
if(pre[z] * 10 % m == z){
s.push_back('0');
}
else{
s.push_back('1');
}
z = pre[z];
}
reverse(s.begin(), s.end());
g << s << '\n';
}