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

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(obj) (obj).begin(), (obj).end()

#define debug(var)                                                             \\
  do {                                                                         \\
    std::cout << __LINE__ << "> " << #var << ": ";                             \\
    view(var);                                                                 \\
  } while (0)
template <typename T> void view(T e) { std::cout << e << std::endl; }
template <typename T> void view(const std::vector<T> &v) {
  for (const auto &e : v) {
    std::cout << e << " ";
  }
  std::cout << std::endl;
}
template <typename T> void view(const std::vector<std::vector<T>> &vv) {
  for (const auto &v : vv) {
    view(v);
  }
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

using namespace std;
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;

long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
vector<long long> divisor(long long n) {
  vector<long long> res;
  for (long long i = 1; i * i <= n; ++i) {
    if (n % i == 0) {
      res.push_back(i);
      if (i != n / i)
        res.push_back(n / i);
    }
  }
  return res;
}
struct SieveOfEstosthenes {
  int n;
  vector<bool> is_prime;
  vector<int> prime;
  SieveOfEstosthenes(int n) : n(n), is_prime(n, true) {
    is_prime[0] = is_prime[1] = false;
    for (int i = 2; i <= n; ++i) {
      if (is_prime[i]) {
        prime.push_back(i);
        for (int j = 2 * i; j <= n; j += i)
          is_prime[j] = false;
      }
    }
  }
};

void Main() {
  int N;
  cin >> N;
  vl A(N);
  ll m = 1;
  REP(i, N) {
    cin >> A[i];
    m = max(m, A[i]);
  }
  vi cnt(m + 1, 0);
  SieveOfEstosthenes era(m + 1);
  bool pairwise = true;
  REP(i, N) {
    ll n = A[i];
    // vi in_cnt(m + 1);
    // for (long long j = 2; j * j <= n; ++j) {
    // 	while (n % j == 0) {
    // 		if (in_cnt[j] == 0 && cnt[j] == 1) {
    // 			pairwise = false;
    // 			goto NOT_PAR;
    // 		}
    // 		if (in_cnt[j] == 0) {
    // 			cnt[j]++;
    // 		}
    // 		in_cnt[j]++;
    // 		n /= j;
    // 	}
    // }
    for (long long j = 1; j * j <= n; ++j) {
      if (n % j == 0) {
        if (cnt[j] == 1 && era.is_prime[j]) {
          pairwise = false;
          goto NOT_PAR;
        }
        cnt[j]++;
        if (j != n / j) {
          if (cnt[n / j] == 1 && era.is_prime[n / j]) {
            pairwise = false;
            goto NOT_PAR;
          }
          cnt[n / j]++;
        };
      }
    }
  }
NOT_PAR:
  if (pairwise) {
    cout << "pairwise coprime" << endl;
    return;
  }
  ll g = A[0];
  FOR(i, 1, N) { g = gcd(g, A[i]); }
  bool setwise = g == 1;
  if (setwise) {
    cout << "setwise coprime" << endl;
    return;
  }
  cout << "not coprime" << endl;
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(obj) (obj).begin(), (obj).end()

#define debug(var)                                                             \\
  do {                                                                         \\
    std::cout << __LINE__ << "> " << #var << ": ";                             \\
    view(var);                                                                 \\
  } while (0)
template <typename T> void view(T e) { std::cout << e << std::endl; }
template <typename T> void view(const std::vector<T> &v) {
  for (const auto &e : v) {
    std::cout << e << " ";
  }
  std::cout << std::endl;
}
template <typename T> void view(const std::vector<std::vector<T>> &vv) {
  for (const auto &v : vv) {
    view(v);
  }
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

using namespace std;
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;

long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
vector<long long> divisor(long long n) {
  vector<long long> res;
  for (long long i = 1; i * i <= n; ++i) {
    if (n % i == 0) {
      res.push_back(i);
      if (i != n / i)
        res.push_back(n / i);
    }
  }
  return res;
}
struct SieveOfEstosthenes {
  int n;
  vector<bool> is_prime;
  vector<int> prime;
  SieveOfEstosthenes(int n) : n(n), is_prime(n, true) {
    is_prime[0] = is_prime[1] = false;
    for (int i = 2; i <= n; ++i) {
      if (is_prime[i]) {
        prime.push_back(i);
        for (int j = 2 * i; j <= n; j += i)
          is_prime[j] = false;
      }
    }
  }
};

void Main() {
  int N;
  cin >> N;
  vl A(N);
  ll m = 1;
  REP(i, N) {
    cin >> A[i];
    m = max(m, A[i]);
  }
  vi cnt(m + 1);
  SieveOfEstosthenes era(m);
  bool pairwise = true;
  REP(i, N) {
    ll n = A[i];
    // vi in_cnt(m + 1);
    // for (long long j = 2; j * j <= n; ++j) {
    // 	while (n % j == 0) {
    // 		if (in_cnt[j] == 0 && cnt[j] == 1) {
    // 			pairwise = false;
    // 			goto NOT_PAR;
    // 		}
    // 		if (in_cnt[j] == 0) {
    // 			cnt[j]++;
    // 		}
    // 		in_cnt[j]++;
    // 		n /= j;
    // 	}
    // }
    for (long long j = 1; j * j <= n; ++j) {
      if (n % j == 0) {
        if (cnt[j] == 1 && era.is_prime[j]) {
          pairwise = false;
          goto NOT_PAR;
        }
        cnt[j]++;
        if (j != n / j) {
          if (cnt[n / j] == 1 && era.is_prime[j]) {
            pairwise = false;
            goto NOT_PAR;
          }
          cnt[n / j]++;
        };
      }
    }
  }
NOT_PAR:
  if (pairwise) {
    cout << "pairwise coprime" << endl;
    return;
  }
  ll g = A[0];
  FOR(i, 1, N) { g = gcd(g, A[i]); }
  bool setwise = g == 1;
  if (setwise) {
    cout << "setwise coprime" << endl;
    return;
  }
  cout << "not coprime" << endl;
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
}