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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

bool is_prime[2000000];

int a[2000000];
vector<int> v[2000000];

int main() {
  memset(is_prime, true, sizeof(is_prime));
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i < 2000000; i++) {
    if (!is_prime[i])
      continue;
    v[i].push_back(i);
    for (int j = 2 * i; j < 2000000; j += i) {
      is_prime[j] = false;
      v[j].push_back(i);
    }
  }
  int n;
  cin >> n;
  int g = 0;
  unordered_map<int, int> mp;
  rep(i, n) {
    scanf("%d", &a[i]);
    g = __gcd(g, a[i]);
    for (int u : v[a[i]]) {
      mp[u]++;
    }
  }
  bool ok = true;
  for (auto p : mp) {
    if (p.second != 1)
      ok = false;
  }
  if (ok)
    puts("pairwise coprime");
  else if (g == 1)
    puts("setwise coprime");
  else
    puts("not coprime");
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define ff first
#define ss second
#define it(a, x) for (auto &a : x)
#define ll long long
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vpii vector<pii>
#define vs vector<string>
#define gi greater<int>
#define vc vector<char>
#define vvi vector<vi>
#define vvc vector<vc>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
#define deb(x) cout << #x << "=" << x << endl;
#define in(i, n) for (auto i = 0; i < n; i++)
#define all(x) begin(x), end(x)
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());//random
// shuffler

ll fxp(ll a, ll b) {
  if (b == 0)
    return 1;
  if (b % 2 == 0) {
    ll c = fxp(a, b / 2);
    return (c * c) % mod;
  }
  return (a * fxp(a, b - 1)) % mod;
}

void c_p_c() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
#endif
}

const int MXN = 1e6 + 5;
vector<int> primes(MXN, 1);
vector<int> factor(MXN);

void pre() {
  primes[0] = 0;
  primes[1] = 0;
  for (int i = 2; i * i < MXN; i++) {
    if (primes[i] == 1) {
      factor[i] = i;
      for (int j = i * i; j < MXN; j += i) {
        primes[j] = 0;
        factor[j] = i;
      }
    }
  }
}

void solve() {
  pre();
  int n;
  cin >> n;
  vector<int> a(n);
  in(i, n)cin >> a[i];
  unordered_map<int, int> freq;
  bool first = true;
  for (int i = 0; i < n; i++) {
    int x = a[i];
    while (factor[x] != x) {
      freq[factor[x]]++;
      x /= factor[x];
    }
  }
  for (auto x : freq) {
    if (x.second > 1) {
      first = false;
      break;
    }
  }
  if (first) {
    cout << "pairwise coprime";
    return;
  }

  int gcd = a[0];
  for (int i = 1; i < n; i++)
    gcd = __gcd(gcd, a[i]);

  if (gcd == 1) {
    cout << "setwise coprime";
    return;
  }

  cout << "not coprime";
}

int main(int argc, char const *argv[]) {
  c_p_c();
  solve();
  return 0;
}