Cod sursa(job #1198910)

Utilizator gabrielinelusGabriel-Robert Inelus gabrielinelus Data 17 iunie 2014 16:49:41
Problema Indep Scor 85
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.57 kb
#include <cstring>
#include <cstdio>
#include <algorithm>
#define BASE 1000000000

using namespace std;

int X,N;

int cmmdc(int a,int b)
{
    if(!b)return a;
    return cmmdc(b,a%b);
}

struct nr_mare{
    int nc,cif[50];
    nr_mare()
    {
        nc = 0;
        memset(cif,0,sizeof(cif));
    }
    nr_mare operator=(int b)
    {
        while(b)
        {
            cif[nc++] = b%BASE;
            b/=BASE;
        }
        return *this;
    }
    friend nr_mare operator+(nr_mare a,nr_mare b)
    {
        nr_mare c;
        c.nc = max(a.nc,b.nc);
        for(int i = a.nc; i <= c.nc; ++i)a.cif[i] = 0;
        for(int i = b.nc; i <= c.nc; ++i)b.cif[i] = 0;
        int t = 0;
        for(int i = 0; i < c.nc; ++i){
            c.cif[i] = (a.cif[i] + b.cif[i] + t) % BASE;
            t = (t + a.cif[i] + b.cif[i]) / BASE;
        }
        if(t)
            c.cif[c.nc++] = 1;
        return c;
    }
    friend nr_mare operator+(nr_mare a,int b)
    {
        nr_mare B;
        B = b;
        B = a + B;
        return B;
    }
};
nr_mare DP[1005];

int main()
{
    freopen("indep.in","r",stdin);
    freopen("indep.out","w",stdout);

    scanf("%d",&N);
    for(int i = 1; i <= N; ++i){
        scanf("%d",&X);
        for(int j = 1; j <= 1000; ++j)
        {
            int CMM = cmmdc(j,X);
            DP[CMM] = DP[CMM] + DP[j];
        }
        DP[X] = DP[X] + 1;
    }
    printf("%d",DP[1].cif[DP[1].nc-1]);
    for(int i = DP[1].nc - 2; i >= 0; --i)
        printf("%.9d",DP[1].cif[i]);

    return 0;
}