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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define dup(x, y) (((x) + (y)-1) / (y))
#define ALL(x) (x).begin(), (x).end()
typedef long long ll;
typedef pair<int, int> pii;
const double EPS = 1e-10;
const int INF = 1e9;
const ll LINF = 1e15;
const int MOD = 1e9 + 7;
const double PI = acos(-1);
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
typedef pair<ll, ll> pll;

bool is_prime(int N) {
  if (N == 1)
    return false;
  for (int i = 2; i * i <= N; ++i) {
    if (N % i == 0)
      return false;
  }
  return true;
}

vector<int> prime_factorize(long long N) {
  vector<int> res;
  for (long long a = 2; a * a <= N; ++a) {
    if (N % a != 0)
      continue;
    long long ex = 0; // 指数

    // 割れる限り割り続ける
    while (N % a == 0) {
      ++ex;
      N /= a;
    }

    // その結果を push
    res.push_back(a);
  }

  // 最後に残った数について
  if (N != 1)
    res.push_back(N);
  return res;
}

int main() {
  int N;
  cin >> N;
  vector<ll> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  bool flag1 = true;
  bool flag2 = true;

  map<ll, ll> mp;
  for (int i = 0; i < N; i++) {
    if (is_prime(A[i])) {
      if (A[i] != 1)
        mp[A[i]]++;
    } else {
      vector<int> res = prime_factorize(A[i]);
      for (auto it : res) {
        mp[it]++;
      }
    }
  }

  for (auto it : mp) {
    if (it.second == N)
      flag1 = false;
    if (it.second > 1)
      flag2 = false;
  }

  // cout << g << endl;
  if (flag2) {
    cout << "pairwise coprime" << endl;
  } else if (flag1) {
    cout << "setwise coprime" << endl;
  } else {
    cout << "not coprime" << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define dup(x, y) (((x) + (y)-1) / (y))
#define ALL(x) (x).begin(), (x).end()
typedef long long ll;
typedef pair<int, int> pii;
const double EPS = 1e-10;
const int INF = 1e9;
const ll LINF = 1e15;
const int MOD = 1e9 + 7;
const double PI = acos(-1);
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
typedef pair<ll, ll> pll;

std::vector<ll> Eratosthenes(const ll N) {
  std::vector<bool> is_prime(N + 1);
  for (int i = 0; i <= N; i++) {
    is_prime[i] = true;
  }
  std::vector<ll> P;
  for (int i = 2; i <= N; i++) {
    if (is_prime[i]) {
      for (int j = 2 * i; j <= N; j += i) {
        is_prime[j] = false;
      }
      P.emplace_back(i);
    }
  }
  return P;
}

vector<long long> enum_divisors(long long N) {
  vector<long long> res;
  for (long long i = 1; i * i <= N; ++i) {
    if (N % i == 0) {
      res.push_back(i);
      // 重複しないならば i の相方である N/i も push
      if (N / i != i)
        res.push_back(N / i);
    }
  }
  // 小さい順に並び替える
  sort(res.begin(), res.end());
  return res;
}

vector<pair<long long, long long>> prime_factorize(long long N) {
  vector<pair<long long, long long>> res;
  for (long long a = 2; a * a <= N; ++a) {
    if (N % a != 0)
      continue;
    long long ex = 0; // 指数

    // 割れる限り割り続ける
    while (N % a == 0) {
      ++ex;
      N /= a;
    }

    // その結果を push
    res.push_back({a, ex});
  }

  // 最後に残った数について
  if (N != 1)
    res.push_back({N, 1});
  return res;
}

int main() {
  int N;
  cin >> N;
  vector<ll> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  vector<ll> p = Eratosthenes(1000006);
  set<ll> st;
  bool flag1 = false;
  bool flag2 = false;
  map<ll, ll> mp;
  for (int i = 0; i < N; i++) {
    if (p[A[i]])
      flag2 = true;

    vector<pll> res = prime_factorize(A[i]);
    for (auto it : res) {
      mp[it.first]++;
    }
  }

  for (auto it : mp) {
    // cout << it.first << " " << it.second << endl;
    if (it.second == N)
      flag1 = true;
  }
  // cout << flag1 << " " << flag2 << endl;
  if (!flag1 & flag2) {
    cout << "pairwise coprime" << endl;
  } else if (!flag1) {
    cout << "setwise coprime" << endl;
  } else {
    cout << "not coprime" << endl;
  }
}