Cod sursa(job #2954495)

Utilizator LucaMuresanMuresan Luca Valentin LucaMuresan Data 14 decembrie 2022 17:19:15
Problema Indep Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.34 kb
#include <bits/stdc++.h>

using namespace std;

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

int a[501];

struct BigInt
{
    int a[501];

    void operator = (int x)
    {
        a[0] = 0;
        while (x)
            a[++a[0]] = x % 10, x /= 10;
    }

    void operator = (BigInt b)
    {
        for (int i=0; i<=b.a[0]; i++)
            a[i] = b.a[i];
    }

    void operator += (BigInt b)
    {
        int t = 0;
        a[0] = max(a[0], b.a[0]);

        for (int i=1; i<=a[0]; i++)
        {
            t += a[i] + b.a[i];
            a[i] = t % 10;
            t /= 10;
        }

        if (t)
            a[++a[0]] = t;
    }

    void print()
    {
        for (int i=a[0]; i>0; i--)
            out << a[i];
    }
};

BigInt dp[2][1001]; /// dp[n][k] - nr de subsiruri pt care gcd(a[i1], a[i2], ..., a[im]) == k, m <= n

int main()
{
    int n;
    in >> n;

    for (int i=1; i<=n; i++)
        in >> a[i];

    BigInt UNU;
    UNU = 1;

    for (int i=1; i<=n; i++)
    {
        bool c = i & 1, p = c ^ 1;

        for (int j=1; j<1001; j++)
            dp[c][j] = dp[p][j];

        for (int j=1; j<1001; j++)
        {
            dp[c][__gcd(a[i], j)] += dp[p][j];
        }

        dp[c][a[i]] += UNU;
    }

    dp[n & 1][1].print();

    return 0;
}