Cod sursa(job #1198898)

Utilizator gabrielinelusGabriel-Robert Inelus gabrielinelus Data 17 iunie 2014 16:38:41
Problema Indep Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.64 kb
#include <iostream>
#include <fstream>
#include <cstring>
#define BASE 10

using namespace std;
ifstream fin("indep.in");
ofstream fout("indep.out");
int v[505],N;

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

struct nr_mare{
    int nc,cif[1005];
    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;
    }
    friend ostream& operator << (ostream &out, nr_mare n)
    {
        while(n.nc--) out << n.cif[n.nc];
        return out;
    }
};
nr_mare DP[1005];

int main()
{
    fin.sync_with_stdio(false);
    fin >> N;
    for(int i = 1; i <= N; ++i)
        fin >> v[i];
    for(int i = 1; i <= N; ++i){
        for(int j = 1; j <= 1000; ++j)
        {
            int x = cmmdc(j,v[i]);
            DP[x] = DP[x] + DP[j];
        }
        DP[v[i]] = DP[v[i]] + 1;
    }
    fout << DP[1];

    return 0;
}