Cod sursa(job #645343)

Utilizator toniobFMI - Barbalau Antonio toniob Data 9 decembrie 2011 12:21:01
Problema Multiplu Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.02 kb
#include <fstream>
#include <queue>
using namespace std;

ifstream in("multiplu.in");
ofstream out ("multiplu.out");

int a,b,m,nr[2000001],pred[2000001],sol;
queue <int> q;

int cmmdc (int a,int b){
	for(int c; b; c= a%b,a=b,b=c);
	return a;
}

int cmmmc (int a,int b) {
	return a * b / cmmdc (a,b);
}

void citire () {
	in >> a >> b;
	m = cmmmc (a,b);
	for (int i = 0 ; i < m; ++i){
		nr[i] =-1;
	}
}

void reconstruct(int y){
	if (y == 1){
		out << nr[y];
		return;
	}
	reconstruct(pred[y]);
	out << nr[y];
}

void bfs(){
	if (m == 1){
		out << "1\n";
		return;
	}
	int x,y;
	q.push(1);
	nr[1] =1;
	pred[1] = 0;
	while (!q.empty()){
		x = q.front();
		q.pop();
		y = (x * 10) % m;
		if (nr[y] == -1){
			q.push(y);
			nr[y] = 0;
			pred[y] = x;
		}
		if (y == 0){
			break;
		}
		y = (x*10+1)%m;
		if (nr[y] == -1){
			q.push(y);
			nr[y] = 1;
			pred[y] = x;
		}
		if (y == 0) {
			break;
		}
	}
	reconstruct(y);
}

int main () {
	citire();
	
	bfs();
	
	return 0;
}