Problem p02574 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define rep2(x, fr, to) for (int x = (fr); x < (to); x++)
#define rep(x, to) for (int x = 0; x < (to); x++)
#define repr(x, fr, to) for (int x = (fr); x >= (to); x--)
#define all(c) c.begin(), c.end()
#define sz(v) (int)v.size()

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<ll> VL;
const int MD = 1e9 + 7;
void dbg() { cerr << "\\n"; }
template <class F, class... S> void dbg(const F &f, const S &...s) {
  cerr << f << ": ";
  dbg(s...);
}

VI pr;
void eratos(int n) {
  vector<bool> prime(n + 5); // prime.resize(n+5);
  prime[2] = true;
  prime[0] = prime[1] = false;
  for (int i = 3; i <= n; i += 2) {
    prime[i] = true;
    prime[i + 1] = false;
  }
  int limit = (int)sqrt((double)n) + 1;
  for (int i = 3; i <= limit; i += 2)
    if (prime[i]) {
      for (int j = i * i; j <= n; j += i + i)
        prime[j] = false;
    }
  rep(i, n + 1) if (prime[i]) pr.push_back(i);
}
map<ll, int> fctm2(ll x) {
  map<ll, int> fmp;
  for (int i = 0; (ll)pr[i] * pr[i] <= x; i++) {
    for (int pz = pr[i]; x % pz == 0 && x >= pz; x /= pz)
      fmp[pz]++;
  }
  if (x != 1)
    fmp[x]++;
  return fmp;
}
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  VL a(n);
  rep(i, n) cin >> a[i];
  sort(all(a));

  eratos(a.back() + 100);
  int ok = 1;
  map<ll, int> mp;
  rep(i, n) {
    auto uk = fctm2(a[i]);
    for (auto &z : uk) {
      mp[z.first]++;
      if (mp[z.first] > 1) {
        ok = 2;
        break;
      }
    }
    if (ok > 1)
      break;
  }

  if (ok == 1) {
    puts("pairwise coprime");
    return 0;
  }

  ll g = 0;
  rep(i, n) g = gcd(g, a[i]);
  if (g == 1)
    puts("setwise coprime");
  else
    puts("not coprime");
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define rep2(x, fr, to) for (int x = (fr); x < (to); x++)
#define rep(x, to) for (int x = 0; x < (to); x++)
#define repr(x, fr, to) for (int x = (fr); x >= (to); x--)
#define all(c) c.begin(), c.end()
#define sz(v) (int)v.size()

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<ll> VL;
const int MD = 1e9 + 7;
void dbg() { cerr << "\\n"; }
template <class F, class... S> void dbg(const F &f, const S &...s) {
  cerr << f << ": ";
  dbg(s...);
}

VI pr;
void eratos(int n) {
  vector<bool> prime(n + 5); // prime.resize(n+5);
  prime[2] = true;
  prime[0] = prime[1] = false;
  for (int i = 3; i <= n; i += 2) {
    prime[i] = true;
    prime[i + 1] = false;
  }
  int limit = (int)sqrt((double)n) + 1;
  for (int i = 3; i <= limit; i += 2)
    if (prime[i]) {
      for (int j = i * i; j <= n; j += i + i)
        prime[j] = false;
    }
  rep(i, n + 1) if (prime[i]) pr.push_back(i);
}
map<ll, int> fctm2(ll x) {
  map<ll, int> fmp;
  for (int i = 0; (ll)pr[i] * pr[i] <= x; i++) {
    for (int pz = pr[i]; x % pz == 0 && x >= pz; x /= pz)
      fmp[pz]++;
  }
  if (x != 1)
    fmp[x]++;
  return fmp;
}
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  VL a(n);
  rep(i, n) cin >> a[i];
  sort(all(a));

  eratos(a.back());
  int ok = 1;
  map<ll, int> mp;
  rep(i, n) {
    auto uk = fctm2(a[i]);
    for (auto &z : uk) {
      mp[z.first]++;
      if (mp[z.first] > 1) {
        ok = 2;
        goto lpout;
      }
    }
  }
lpout:;

  if (ok == 1) {
    puts("pairwise coprime");
    return 0;
  }

  ll g = a[0];
  rep(i, n) g = gcd(g, a[i]);
  if (g == 1)
    puts("setwise coprime");
  else
    puts("not coprime");
  return 0;
}