Cod sursa(job #2972640)

Utilizator 100pCiornei Stefan 100p Data 29 ianuarie 2023 21:35:19
Problema Pairs Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.37 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]++;
        if(i % 4 == 0)
            ok[i] = 1;
    }
    dp[2] = 1;
    for(int i = 3; i <= VALMAX; i += 2)
    {
        if(!ciur[i])
        {

            dp[i] = 1;
            for(int j = i + i; j <= VALMAX; j += i)
            {
                dp[j]++, ciur[j] = 1;
                if(j % (i * i) == 0 && j >= i * i)
                    ok[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;
}