Cod sursa(job #1291074)

Utilizator stefanzzzStefan Popa stefanzzz Data 12 decembrie 2014 10:33:03
Problema Indep Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.3 kb
#include <fstream>
#define MAXN 505
#define MAXC 305
#define MAXNUM 1000
using namespace std;

ifstream f("indep.in");
ofstream g("indep.out");

int n, v[MAXN], sum_pd[MAXNUM + 5][MAXC], pd[MAXNUM + 5][MAXC], x, y;

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

void aduna(int v1[], int v2[]);

int main(){
    int i, j, k;

    f >> n;
    for(i = 1; i <= n; i++)
        f >> v[i];

    for(j = 1; j <= MAXNUM; j++)
        pd[j][0] = 1;
    for(i = 1; i <= n; i++){
        x = v[i];
        pd[x][1] = 1;

        for(j = 1; j <= MAXNUM; j++){
            y = gcd(x, j);
            aduna(pd[y], sum_pd[j]);
        }

        for(j = 1; j <= MAXNUM; j++){
            aduna(sum_pd[j], pd[j]);
            for(k = 1; k <= pd[j][0]; k++)
                pd[j][k] = 0;
            pd[j][0] = 1;
            pd[j][1] = 0;
        }
    }

    for(i = sum_pd[1][0]; i >= 1; i--)
        g << sum_pd[1][i];
    f.close();
    g.close();
    return 0;
}

void aduna(int v1[], int v2[]){
    int i, r = 0;
    for(i = 1; i <= v2[0] || r; i++){
        v1[i] += v2[i] + r;
        if(v1[i] > 9){
            v1[i] -= 10;
            r = 1;
        }
        else
            r = 0;
    }

    if(i - 1 > v1[0])
        v1[0] = i - 1;
}