Cod sursa(job #2737504)

Utilizator MocalinnoMoca Andrei Catalin Mocalinno Data 4 aprilie 2021 20:08:28
Problema Pairs Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.72 kb
#include <bits/stdc++.h>
namespace FastRead {
    const int Dim(1e6);
    char buff[Dim];
    int pos, len;
    inline char nc() {
        if (pos == len) {
            pos = 0;
            len = fread(buff, 1, Dim, stdin);
            if (!len) return EOF;
        }
        return buff[pos++];
    }
    template<class T> inline void Read(T& x) {
        char ch;
        int sgn(1);
        while (!isdigit(ch = nc()))
            if (ch == '-')
                sgn = -1;
        x = ch - '0';
        while (isdigit(ch = nc()))
            x = x * 10 + (ch - '0');
        x *= sgn;
    }
}
using namespace FastRead;
using namespace std;
void DAU(const string& task = "") {
    if (!task.empty())
        freopen((task + ".in").c_str(), "r", stdin),
        freopen((task + ".out").c_str(), "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
}
void PLEC() {
    exit(0);
}
using i64 = long long;
const int N(1000005);
int n, x, maxi, nrf[N];
bool freq[N], no[N], prim;
i64 res, curr;
signed main() {
    DAU("pairs");
    Read(n);
    for (int i = 1; i <= n; ++i) {
        Read(x);
        freq[x] = 1;
        maxi = max(maxi, x);
    }
    for (int i = 2; i <= maxi; ++i)
        if (!no[i]) {
            curr = 0;
            prim = (!nrf[i]);
            for (int j = i; j <= maxi; j += i) {
                curr += freq[j];
                nrf[j] += prim;
                if (j % (i * i) == 0)
                    no[j] = true;
            }
            curr = curr * (curr - 1) / 2;
            if (nrf[i] & 1)
                res += curr;
            else res -= curr;
        }
    res = 1LL * n * (n - 1) / 2 - res;
    cout << res;
    PLEC();
}