Cod sursa(job #3306552)

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

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

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

int n;
vector<int> cnt(VMAX, 0);
vector<int> freq(VMAX, 0);

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";
    }
    
    vector<int> dp(vmax + 1, 0);
    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;
}