Cod sursa(job #1933248)

Utilizator Mihai_PredaPreda Mihai Dragos Mihai_Preda Data 20 martie 2017 16:27:24
Problema Indep Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.65 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cstring>
#include <cstdio>

using namespace std;

ifstream in("indep.in");

const int valMax = 1000;
const int nMax = 505;
const int base = 1000000000;
const int dim = 120;

struct BigNumber
{
    int lg;
    int c[dim];
    BigNumber(int x = 0)
    {
        lg = 0;
        memset(c, 0, sizeof(c));
        if(x == 0)
            lg++;
        while(x != 0)
        {
            c[lg++] = x % base;
            x /= base;
        }
    }

    void operator +=(const BigNumber &that)
    {
        if(that.lg > lg)
            lg = that.lg;
        int t = 0;
        for(int i = 0; i < lg; ++i)
        {
            if(i < that.lg)
                c[i] += that.c[i];
            c[i] += t;

            t = c[i] / base;
            c[i] %= base;
        }
        if(t)
            c[lg++] = t;
    }
};

int n;
BigNumber dp[valMax+1];
int a[nMax];

int gcd(int a, int b)
{
    static int ret[valMax+1][valMax+1];
    if(ret[a][b] == 0)
        ret[a][b] = ret[b][a] = __gcd(a, b);
    return ret[a][b];
}

void citire()
{
    in >> n;
    for(int i = 1; i <= n; ++i)
        in >> a[i];
}

void rezolvare()
{
    BigNumber one(1);
    for(int i = 1; i <= n; ++i)
    {
        for(int j = 1; j <= valMax; ++j)
            dp[__gcd(a[i], j)] += dp[j];
        dp[a[i]] += one;
    }
    int c;
    freopen("indep.out", "w", stdout);
    printf("%d", dp[1].c[dp[1].lg-1]);
    for(int i = dp[1].lg - 2; i >= 0; --i)
    {
        printf("%.9d", dp[1].c[i]);
    }
}

int main()
{
    citire();
    rezolvare();
    return 0;
}