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

#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll long long
const int mod = 1000000007;
const int MOD = 998244353;
const ll INF = 1000000000000000000;

int A[1000010];
int cnt[1000010];
int gcd(int a, int b) {
  if (a < b)
    return gcd(b, a);
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
bool isPrime[1000010];
void set_prime() {
  for (int i = 1; i <= 1000000; i++) {
    isPrime[i] = true;
  }
  isPrime[0] = false;
  isPrime[1] = false;
  for (int i = 2; i * i <= 1000000; i++) {
    int j = 2;
    while (i * j <= 1000000) {
      isPrime[i * j] = false;
      j++;
    }
  }
}
int main() {
  set_prime();
  vector<int> V;
  for (int i = 0; i < 1000010; i++) {
    if (isPrime[i])
      V.push_back(i);
  }
  int N;
  // N = 10000000;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  vector<int> B;
  for (int i = 0; i < N; i++) {
    if (A[i] != 1)
      B.push_back(A[i]);
  }
  bool pc = true;
  if (B.size() <= V.size()) {
    for (int i = 0; i < B.size(); i++) {
      int tmp = B[i];
      for (int j : V) {
        if (j * j > B[i])
          break;
        bool first = true;
        while (tmp % j == 0) {
          if (first && cnt[j] >= 1)
            pc = false;
          cnt[j]++;
          first = false;
          tmp /= j;
        }
      }
      if (tmp > 1) {
        if (cnt[tmp] >= 1)
          pc = false;
      }
      cnt[tmp]++;
    }
  } else
    pc = false;
  if (pc) {
    cout << "pairwise coprime" << endl;
    return 0;
  }
  int GCD = A[0];
  for (int i = 1; i < N; i++) {
    GCD = gcd(GCD, A[i]);
  }
  if (GCD == 1)
    cout << "setwise coprime" << endl;
  else
    cout << "not coprime" << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll long long
const int mod = 1000000007;
const int MOD = 998244353;
const ll INF = 1000000000000000000;

int A[1000010];
int cnt[1000010];
int gcd(int a, int b) {
  if (a < b)
    return gcd(b, a);
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
bool isPrime[1000010];
void set_prime() {
  for (int i = 1; i <= 1000000; i++) {
    isPrime[i] = true;
  }
  isPrime[0] = false;
  isPrime[1] = false;
  for (int i = 2; i * i <= 1000000; i++) {
    int j = 2;
    while (i * j <= 1000000) {
      isPrime[i * j] = false;
      j++;
    }
  }
}
int main() {
  set_prime();
  vector<int> V;
  for (int i = 0; i < 1000010; i++) {
    if (isPrime[i])
      V.push_back(i);
  }
  int N;
  // N = 10000000;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  vector<int> B;
  for (int i = 0; i < N; i++) {
    if (A[i] != 1)
      B.push_back(A[i]);
  }
  bool pc = true;
  if (B.size() <= V.size()) {
    for (int i = 0; i < N; i++) {
      int tmp = B[i];
      for (int j : V) {
        if (j * j > B[i])
          break;
        bool first = true;
        while (tmp % j == 0) {
          if (first && cnt[j] >= 1)
            pc = false;
          cnt[j]++;
          first = false;
          tmp /= j;
        }
      }
      if (tmp > 1) {
        if (cnt[tmp] >= 1)
          pc = false;
      }
      cnt[tmp]++;
    }
  } else
    pc = false;
  if (pc) {
    cout << "pairwise coprime" << endl;
    return 0;
  }
  int GCD = A[0];
  for (int i = 1; i < N; i++) {
    GCD = gcd(GCD, A[i]);
  }
  if (GCD == 1)
    cout << "setwise coprime" << endl;
  else
    cout << "not coprime" << endl;
}