Cod sursa(job #126478)

Utilizator gicagica popescu gica Data 22 ianuarie 2008 12:29:58
Problema Indep Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.15 kb
#include <stdio.h>


int n,a[600],b[2][1100][400],m,c[400],cm,x;

int cmmdc( int a, int b)
{
	int r;
	if (a==0)
	   return b;
	if (b==0)
	   return a;
	r=a%b;
	while (r!=0)
	{
		a=b;
		b=r;
	r=a%b;
	}
	return b;
}

void add( int a[1000],int b[1000])
{
	int i,t=0;
	//for (i=1; i<=c[0]; ++i) c[i]=0;
	i=0;
	//c[0]=0;
	while (t>0 || i<a[0] || i<b[0])
	{
		++i;
		a[i]=(t+b[i]+a[i])%10;
		t=a[i]/10;
		if (i>a[0]) a[0]=i;
	}
}

int main()
{
	freopen("indep.in","r",stdin);
	freopen("indep.out","w",stdout);
	int i,j,p = 1;
	scanf("%d",&n);
	for (i=1; i<=n; ++i) 
	{
		scanf("%d",&a[i]);
		if (m<a[i]) m=a[i];
	}
	b[p^1][a[1]][0]=1;
	b[p^1][a[1]][1]=1;
	for (i=2; i<=n; i++) 
	{
		for (j=0; j<=m; j++)
		{
			for (int k=1; k<=b[p][j][0]; k++)
			{
			b[p][j][k] = 0;
			}
			b[p][j][0] = 0;
		}
        b[p][a[i]][0]=1;
		b[p][a[i]][1]=1;

		for (j=0; j<=m; j++) 
		{
			cm=cmmdc(j,a[i]);
			add(b[p][j],b[p^1][j]);
			//b[i][j]+=b[i-1][j];
			add(b[p][cm],b[p^1][j]);
			//b[i][cm]+=b[i-1][j];
		}
	p ^= 1;
	}
	for (i=b[p^1][1][0]; i>0; --i) printf("%d",b[p^1][1][i]);
	
	//printf("%d",b[n][1]);
	return 0;
}