Cod sursa(job #3292181)

Utilizator BledeaAlexBledea Alexandru BledeaAlex Data 7 aprilie 2025 11:47:53
Problema Pairs Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.55 kb
#include <bits/stdc++.h>

using namespace std;

const long long M_MAX = 1e6 + 5, MAX_PRIME = 1e3 + 10;

void SetInput(string name)
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    (void)!freopen((name + ".in").c_str(), "r", stdin);
    (void)!freopen((name + ".out").c_str(), "w", stdout);
}

int multiples[M_MAX];
bool sieve[M_MAX];
int primeCnt[M_MAX];

void PrecalcPrimeCnt()
{
    sieve[0] = sieve[1] = 1;
    for(int i = 2; i < M_MAX; i++)
        if(sieve[i] == 0)
        {
            for(int j = i; j < M_MAX; j += i)
            {
                sieve[j] = 1;
                primeCnt[j]++;
            }
            sieve[i] = 0;
        }
}

void AddDivisors(int x)
{
    int i;
    for(i = 1; i * i < x; i++)
        if(x % i == 0)
        {
            multiples[i]++;
            multiples[x/i]++;
        }

    if(i * i == x)
        multiples[i]++;
}

inline long long Comb2(const int& n)
{
    return 1LL * n * (n - 1) / 2;
}

void Solve()
{
    int N, M = 0;
    cin >> N;

    for(int i = 1, x; i <= N; i++)
    {
        cin >> x;
        M = max(M, x);
        AddDivisors(x);
    }

    long long sol = Comb2(N);

    for(int i = 2; i <= M; i++)
        if(multiples[i] >= 2)
        {
            if(primeCnt[i] % 2 == 1)
                sol -= Comb2(multiples[i]);
            else
                sol += Comb2(multiples[i]);
        }

    cout << sol << '\n';
}

int main()
{
    SetInput("pairs");

    PrecalcPrimeCnt();
    Solve();

    return 0;
}