Cod sursa(job #118321)

Utilizator sima_cotizoSima Cotizo sima_cotizo Data 24 decembrie 2007 15:04:29
Problema Multiplu Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.06 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 < long > Q;
pair<bool, pair<long ,long> > V[2000101];

void print(long);

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

	M = A*B / cmmdc(A,B);
	Q.push( 1 ); V[1].first = true; V[1].second.first = -1, V[1].second.second=1;
	while ( !Q.empty() ) {
		long x;
		x = Q.front(); Q.pop();
		if ( !V[(x*10) % M].first ) {
			Q.push((x*10)%M);
			V[(x*10)%M].first = true;
			V[(x*10)%M].second.first = x;
			V[(x*10)%M].second.second= 0;
		}
		if ( !V[(x*10+1) % M].first ) {
			Q.push((x*10)%M);
			V[(x*10+1)%M].first = true;
			V[(x*10+1)%M].second.first = x;
			V[(x*10+1)%M].second.second= 1;
		}
		if ( V[0].first ) 
			break;
	}

	freopen("multiplu.out","w", stdout);
	print(0);
	fclose(stdout);
	return 0;
}




void print(long x) {
	if ( x==-1 )
		return;

	print(V[x].second.first);
	printf("%ld", V[x].second.second);
}