Problem p02574 (C++)

Buggy submission (status: Runtime Error):
// abc177_e
#include <bits/stdc++.h>
#ifdef LOCAL
#include "../cxx-prettyprint/prettyprint.hpp"
#endif
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;

#define REP(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define REPN(i, m, n) for (int(i) = m; (i) < (int)(n); ++(i))
#define REP_REV(i, n) for (int(i) = (int)(n)-1; (i) >= 0; --(i))
#define REPN_REV(i, m, n) for (int(i) = (int)(n)-1; (i) >= m; --(i))
#define ALL(x) x.begin(), x.end()

#define INF (ll)(1e9)
#define MOD (1000000007)

#define print2D(h, w, arr)                                                     \
  REP(i, h) {                                                                  \
    REP(j, w) cout << arr[i][j] << " ";                                        \
    cout << endl;                                                              \
  }
#define print_line(vec, n)                                                     \
  {                                                                            \
    for (int idx = 0; idx < (n - 1); idx++)                                    \
      cout << (vec)[idx] << " ";                                               \
    cout << (vec)[(n)-1] << endl;                                              \
  }
template <class T> void print(const T &x) { cout << x << "\n"; }
template <class T, class... A> void print(const T &first, const A &...rest) {
  cout << first << " ";
  print(rest...);
}
struct PreMain {
  PreMain() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} premain;

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

int main() {
#ifdef LOCAL
  ifstream in("../arg.txt");
  cin.rdbuf(in.rdbuf());
#endif

  int N;
  cin >> N;

  vector<int> A(N);
  REP(i, N) cin >> A[i];

  bool pairwise = true;

  vector<int> res(1e4);
  REP(i, N) {
    ll tmp = A[i];
    for (ll a = 2; a * a <= A[i]; a++) {
      int cnt = 0;
      while (tmp % a == 0) {
        cnt++;
        tmp /= a;
      }
      if (cnt > 0) {
        if (res[a] > 0) {
          pairwise = false;
          break;
        }
        res[a]++;
      }
    }
    if (tmp > 1) {
      if (res[tmp] > 0) {
        pairwise = false;
        break;
      }
      res[tmp]++;
    }

    if (pairwise == false)
      break;
  }
  //    print(res);

  //    for (auto e: res){
  //        if (e.second >= 2){
  //            pairwise = false;
  //            break;
  //        }
  //    }

  if (pairwise) {
    print("pairwise coprime");
    return 0;
  }

  int d = 0;
  REP(i, N) { d = gcd(A[i], d); }

  //    print(d);

  if (d == 1) {
    print("setwise coprime");
  } else {
    print("not coprime");
  }

  return 0;
}


Fixed submission:
// abc177_e
#include <bits/stdc++.h>
#ifdef LOCAL
#include "../cxx-prettyprint/prettyprint.hpp"
#endif
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;

#define REP(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define REPN(i, m, n) for (int(i) = m; (i) < (int)(n); ++(i))
#define REP_REV(i, n) for (int(i) = (int)(n)-1; (i) >= 0; --(i))
#define REPN_REV(i, m, n) for (int(i) = (int)(n)-1; (i) >= m; --(i))
#define ALL(x) x.begin(), x.end()

#define INF (ll)(1e9)
#define MOD (1000000007)

#define print2D(h, w, arr)                                                     \
  REP(i, h) {                                                                  \
    REP(j, w) cout << arr[i][j] << " ";                                        \
    cout << endl;                                                              \
  }
#define print_line(vec, n)                                                     \
  {                                                                            \
    for (int idx = 0; idx < (n - 1); idx++)                                    \
      cout << (vec)[idx] << " ";                                               \
    cout << (vec)[(n)-1] << endl;                                              \
  }
template <class T> void print(const T &x) { cout << x << "\n"; }
template <class T, class... A> void print(const T &first, const A &...rest) {
  cout << first << " ";
  print(rest...);
}
struct PreMain {
  PreMain() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} premain;

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

int main() {
#ifdef LOCAL
  ifstream in("../arg.txt");
  cin.rdbuf(in.rdbuf());
#endif

  int N;
  cin >> N;

  vector<int> A(N);
  REP(i, N) cin >> A[i];

  bool pairwise = true;

  vector<int> res(1e6 + 1);
  REP(i, N) {
    ll tmp = A[i];
    for (ll a = 2; a * a <= A[i]; a++) {
      int cnt = 0;
      while (tmp % a == 0) {
        cnt++;
        tmp /= a;
      }
      if (cnt > 0) {
        if (res[a] > 0) {
          pairwise = false;
          break;
        }
        res[a]++;
      }
    }
    if (tmp > 1) {
      if (res[tmp] > 0) {
        pairwise = false;
        break;
      }
      res[tmp]++;
    }

    if (pairwise == false)
      break;
  }
  //    print(res);

  //    for (auto e: res){
  //        if (e.second >= 2){
  //            pairwise = false;
  //            break;
  //        }
  //    }

  if (pairwise) {
    print("pairwise coprime");
    return 0;
  }

  int d = 0;
  REP(i, N) { d = gcd(A[i], d); }

  //    print(d);

  if (d == 1) {
    print("setwise coprime");
  } else {
    print("not coprime");
  }

  return 0;
}