Cod sursa(job #118314)

Utilizator sima_cotizoSima Cotizo sima_cotizo Data 24 decembrie 2007 14:31:34
Problema Multiplu Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.96 kb
#include <cstdio>
#include <string>
#include <queue>
#include <utility>
using namespace std;

long A,B,M;

long cmmdc(long a, long b) {
	if ( a<b ) { long r = a; a = b; b = r; }
	while ( b ) { long r = a%b; a=b; b=r; }
	return a;
}

queue < pair<string, long> > Q;
pair<bool, string> V[20000001];

int main() { 
	fscanf(fopen("multiplu.in", "r"), "%ld %ld", &A, &B);

	M = A*B / cmmdc(A,B);
	Q.push(make_pair("1", 1)); V[1].first = true;
	while ( !Q.empty() ) {
		pair <string, long> x = Q.front(); Q.pop();
		if ( !V[(x.second*10) % M].first ) {
			Q.push(make_pair(x.first+"0",(x.second*10)%M));
			V[(x.second*10)%M].first = true;
			V[(x.second*10)%M].second = x.first+"0";
		}
		if ( !V[(x.second*10+1) % M].first ) {
			Q.push(make_pair(x.first+"1",(x.second*10)%M));
			V[(x.second*10+1)%M].first = true;
			V[(x.second*10+1)%M].second = x.first+"1";
		}
		if ( V[0].first ) 
			break;
	}
	
	fprintf(fopen("multiplu.out", "w"), "%s", V[0].second.c_str());
	return 0;
}