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

#include <bits/stdc++.h>
#define ll long long int
#define MOD 1000000007
#define INF 1000000000000000000

using namespace std;

#define N 1000001

ll gcd(ll a, ll b) {
  if (a < b)
    return gcd(b, a);
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}

vector<bool> isprime;

void make_table() {
  isprime[0] = isprime[1] = 0;
  for (ll i = 2; i < N; i++) {
    if (isprime[i]) {
      for (ll j = i * i; j < N; j += i) {
        isprime[j] = 0;
      }
    }
  }
}

int main(void) {
  ll n;
  cin >> n;

  isprime = vector<bool>(N, 1);

  vector<ll> a(n);
  bool is1 = 0;
  int id = 0;
  for (int i = 0; i < n; i++) {
    ll b;
    cin >> b;

    if (b != 1) {
      a[id] = b;
      id++;
    } else if (!is1) {
      a[id] = b;
      id++;
      is1 = 1;
    }
  }

  n = id;
  // setwise
  ll g = a[0];
  for (int i = 0; i < n; i++) {
    g = gcd(g, a[i]);
  }

  // pairwise
  vector<ll> primes(0);
  make_table();
  for (int i = 0; i < N; i++) {
    if (isprime[i])
      primes.emplace_back(i);
  }

  int ok = 1;
  if (n > primes.size()) {
    ok = 0;
  } else {
    for (int i = 0; i < n; i++) {
      /*
      for (int j = 0; j < primes.size(); j++){
        if (a[i] % primes[j] == 0 && isprime[primes[j]] == 0){
          ok = 0;
        }
        else if (a[i] % primes[j] == 0){
          isprime[primes[j]] = 0;
        }
      }
      */

      for (int j = 2; j * j <= a[i]; j++) {
        if (a[i] % j == 0) {
          if (isprime[j] == 0) {
            ok = 0;
          } else {
            isprime[j] = 0;
            while (a[i] % j == 0) {
              a[i] /= j;
            }
          }
        }
      }
      if (a[i] != 1) {
        if (isprime[a[i]] == 0) {
          ok = 0;
        } else {
          isprime[a[i]] = 0;
        }
      }
    }
  }

  if (ok) {
    cout << "pairwise coprime" << endl;
  } else if (g == 1) {
    cout << "setwise coprime" << endl;
  } else
    cout << "not coprime" << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define MOD 1000000007
#define INF 1000000000000000000

using namespace std;

#define N 1000001

ll gcd(ll a, ll b) {
  if (a < b)
    return gcd(b, a);
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}

vector<bool> isprime;

void make_table() {
  isprime[0] = isprime[1] = 0;
  for (ll i = 2; i < N; i++) {
    if (isprime[i]) {
      for (ll j = i * i; j < N; j += i) {
        isprime[j] = 0;
      }
    }
  }
}

int main(void) {
  ll n;
  cin >> n;

  isprime = vector<bool>(N, 1);

  vector<ll> a(0);
  bool is1 = 0;
  for (int i = 0; i < n; i++) {
    ll b;
    cin >> b;

    if (b != 1 && !is1) {
      a.emplace_back(b);
    } else if (b == 1) {
      is1 = 1;
    }
  }

  n = a.size();
  // setwise
  ll g = a[0];
  for (int i = 0; i < n; i++) {
    g = gcd(g, a[i]);
  }

  // pairwise
  vector<ll> primes(0);
  make_table();
  for (int i = 0; i < N; i++) {
    if (isprime[i])
      primes.emplace_back(i);
  }

  int ok = 1;
  if (n > primes.size()) {
    ok = 0;
  } else {
    for (int i = 0; i < n; i++) {
      /*
      for (int j = 0; j < primes.size(); j++){
        if (a[i] % primes[j] == 0 && isprime[primes[j]] == 0){
          ok = 0;
        }
        else if (a[i] % primes[j] == 0){
          isprime[primes[j]] = 0;
        }
      }
      */

      for (int j = 2; j * j <= a[i]; j++) {
        if (a[i] % j == 0) {
          if (isprime[j] == 0) {
            ok = 0;
          } else {
            isprime[j] = 0;
            while (a[i] % j == 0) {
              a[i] /= j;
            }
          }
        }
      }
      if (a[i] != 1) {
        if (isprime[a[i]] == 0) {
          ok = 0;
        } else {
          isprime[a[i]] = 0;
        }
      }
    }
  }

  if (ok) {
    cout << "pairwise coprime" << endl;
  } else if (g == 1) {
    cout << "setwise coprime" << endl;
  } else
    cout << "not coprime" << endl;

  return 0;
}