Cod sursa(job #299687)

Utilizator DraStiKDragos Oprica DraStiK Data 6 aprilie 2009 22:16:42
Problema Indep Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.24 kb
#include <algorithm>
#define DIM 505
using namespace std;
int a[DIM],b[2][2*DIM][2*DIM];
int n,nmax;
void read ()
{
	int i;
	scanf ("%d",&n);
    for (i=1; i<=n; ++i)
    {
        scanf ("%d",&a[i]);
        if (a[i]>nmax)
            nmax=a[i];
    }
}
int cmmdc (int a,int b)
{
    int r;
    do
    {
        r=a%b;
        a=b;
        b=r;
    }
    while (r);
    return a;
}
void add (int a[2*DIM],int b[2*DIM])
{
    int i,t=0;
	for (i=1; i<=a[0] || i<=b[0] || t; ++i, t/=10)
		a[i]=(t+=a[i]+b[i])%10;
    a[0]=i-1;
}
void solve ()
{
    int i,j,i1=0,i2=1,nr;
    for (i=1; i<=n; ++i)
    {
        b[i2][a[i]][0]=b[i2][a[i]][1]=1;
        for (j=1; j<=nmax; ++j)
			if (b[i1][j][0])
            {
                nr=cmmdc (a[i],j);
                add (b[i2][nr],b[i1][j]);
                add (b[i2][j],b[i1][j]);
            }
		i1^=i2^=i1^=i2;
		for (j=1; j<=nmax; ++j)
            memset (b[i2][j],0,4*(b[i2][j][0]+1));
    }
    if (!b[i1][1][0])
        printf ("0");
    else
        for (i=b[i1][1][0]; i; --i)
            printf ("%d",b[i1][1][i]);    
}
int main ()
{
    freopen ("indep.in","r",stdin);
    freopen ("indep.out","w",stdout);
    read ();
    solve ();
    return 0;
}