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

#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
#define all(x) (x).begin(), (x).end()
#define endl "\\n"
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ar array
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
const ll mod = 1e9 + 7;
const ll inf = 2e9 + 5;
double PI = 3.14159265358979323846;

const int N = 2e6 + 5;
vi primes;
vi p(N, 1);
vi cnt(N, 0);

void sieve() {
  p[1] = p[0] = 0;
  for (ll i = 2; i < N; i++) {
    if (p[i]) {
      for (ll j = i * i; j < N; j += i) {
        p[j] = 0;
      }
      primes.pb(i);
    }
  }
}

template <typename T> T gcd(T a, T b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}

template <typename T> T lcm(T a, T b) { return (a * b) / gcd(a, b); }

void solve() {

  sieve();
  int n;
  cin >> n;
  int a[n];
  int G;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  G = a[0];
  for (int i = 1; i < n; i++) {
    G = gcd(G, a[i]);
  }

  if (G != 1) {
    cout << "not coprime" << endl;
    return;
  }

  for (int i = 0; i < n; i++) {
    int x = a[i];
    for (int k = 0; k < (int)primes.size() && primes[k] * primes[k] <= x; k++) {
      if (x % primes[k] == 0) {
        if (cnt[primes[k]]) {
          cout << "setwise coprime" << endl;
          return;
        }

        while (x % primes[k] == 0) {
          x = x / primes[k];
        }
        cnt[primes[k]]++;
      }
    }
    if (x > 1) {
      if (cnt[x]) {
        cout << "setwise coprime" << endl;
        return;
      }
      cnt[x]++;
    }
  }

  cout << "pairwise coprime" << endl;
}

int32_t main() {
  IOS
  // int T; cin >> T; while (T--)
  solve();
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
#define all(x) (x).begin(), (x).end()
#define endl "\\n"
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ar array
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
const ll mod = 1e9 + 7;
const ll inf = 2e9 + 5;
double PI = 3.14159265358979323846;

const int N = 2e5 + 5;
vi primes;
vi p(N, 1);

void sieve() {
  p[1] = p[0] = 0;
  for (ll i = 2; i < N; i++) {
    if (p[i]) {
      for (ll j = i * i; j < N; j += i) {
        p[j] = 0;
      }
      primes.pb(i);
    }
  }
}

template <typename T> T gcd(T a, T b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}

template <typename T> T lcm(T a, T b) { return (a * b) / gcd(a, b); }

void solve() {

  sieve();
  int n;
  cin >> n;
  int a[n];
  int G;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  G = a[0];
  for (int i = 1; i < n; i++) {
    G = gcd(G, a[i]);
  }

  if (G != 1) {
    cout << "not coprime" << endl;
    return;
  }

  vi cnt(N, 0);

  for (int i = 0; i < n; i++) {
    int x = a[i];
    for (int k = 0; k < (int)primes.size() && primes[k] * primes[k] <= x; k++) {
      if (x % primes[k] == 0) {
        if (cnt[primes[k]]) {
          cout << "setwise coprime" << endl;
          return;
        }

        while (x % primes[k] == 0) {
          x = x / primes[k];
        }
        cnt[primes[k]]++;
      }
    }
    if (x > 1) {
      if (cnt[x]) {
        cout << "setwise coprime" << endl;
        return;
      }
      cnt[x]++;
    }
  }

  cout << "pairwise coprime" << endl;
}

int32_t main() {
  IOS
  // int T; cin >> T; while (T--)
  solve();
  return 0;
}