Cod sursa(job #2580434)

Utilizator alexradu04Radu Alexandru alexradu04 Data 13 martie 2020 17:04:23
Problema Indep Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.89 kb
#include <cstdio>
#include <regex>

using namespace std;

const int BASE = 1e9;
const int MAXVAL = 1000;
const int MAXN = 100;
struct Huge {
    int nrcif;
    int v[MAXN + 1];
    Huge () {
        nrcif = 1;
        memset (v, 0, sizeof (v));
    }
    Huge operator = (const Huge &other) {
        int i;
        nrcif = other.nrcif;
        for (i = 1; i <= nrcif; i++)
            v[i] = other.v[i];
        return *this;
    }
    Huge operator += (const Huge &other) {
        int r, i;
        nrcif = max (nrcif, other.nrcif);
        r = 0;
        for (i = 1; i <= nrcif; i++) {
            v[i] = v[i] + r + other.v[i];
            r = v[i] / BASE;
            v[i] = v[i] % BASE;
        }
        if (r) {
            nrcif++;
            v[nrcif] = r;
        }
        return *this;
    }
    void afis () {
        int i, p;
        printf ("%d", v[nrcif]);
        for (i = nrcif - 1; i > 0; i--) {
            p = BASE / 10;
            while (p > v[i]) {
                p = p / 10;
                printf ("0");
            }
            printf ("%d", v[i]);
        }
        printf ("\n");
    }
    void reset () {
        int i, p;
        for (i = nrcif; i > 0; i--) {
            v[i]=0;
        }
        nrcif=0;
    }
};
int v[505];
Huge dp[3][1005];
int main(){
    int n, i, j;
    //freopen("indep.in","r",stdin);
    //freopen("indep.out","w",stdout);
    scanf("%d",&n);
    for (i=1; i<=n; i++) {
        scanf("%d",&v[i]);
    }
    Huge unu;
    Huge zero;
    zero.nrcif=0;
    int lin=2;
    unu.nrcif=unu.v[1]=1;
    dp[1][v[1]] = unu;
    for (i=2; i<=n; i++) {
        for (j=1; j<=1000; j++) {
            dp[lin][__gcd (j, v[i])] += dp[3-lin][j];
            dp[lin][j] += dp[3-lin][j];
        }
        dp[lin][v[i]]+=unu;
        lin=3-lin;
        for (j=1; j<=1000; j++) {
            dp[lin][j].reset();
        }
    }
    dp[3-lin][1].afis();
    //printf("%d",dp[n][1]);
    return 0;
}