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

#include <bits/stdc++.h>
using namespace std;
#define sz(x) (int)x.size()
#define pb push_back
#define mp make_pair
#define ll long long
#define mod 1000000007
void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}

int main() {
  fast();
  int n;
  cin >> n;
  int a[n];
  unordered_map<int, int> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int allgcd = a[0];
  for (int i = 1; i < n; i++) {
    allgcd = __gcd(a[i], allgcd);
  }
  const int N = 1000001;
  vector<char> is_prime(N + 1, true);
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i <= N; i++) {
    if (is_prime[i] && (long long)i * i <= N) {
      for (int j = i * i; j <= N; j += i)
        is_prime[j] = false;
    }
  }
  vector<int> c;
  for (int i = 0; i <= N / 2; i++) {
    if (is_prime[i]) {
      c.emplace_back(i);
    }
  }
  int g = 1;
  for (int i = 0; i < n; i++) {
    int l = a[i];
    if (is_prime[l]) {
      m[l]++;
      if (m[l] > 1) {
        g = 0;
        i = n;
      }
    } else {
      for (int j = 0; j < (int)c.size(); j++) {
        if (is_prime[l]) {
          m[l]++;
          if (m[l] > 1) {
            g = 0;
            i = n;
          }
          break;
        }
        if (l % c[j] == 0) {
          m[c[j]]++;
          if (m[c[j]] > 1) {
            g = 0;
            i = n;
            break;
          }
          while (l % c[j] == 0) {
            l = l / c[j];
          }
        }
        if (l < c[j]) {
          break;
        }
      }
    }
  }

  if (g) {
    cout << "pairwise coprime"
         << "\\n";
    return 0;
  }
  if (allgcd == 1) {
    cout << "setwise coprime"
         << "\\n";
    return 0;
  }
  cout << "not coprime"
       << "\\n";
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define sz(x) (int)x.size()
#define pb push_back
#define mp make_pair
#define ll long long
#define mod 1000000007
void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}

int main() {
  fast();
  int n;
  cin >> n;
  int a[n];
  unordered_map<int, int> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int allgcd = a[0];
  for (int i = 1; i < n; i++) {
    allgcd = __gcd(a[i], allgcd);
  }
  const int N = 500001;
  vector<char> is_prime(N + 1, true);
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i <= N; i++) {
    if (is_prime[i] && (long long)i * i <= N) {
      for (int j = i * i; j <= N; j += i)
        is_prime[j] = false;
    }
  }
  vector<int> c;
  for (int i = 0; i <= N; i++) {
    if (is_prime[i]) {
      c.emplace_back(i);
    }
  }
  int g = 1;
  for (int i = 0; i < n; i++) {
    int l = a[i];
    if (is_prime[l]) {
      m[l]++;
      if (m[l] > 1) {
        g = 0;
        i = n;
      }
    } else {
      for (int j = 0; j < (int)c.size(); j++) {
        if (is_prime[l]) {
          m[l]++;
          if (m[l] > 1) {
            g = 0;
            i = n;
          }
          break;
        }
        if (l % c[j] == 0) {
          m[c[j]]++;
          if (m[c[j]] > 1) {
            g = 0;
            i = n;
            break;
          }
          while (l % c[j] == 0) {
            l = l / c[j];
          }
        }
        if (l < c[j]) {
          break;
        }
      }
    }
  }

  if (g) {
    cout << "pairwise coprime"
         << "\\n";
    return 0;
  }
  if (allgcd == 1) {
    cout << "setwise coprime"
         << "\\n";
    return 0;
  }
  cout << "not coprime"
       << "\\n";
  return 0;
}