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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstring> //memset(dp,0,sizeof(dp))
#include <functional>
#include <iomanip>
#include <iostream>
#include <locale>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define fi first
#define se second
#define pb push_back
#define ALL(a) (a).begin(), (a).end()
using namespace std;
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
#define fout(num) cout << fixed << setprecision(20) << (num) << endl
// s[i]=tolower(s[i]); islower(s[i]); cout << tolower(s[i])はバグ
// vector<vector<ll>> dp(n,vector<ll>(n))
// exist x map o setconst ll MOD = 1e9+7;return fac[n]*(finv[k]*finv[n-k] % MOD)
// % MOD; 2-dim:vector<vector<Type>> vv(n, vector<Type>(m, d));
// 3-dim:vector<vector<vector<Type>>> vvv(n, vector<vector<Type>>(m,
// vector<Type>(l, d)));

int gcd(int a, int b) {
  while (b != 0) {
    a %= b;
    if (a == 0)
      return b;
    b %= a;
  }
  return a;
}
signed main() {
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> mp(1001001, 0);
  bool ok = 1;
  int G;
  rep(i, n) {
    cin >> a[i];
    int mx = a[i];
    if (i == 0)
      G = a[i];
    else
      G = gcd(G, a[i]);
    if (ok) {
      for (int j = 2; j * j <= mx; j++) {
        if (mx % j == 0) {
          if (mp[j] > 0)
            ok = 0;
          mp[j]++;
          while (mx % j == 0) {
            mx /= j;
          }
        }
      }
      if (mx != 1) {
        if (mp[mx] > 0)
          ok = 0;
        mp[mx]++;
      }
    }
  }
  if (ok)
    cout << "pairwise coprime" << endl;
  else if (G == 1)
    cout << "setwise coprime" << endl;
  else
    cout << "not coprime" << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstring> //memset(dp,0,sizeof(dp))
#include <functional>
#include <iomanip>
#include <iostream>
#include <locale>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define fi first
#define se second
#define pb push_back
#define ALL(a) (a).begin(), (a).end()
using namespace std;
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
#define fout(num) cout << fixed << setprecision(20) << (num) << endl
// s[i]=tolower(s[i]); islower(s[i]); cout << tolower(s[i])はバグ
// vector<vector<ll>> dp(n,vector<ll>(n))
// exist x map o setconst ll MOD = 1e9+7;return fac[n]*(finv[k]*finv[n-k] % MOD)
// % MOD; 2-dim:vector<vector<Type>> vv(n, vector<Type>(m, d));
// 3-dim:vector<vector<vector<Type>>> vvv(n, vector<vector<Type>>(m,
// vector<Type>(l, d)));

int gcd(int a, int b) {
  while (b != 0) {
    a %= b;
    if (a == 0)
      return b;
    b %= a;
  }
  return a;
}
signed main() {
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> mp(100100, 0);
  bool ok = 1;
  int G;
  rep(i, n) {
    cin >> a[i];
    if (i == 0)
      G = a[i];
    else
      G = gcd(G, a[i]);
    if (ok) {
      if (a[i] != 1 && mp[a[i]] > 0)
        ok = 0;
      mp[a[i]]++;
      int mx = a[i];
      for (int j = 2; j * j <= mx; j++) {
        if (a[i] % j == 0) {
          if (mp[j] > 0)
            ok = 0;
          mp[j]++;
          while (a[i] % j == 0) {
            a[i] /= j;
          }
        }
      }
    }
  }
  if (ok)
    cout << "pairwise coprime" << endl;
  else if (G == 1)
    cout << "setwise coprime" << endl;
  else
    cout << "not coprime" << endl;
}