Cod sursa(job #147085)

Utilizator hadesgamesTache Alexandru hadesgames Data 2 martie 2008 16:20:15
Problema Multiplu Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.11 kb
#include <stdio.h>
FILE *in,*out;
char e[2000002];
int  d[2000002];
int  c[2000002][2];
int b,cmc,r,a,x,i,j,p,u,u2;
void af(int x)
{
	if (x==0)
		return ;
	af(c[x][1]);
	fprintf(out,"%d",c[x][0]);
}
int main()
{
	in=fopen("multiplu.in","r");
	out=fopen("multiplu.out","w");
	fscanf(in,"%d%d",&a,&b);
	cmc=a*b;
	r=a%b;
	while (r)
	{
		a=b;
		b=r;
		r=a%b;
	}
	cmc/=b;
	if (cmc==1)
	{
		fprintf(out,"1\n");
		fclose(in);
		fclose(out);
		return 0;
	}
	p=1;
	u=1;
	c[1][0]=1;
	c[1][1]=0;
	d[1]=1;
	while(666999)
	{
		u2=u;
		for (i=p;i<=u;i++)
		{
			x=d[i]*10;
			if (!e[(x+1)%cmc])
			{
				u2++;
				c[u2][0]=1;
				c[u2][1]=i;
				d[u2]=(x+1)%cmc;
				e[d[u2]]=1;
				if (d[u2]==0)
				{
					af(u2);
					fprintf(out,"\n");
					fclose(in);
					fclose(out);
					return 0;
				}
			}
			if (!e[x%cmc])
			{
				u2++;
				c[u2][0]=0;
				c[u2][1]=i;
				d[u2]=x%cmc;
				e[d[u2]]=1;
				if (d[u2]==0)
				{
					af(u2);
					fprintf(out,"\n");
					fclose(in);
					fclose(out);
					return 0;
				}
			}
			
		}
		p=u+1;
		u=u2;
	}
	return 0;
}