Cod sursa(job #1097311)

Utilizator armandpredaPreda Armand armandpreda Data 3 februarie 2014 12:09:07
Problema Multiplu Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <cstdio>

using namespace std;

int viz[2000001];
struct queue
{
	int r,pred;
	char c;
}q[2000005],temp;
int rez[100];
int main()
{
    freopen("multiplu.in","r",stdin);
    freopen("multiplu.out","w",stdout);
    int a,b,m,p=1,u=1,r,nr=0,i;
    scanf("%d%d",&a,&b);
    m=a*b;
    while(a!=b)
		if(a>b)
			a=a-b;
		else
			b=b-a;
	m=m/a;
	q[p].c='1';
	q[p].r=1;
	q[p].pred=0;
	while(p<=u)
	{
		r=(q[p].r*10+0)%m;
		if(viz[r]==0)
		{
			temp.c='0';
			temp.r=r;
			temp.pred=p;
			viz[r]=1;
			q[++u]=temp;
		}
		if(r==0)
			break;
		r=(q[p].r*10+1)%m;
		if(viz[r]==0)
		{
			temp.c='1';
			temp.r=r;
			temp.pred=p;
			viz[r]=1;
			q[++u]=temp;
		}
		if(r==0)
			break;
		p++;
	}
	p=u;
	while(q[p].pred!=0)
	{
		nr++;
		rez[nr]=q[p].c-48;
		p=q[p].pred;
	}
	printf("1");
	for(i=nr;i>=1;--i)
		printf("%d",rez[i]);
    return 0;
}