Cod sursa(job #245807)

Utilizator ilincaSorescu Ilinca ilinca Data 18 ianuarie 2009 22:16:51
Problema Multiplu Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.85 kb
#include <stdio.h>
#include <vector>

using namespace std;

struct queue 
{
	int r, c, p;
};

int A, B, LCM;
queue q [3000005];
vector <bool> v (3000005);


int GCD (int A, int B)
{
	int r;
	while (B)
	{
		r=A%B;
		A=B;
		B=r;
	}
	return A;
}

int BFS ()
{
	int f, l, c, rest;
	f=l=1;
	q [1].r=1;
	q [1].p=0;
	q [1].c=1;
	while (f <= l)
	{
		for (c=0; c <= 1; ++c)
		{
			rest=(q [f].r*10+c)%LCM;
			if (v [rest] == false)
			{
				v [rest]=true;
				q [++l].r=rest;
				q [l].p=f;
				q [l].c=c;
				if (rest == 0)
					return l;
			}
		}
		++f;
	}
	return 0;
}

void print (int p)
{
	if (q [p].p)
		print (q [p].p);
	printf ("%d", q [p].c);
}

int main ()
{
	freopen ("multiplu.in", "r", stdin);
	freopen ("multiplu.out", "w", stdout);
	scanf ("%d%d", &A, &B);
	LCM=A*B/GCD (A, B);
	if (LCM == 1)
		printf ("1");
	else
		print (BFS ());
	printf ("\n");
	return 0;
}