Cod sursa(job #3306553)

Utilizator Costy2345Costi Dimian Costy2345 Data 12 august 2025 02:14:17
Problema Pairs Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.05 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);
vector<int> dp(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]++;
    }

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

    for(int d = vmax; d >= 1; d--)
    {
        int total = dp[d];
        for(int i = 2 * d; i <= vmax; i += d)
        {
            total -= dp[i];
        }
        dp[d] = total;
    }
    fout << dp[1];
    return 0;
}