Cod sursa(job #2766673)

Utilizator Alex_tz307Lorintz Alexandru Alex_tz307 Data 2 august 2021 19:41:40
Problema Pairs Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.9 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("pairs.in");
ofstream fout("pairs.out");

const int MAXV = 1e6;
bitset<1 + MAXV> ap, is_bad;
int cnt_fact[1 + MAXV];

int main() {
  int n;
  fin >> n;
  int mx = 0;
  for (int i = 0; i < n; ++i) {
    int x;
    fin >> x;
    ap[x] = true;
    if (x > mx)
      mx = x;
  }
  int64_t ans = 0;
  for (int x = 2; x <= mx; ++x)
    if (!is_bad[x]) {
      int64_t cnt = 0;
      bool is_prime = cnt_fact[x] == 0;
      for (int y = x; y <= mx; y += x) {
        cnt += ap[y];
        if (!is_prime)
          continue;
        ++cnt_fact[y];
        if (y % (x * x) == 0)
          is_bad[y] = true;
      }
      cnt = (cnt * (cnt  - 1)) >> 1;
      if (cnt_fact[x] & 1)
        ans += cnt;
      else ans -= cnt;
    }
  fout << (int64_t)n * (n - 1) / 2 - ans << '\n';
  fin.close();
  fout.close();
  return 0;
}