Cod sursa(job #2931667)

Utilizator rapidu36Victor Manz rapidu36 Data 31 octombrie 2022 18:21:56
Problema Indep Scor 95
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.81 kb
#include <fstream>

using namespace std;

const int N = 500;
const int VMAX = 1000;
const int NC = 200;

short int n, dp[2][1+VMAX][NC], unu[NC] = {1, 1};///unu = (1, 1, 0, 0, 0, ....)

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

void copie(short int dest[NC], short int sursa[NC])
{
    for (short int i = 0; i < NC; i++)
    {
        dest[i] = sursa[i];
    }
}

void suma(short int s[NC], short int a[NC], short int b[NC])
{
    short int t = 0, i = 1;
    while (i <= a[0] || i <= b[0] || t > 0)
    {
        int aux = a[i] + b[i] + t;
        if (aux < 10)
        {
            s[i] = aux;
            t = 0;
        }
        else
        {
            s[i] = aux - 10;
            t = 1;
        }
        i++;
    }
    s[0] = i - 1;
}

int cmmdc(short int a, short int b)
{
    while (b != 0)
    {
        int r = a % b;
        a = b;
        b = r;
    }
    return a;
}

void afis(short int v[NC])
{
    for (short int i = v[0]; i >= 1; i--)
    {
        out << v[i];
    }
}

int main()
{
    in >> n;
    short int v_max = 0;
    for (short int i = 1; i <= n; i++)
    {
        short int v_i;
        in >> v_i;
        v_max = max(v_max, v_i);
        if (i == 1)
        {
            copie(dp[1][v_i], unu);
            continue;
        }
        short int i_c = i % 2;
        short int i_a = 1 - i_c;
        for (short int j = 1; j <= v_max; j++)
        {
            copie(dp[i_c][j], dp[i_a][j]);
        }
        for (short int j = 1; j <= v_max; j++)
        {
            short int d = cmmdc(v_i, j);
            suma(dp[i_c][d], dp[i_c][d], dp[i_a][j]);
        }
        suma(dp[i_c][v_i], dp[i_c][v_i], unu);
    }
    ///afisam dp[n % 2][1]
    afis(dp[n%2][1]);
    in.close();
    out.close();
    return 0;
}