Cod sursa(job #477643)

Utilizator crushackPopescu Silviu crushack Data 15 august 2010 18:55:15
Problema Multiplu Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.03 kb
#include <stdio.h>
#define ABMAX 2000000

const char IN[]   ="multiplu.in";
const char OUT[]  ="multiplu.out";

int A,B;

int c[ABMAX],Pr[ABMAX],cif[ABMAX],rz[ABMAX+1];
bool b[ABMAX+1];

int cmmmc(int a,int b)
{
	int r,xa,xb;
	xa=a,xb=b;
	while (xb)
	{
		r=xa%xb;
		xa=xb;
		xb=r;
	}
	return (a*b)/xa;
}

void Rez()
{
	int in,sf;
	int M=cmmmc(A,B);
	in=sf=0;
	c[0]=1;b[1]=1;cif[0]=1;
	while (c[sf])
	{
		int x= c[in]*10 % M;
		if (!b[x])
		{
			sf++;
			c[sf]=x%M;
			Pr[sf]=in;
			cif[sf]=0;
			b[x]=1;
		}
		x= (x+1)%M;
		if (!b[x])
		{
			sf++;
			c[sf]=x%M;
			Pr[sf]=in;
			cif[sf]=1;
			b[x]=1;
		}
		in++;
	}
	for (int i=sf;;i=Pr[i])
	{
		rz[++rz[0]]=cif[i];
		if (!i)
			break;
	}
}

void citire()
{
	freopen(IN,"r",stdin);
	scanf("%d%d",&A,&B);
	fclose(stdin);
}

void scriere(int *x)
{
	int i;
	freopen(OUT,"w",stdout);
	for (i=x[0];i>0;i--)
		printf("%d",x[i]);
	printf("\n");
	fclose(stdout);
}

int main()
{
	citire();
	Rez();
	scriere(rz);
	return 0;
}