Cod sursa(job #2580643)

Utilizator MatteoalexandruMatteo Verzotti Matteoalexandru Data 13 martie 2020 20:29:50
Problema Indep Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.68 kb
/*
                `-/oo+/-   ``
              .oyhhhhhhyo.`od
             +hhhhyyoooos. h/
            +hhyso++oosy- /s
           .yoooossyyo:``-y`
            ..----.` ``.-/+:.`
                   `````..-::/.
                  `..```.-::///`
                 `-.....--::::/:
                `.......--::////:
               `...`....---:::://:
             `......``..--:::::///:`
            `---.......--:::::////+/`
            ----------::::::/::///++:
            ----:---:::::///////////:`
            .----::::::////////////:-`
            `----::::::::::/::::::::-
             `.-----:::::::::::::::-
               ...----:::::::::/:-`
                 `.---::/+osss+:`
                   ``.:://///-.
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <cmath>

using namespace std;

const int INF = 2e9;
const int N = 500;
const int MAX = 1000;
const int DIGITS = 1000;

int v[5 + N];

int gcd(int a, int b) {
    if(b == 0)
        return a;

    return gcd(b, a % b);
}

class HugeN {
    private:
        int v[5 + DIGITS];

    public:
        HugeN() {
            memset(v, 0, sizeof(v));
            v[0] = 1;
        }

        HugeN SetValue(int value){
            int i;

            for(i = 1; value > 0; i++, value /= 10)
                v[i] = value % 10;

            v[0] = i - 1;
        }

        HugeN operator += (const HugeN B) {
            int i, t(0);

            for(i = 1; i <= v[0] || i <= B.v[0] || t; i++){
                v[i] = v[i] + B.v[i] + t;
                t = v[i] / 10;
                v[i] = v[i] % 10;
            }

            v[0] = i - 1;
        }

        void Reset() {
            memset(v, 0, sizeof(v));
        }

        void Print() {
            int i = v[0];

            while(i > 0){
                printf("%d", v[i]);
                i--;
            }
        }
};
HugeN dp[2][5 + MAX];
HugeN One;

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

    int n, lin;
    scanf("%d", &n);

    for(int i = 1; i <= n; i++)
        scanf("%d", &v[i]);

    One.SetValue(1);
    lin = 0;
    dp[lin][v[1]] += One;
    for(int i = 2; i <= n; i++) {
        lin = 1 - lin;
        for(int j = 1; j <= MAX; j++)
            dp[lin][j].Reset();

        for(int j = 1; j <= MAX; j++) {
            dp[lin][j] += dp[1 - lin][j];
            dp[lin][gcd(v[i], j)] += dp[1 - lin][j];
        }

        dp[lin][v[i]] += One;
    }

    dp[lin][1].Print();
    return 0;
}