Cod sursa(job #2118318)

Utilizator DruffbaumPopescu Vlad Druffbaum Data 30 ianuarie 2018 14:51:00
Problema Pairs Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1 kb
#include <cstdio>

const int MAXN = 1e6;

int cnt[MAXN + 1]; 
bool pr[MAXN + 1], ok[MAXN + 1], a[MAXN + 1];

int main() {
  int n, x, max;
  long long ans;
  FILE *f = fopen("pairs.in", "r");
  fscanf(f, "%d", &n);
  max = -1;
  for (int i = 0; i < n; ++i) {
    fscanf(f, "%d", &x);
    a[x] = 1;
    if (max < x) {
      max = x;
    }
  }
  fclose(f);
  for (int i = 2; i <= max; ++i) {
    if (!pr[i]) {
      for (int j = i; j <= max; j += i) {
        pr[j] = 1;
        ++cnt[j];
        if (!(j % (i * i))) {
          ok[j] = 1;
        }
      }
    }
  }
  ans = 0;
  for (int i = 2; i <= max; ++i) {
    if (!ok[i]) {
      x = 0;
      for (int j = i; j <= max; j += i) {
        x += a[j];
      }
      if (cnt[i] & 1) {
        ans += ((1LL * x * (x - 1)) >> 1);
      } else {
        ans -= ((1LL * x * (x - 1)) >> 1);
      }
    }
  }
  f = fopen("pairs.out", "w");
  fprintf(f, "%lld\n", ((1LL * n * (n - 1)) >> 1) - ans);
  fclose(f);
  return 0;
}