Problem p02574 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
#define rep(i, n) for (lint(i) = 0; (i) < (n); (i)++)
#define repp(i, m, n) for (lint(i) = (m); (i) < (n); (i)++)
#define repm(i, n) for (lint(i) = (n - 1); (i) >= 0; (i)--)
#define INF (1ll << 60)
#define all(x) (x).begin(), (x).end()
const lint MOD = 1000000007;
const lint MAX = 1000000;
using Graph = vector<vector<lint>>;
typedef pair<lint, lint> P;
typedef map<lint, lint> M;
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)

lint fac[MAX], finv[MAX], inv[MAX];

void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (lint i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

long long COM(lint n, lint k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

lint primary(lint num) {
  if (num < 2)
    return 0;
  else if (num == 2)
    return 1;
  else if (num % 2 == 0)
    return 0;

  double sqrtNum = sqrt(num);
  for (int i = 3; i <= sqrtNum; i += 2) {
    if (num % i == 0) {
      return 0;
    }
  }

  return 1;
}
long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}
lint lcm(lint a, lint b) { return a / __gcd(a, b) * b; }
lint gcd(lint a, lint b) { return __gcd(a, b); }

std::vector<int> sieve(int n) {
  std::vector<int> res(n);
  std::iota(res.begin(), res.end(), 0);
  for (int i = 2; i * i < n; ++i) {
    if (res[i] < i)
      continue;
    for (int j = i * i; j < n; j += i)
      if (res[j] == j)
        res[j] = i;
  }
  return res;
}
std::vector<int> factor(int n, const std::vector<int> &min_factor) {
  // min_factor は sieve() で得られたものとする
  std::vector<int> res;
  while (n > 1) {
    res.push_back(min_factor[n]);
    n /= min_factor[n];
    // 割った後の値についても素因数を知っているので順次求まる
  }
  return res;
}
int main() {
  lint n;
  cin >> n;
  lint a[n];
  rep(i, n) cin >> a[i];
  lint gc = a[0];
  rep(i, n - 1) gc = gcd(gc, a[i + 1]);
  vector<int> p;
  p = sieve(1000005);
  vector<int> x;
  x = factor(n, p);
  M m;
  rep(i, n) {
    x = factor(a[i], p);
    rep(i, x.size() - 1) {
      if (x[i] == x[i + 1])
        x[i] = 0;
    }
    rep(i, x.size()) m[x[i]]++;
  }
  bool f = true;
  for (auto g : m) {
    if (g.first == 0)
      continue;
    if (g.second > 1) {
      f = false;
      break;
    }
  }
  if (f)
    cout << "pairwise coprime" << endl;
  else if (gc == 1)
    cout << "setwise coprime" << endl;
  else
    cout << "not coprime" << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
#define rep(i, n) for (lint(i) = 0; (i) < (n); (i)++)
#define repp(i, m, n) for (lint(i) = (m); (i) < (n); (i)++)
#define repm(i, n) for (lint(i) = (n - 1); (i) >= 0; (i)--)
#define INF (1ll << 60)
#define all(x) (x).begin(), (x).end()
const lint MOD = 1000000007;
const lint MAX = 1000000;
using Graph = vector<vector<lint>>;
typedef pair<lint, lint> P;
typedef map<lint, lint> M;
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)

lint fac[MAX], finv[MAX], inv[MAX];

void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (lint i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

long long COM(lint n, lint k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

lint primary(lint num) {
  if (num < 2)
    return 0;
  else if (num == 2)
    return 1;
  else if (num % 2 == 0)
    return 0;

  double sqrtNum = sqrt(num);
  for (int i = 3; i <= sqrtNum; i += 2) {
    if (num % i == 0) {
      return 0;
    }
  }

  return 1;
}
long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}
lint lcm(lint a, lint b) { return a / __gcd(a, b) * b; }
lint gcd(lint a, lint b) { return __gcd(a, b); }

std::vector<int> sieve(int n) {
  std::vector<int> res(n);
  std::iota(res.begin(), res.end(), 0);
  for (int i = 2; i * i < n; ++i) {
    if (res[i] < i)
      continue;
    for (int j = i * i; j < n; j += i)
      if (res[j] == j)
        res[j] = i;
  }
  return res;
}
std::vector<int> factor(int n, const std::vector<int> &min_factor) {
  // min_factor は sieve() で得られたものとする
  std::vector<int> res;
  while (n > 1) {
    res.push_back(min_factor[n]);
    n /= min_factor[n];
    // 割った後の値についても素因数を知っているので順次求まる
  }
  return res;
}
int main() {
  lint n;
  cin >> n;
  lint a[n];
  rep(i, n) cin >> a[i];
  lint gc = a[0];
  rep(i, n - 1) gc = gcd(gc, a[i + 1]);
  vector<int> p;
  p = sieve(1000005);
  vector<int> x;
  x = factor(n, p);
  M m;
  rep(i, n) {
    if (a[i] == 1)
      continue;
    x = factor(a[i], p);
    rep(i, x.size() - 1) {
      if (x[i] == x[i + 1])
        x[i] = 0;
    }
    rep(i, x.size()) m[x[i]]++;
  }
  bool f = true;
  for (auto g : m) {
    if (g.first == 0)
      continue;
    if (g.second > 1) {
      f = false;
      break;
    }
  }
  if (f)
    cout << "pairwise coprime" << endl;
  else if (gc == 1)
    cout << "setwise coprime" << endl;
  else
    cout << "not coprime" << endl;
}