Cod sursa(job #3306551)

Utilizator Costy2345Costi Dimian Costy2345 Data 12 august 2025 01:53:14
Problema Pairs Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.89 kb
#include <bits/stdc++.h>
#define int long long
using namespace std;

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

const int NMAX = 100001;
const int VMAX = 1e6 + 1;

int n;
int v[NMAX];
int cnt[VMAX];
int freq[VMAX];

signed main()
{
    fin >> n;
    int vmax = -1;
    for(int i = 1; i <= n; i++)
    {
        int x;
        fin >> x;
        vmax = max(vmax, x);
        freq[x] = 1;
    }

    for(int i = 1; i <= vmax; i++)
    {
        for(int j = i; j <= vmax; j += i)
        {
            cnt[i] += freq[j];
        }
        // cout << i << ": " << cnt[i] << "\n";
    }
    
    int dp[vmax + 1];
    for(int d = vmax; d >= 1; d--)
    {
        int total = cnt[d] * (cnt[d] - 1) / 2;
        for(int i = 2 * d; i <= vmax; i += d)
        {
            total -= dp[i];
        }
        dp[d] = total;
    }
    fout << dp[1];
    return 0;
}