Cod sursa(job #2972635)

Utilizator 100pCiornei Stefan 100p Data 29 ianuarie 2023 21:25:52
Problema Pairs Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.47 kb
#include <bits/stdc++.h>

#define MAX 100000
#define VALMAX 1000000

#define FILES freopen("pairs.in","r",stdin);\
              freopen("pairs.out","w",stdout);

using namespace std;

int n, a, v[MAX + 5], ciur[VALMAX + 5], fr[VALMAX + 5], dp[VALMAX + 5];
bool ok[VALMAX + 5];

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    FILES
    std::cin >> n;
    long long ans = 0;
    for(int i = 1; i <= n; ++i)
    {
        std::cin >> v[i];
        fr[v[i]]++;
    }

    for(int i = 4; i <= VALMAX; i += 2)
        ciur[i] = 1, dp[i]++;
    dp[2] = 1;
    long long next = 4;
    while(next <= VALMAX)
    {
        ok[next] = 1;
        next *= 2;
    }
    for(int i = 3; i <= VALMAX; i += 2)
    {
        if(!ciur[i])
        {
            long long next = 1LL * i * i;
            while(next <= VALMAX)
            {
                ok[next] = 1;
                next *= i;
            }
            dp[i] = 1;
            for(int j = i + i; j <= VALMAX; j += i)
                dp[j]++, ciur[j] = 1;
        }
    }
    for(int i = 2; i <= VALMAX; ++i)
    {
        if(!ok[i])
        {
            int cat = 0;

            for(int j = i; j <= VALMAX; j += i)
                cat += fr[j];

            if(dp[i] % 2)
                ans -= 1LL * cat * (cat - 1) / 2;
            else
                ans += 1LL * cat * (cat - 1) / 2;
        }
    }
    std::cout << ans + 1LL * n * (n - 1) / 2;
}