Cod sursa(job #1255778)

Utilizator sebinechitasebi nechita sebinechita Data 5 noiembrie 2014 09:27:04
Problema Indep Scor 25
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.43 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
ifstream fin("indep.in");
ofstream fout("indep.out");
#define COST 1002
#define MAX 502
#define baza 100000000
#define L 300

typedef int huge[L];

int a[MAX], n;
huge b[MAX][COST];

huge aux;

void add(huge a, huge b, huge c)
{
    memset(aux, 0, sizeof(aux));
    int t=0, i;
    aux[0]=max(a[0], b[0]);
    for(i=1;i<=aux[0];i++)
    {
        aux[i]=a[i]+b[i]+t;
        if(aux[i]>=baza)
        {
            t=1;
            aux[i]-=baza;
        }
        else
        {
            t=0;
        }
    }
    if(t)
    {
        aux[0]++;
        aux[aux[0]]++;
    }
    memcpy(c, aux, sizeof(aux));
}

void afi(int x, int b)
{
    if(b)
    {
        afi(x/10, b-1);
        fout << x%10;
    }
}

void af(huge a)
{
    if(!a[0])
    {
        fout << "0 ";
        return;
    }
    fout<<a[a[0]];
    for(int i=a[0]-1;i>=1;i--)
    {
        afi(a[i], log10(baza));
    }
    fout << " ";
}

int main()
{
    int i, j;
    fin>>n;
    for(i=1;i<=n;i++)
    {
        fin>>a[i];
    }
    for(i=1;i<=n;i++)
    {
        b[i][a[i]][0]=b[i][a[i]][1]=1;
        for(j=1;j<=COST;j++)
        {
            add(b[i][j], b[i-1][j], b[i][j]);
            add(b[i][__gcd(j, a[i])], b[i-1][j], b[i][__gcd(j, a[i])]);
        }
    }
    af(b[n][1]);

}