Cod sursa(job #1342878)

Utilizator AlexandruValeanuAlexandru Valeanu AlexandruValeanu Data 14 februarie 2015 17:03:58
Problema Indep Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.7 kb
#include <bits/stdc++.h>

using namespace std;

const int Vmax = 1000;
const int Lmax = 200 + 1;

typedef int BigInteger[Lmax];

BigInteger dp[2][Vmax + 1];
int N;

void init(BigInteger& B, int x)
{
    memset(B, sizeof(B), 0);

    do
    {
        B[ ++B[0] ] = x % 10;
        x /= 10;

    } while ( x );
}

void copiaza(BigInteger&A, BigInteger& B)
{
    for ( int i = 0; i < Lmax; ++i )
        A[i] = B[i];
}

void add(BigInteger& A, int x)
{
    int T = 0;

    for ( int i = 1; i <= A[0]; ++i )
    {
        A[i] += x + T;
        T = A[i] / 10;
        A[i] %= 10;
        x = 0;
    }

    while ( T )
    {
        A[ ++A[0] ] = T % 10;
        T /= 10;
    }
}

void add(BigInteger& A, BigInteger& B)
{
    int T = 0;

    A[0] = max(A[0], B[0]);

    for ( int i = 1; i <= A[0]; ++i )
    {
        A[i] += T + B[i];
        T = A[i] / 10;
        A[i] %= 10;
    }

    while ( T )
    {
        A[ ++A[0] ] = T % 10;
        T /= 10;
    }
}

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

    cin >> N;

    for ( int i = 0; i <= 1; ++i )
        for ( int j = 1; j <= Vmax; ++j )
            init(dp[i][j], 0);

    for ( int i = 1; i <= N; ++i )
    {
        int value;
        int cr = i & 1;
        int pr = cr ^ 1;

        cin >> value;

        add(dp[cr][value], 1);

        for ( int j = 1; j <= Vmax; ++j )
        {
            int GCD = __gcd(j, value);

            add(dp[cr][GCD], dp[pr][j]);
        }

        for ( int j = 1; j <= Vmax; ++j )
            copiaza(dp[pr][j], dp[cr][j]);
    }

    printf("%d", dp[N & 1][1][ dp[N & 1][1][0] ]);

    for ( int i = dp[N & 1][1][0] - 1; i >= 1; i-- )
        printf("%d", dp[N & 1][1][i]);

    return 0;
}