Cod sursa(job #2709210)

Utilizator PatrickCplusplusPatrick Kristian Ondreovici PatrickCplusplus Data 19 februarie 2021 23:54:55
Problema Indep Scor 15
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.99 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("indep.in");
ofstream fout("indep.out");

const int nmax = 505;
int n, v[nmax], dp[nmax][1005][3];

int dinamica(int index, int cmmdc, int contor){
    if (dp[index][cmmdc][contor] > 0){
        return dp[index][cmmdc][contor];
    }
    if (index == n + 1){
        if (cmmdc == 1 && contor >= 2){
            return 1;
        }
        return 0;
    }
    int ans = dinamica(index + 1, cmmdc, contor);
    if (contor == 0){
        ans = ans + dinamica(index + 1, v[index], contor + 1);
    }
    else if (contor == 1){
        ans = ans + dinamica(index + 1, __gcd(v[index], cmmdc), contor + 1);
    }
    else{
        ans = ans + dinamica(index + 1, __gcd(v[index], cmmdc), 2);
    }
    return dp[index][cmmdc][contor] = ans;
}

int main(){
    fin >> n;
    for (int i = 1; i <= n; ++i){
        fin >> v[i];
    }
    fout << dinamica(1, 1, 0);
    fin.close();
    fout.close();
    return 0;
}