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

#include <algorithm>
#include <cmath>
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
using Graph = vector<vector<int>>;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
#define rep(i, N) for (int i = 0; i < N; i++)
#define rep2(i, l, r) for (ll i = (ll)l; i < (ll)(r); i++)
#define INF 1000000000000000000
#define MAX 200001

const ll MOD = 1000000007;

template <typename T> inline string toString(const T &a) {
  ostringstream oss;
  oss << a;
  return oss.str();
};

map<int, int> m;

int func(ll N, vector<ll> &A) {
  vector<int> isPrime(1001, 1);
  for (int i = 2; i < 1001; i++) {
    if (isPrime[i] == 0)
      continue;
    for (int j = 2 * i; j < 1001; j += i) {
      isPrime[j] = 0;
    }
  }
  int flag = 0;
  vector<int> check;
  for (int i = 2; i < 1001; i++) {
    if (isPrime[i] == 1)
      check.push_back(i);
  }
  int s = check.size();
  rep(i, s) {
    int now = check[i];
    int cnt = 0;
    for (int j = 0; j < N; j++) {
      if (now > A[j])
        continue;
      if (A[j] % now == 0) {
        cnt++;
      }
      while (A[j] % now == 0)
        A[j] /= now;
    }
    m[now] = cnt;
  }
  rep(i, N) {
    if (A[i] != 1)
      m[A[i]] += 1;
  }
  int MX = 0;
  for (auto mm : m) {
    MX = max(MX, mm.second);
  }
  if (MX == N)
    flag = 0;
  else if (MX >= 2)
    flag = 1;
  else
    flag = 2;
  return flag;
}

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  rep(i, N) cin >> A[i];
  int flag = func(N, A);
  if (flag == 0) {
    cout << "not coprime" << endl;
  } else if (flag == 1) {
    cout << "setwise coprime" << endl;
  } else {
    cout << "pairwise coprime" << endl;
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
using Graph = vector<vector<int>>;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
#define rep(i, N) for (int i = 0; i < N; i++)
#define rep2(i, l, r) for (ll i = (ll)l; i < (ll)(r); i++)
#define INF 1000000000000000000
#define MAX 200001

const ll MOD = 1000000007;

template <typename T> inline string toString(const T &a) {
  ostringstream oss;
  oss << a;
  return oss.str();
};

map<int, int> m;

int func(ll N, vector<ll> &A) {
  vector<int> isPrime(1001, 1);
  for (int i = 2; i < 1001; i++) {
    if (isPrime[i] == 0)
      continue;
    for (int j = 2 * i; j < 1001; j += i) {
      isPrime[j] = 0;
    }
  }
  int flag = 0;
  vector<int> check;
  for (int i = 2; i < 1001; i++) {
    if (isPrime[i] == 1)
      check.push_back(i);
  }
  int s = check.size();
  rep(i, s) {
    int now = check[i];
    int cnt = 0;
    for (int j = 0; j < N; j++) {
      if (now > A[j])
        continue;
      if (A[j] % now == 0) {
        cnt++;
      }
      while (A[j] % now == 0)
        A[j] /= now;
    }
    m[now] = cnt;
  }
  rep(i, N) {
    if (A[i] != 1)
      m[A[i]] = 1;
  }
  int MX = 0;
  for (auto mm : m) {
    MX = max(MX, mm.second);
  }
  if (MX == N)
    flag = 0;
  else if (MX >= 2)
    flag = 1;
  else
    flag = 2;
}

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  rep(i, N) cin >> A[i];
  int flag = func(N, A);
  if (flag == 0) {
    cout << "not coprime" << endl;
  } else if (flag == 1) {
    cout << "setwise coprime" << endl;
  } else {
    cout << "pairwise coprime" << endl;
  }
}