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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define MOD ll(1e9 + 7)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cerr << #x << ": " << x << endl

vector<int> divisor;
vector<int> extended_eratosthenes(int n) {
  vector<int> arr(n, 1);
  divisor[0] = 1;
  divisor[1] = 1;
  for (int i = 2; i < sqrt(n); i++) {
    if (arr[i]) {
      divisor[i] = i;
      for (int j = 2; i * j < n; j++) {
        arr[i * j] = 0;
        if (divisor[i * j] == -1) {
          divisor[i * j] = i;
        }
      }
    }
  }
  return arr;
}

int calculate_gcd(int a, int b) {
  if (a < b) {
    swap(a, b);
  }
  if (b < 1) {
    return 0;
  }
  int r;
  while ((r = a % b) != 0) {
    a = b;
    b = r;
  }
  return b;
}

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

  int num = 1000005;
  divisor.resize(num);
  for (int i = 0; i < num; i++) {
    divisor[i] = -1;
  }
  extended_eratosthenes(num);
  for (int i = 0; i < num; i++) {
    if (divisor[i] == -1) {
      divisor[i] = i;
    }
  }

  vector<int> used(num, -1);
  bool is_pairwise_coprime = true;
  for (int i = 0; i < n; i++) {
    int idx = a[i];
    while (divisor[idx] != 1) {
      if (used[divisor[idx]] != -1 && used[divisor[idx]] != i) {
        is_pairwise_coprime = false;
      }
      used[divisor[idx]] = i;
      idx /= divisor[idx];
    }
  }

  int s = a[0];
  for (int i = 0; i < n; i++) {
    s = calculate_gcd(s, a[i]);
  }
  bool is_setwise_coprime = true;
  if (s != 1) {
    is_setwise_coprime = false;
  }

  if (is_pairwise_coprime) {
    cout << "pairwise coprime" << endl;
  } else if (is_setwise_coprime) {
    cout << "setwise coprime" << endl;
  } else {
    cout << "not coprime" << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define MOD ll(1e9 + 7)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cerr << #x << ": " << x << endl

vector<int> divisor;
vector<int> extended_eratosthenes(int n) {
  vector<int> arr(n, 1);
  divisor[0] = 1;
  divisor[1] = 1;
  for (int i = 2; i < sqrt(n); i++) {
    if (arr[i]) {
      divisor[i] = i;
      for (int j = 2; i * j < n; j++) {
        arr[i * j] = 0;
        if (divisor[i * j] == -1) {
          divisor[i * j] = i;
        }
      }
    }
  }
  return arr;
}

vector<int> make_prime_list(int n) {
  vector<int> prime = extended_eratosthenes(n);
  vector<int> prime_list;
  for (int i = 2; i < n; i++) {
    if (prime[i]) {
      prime_list.push_back(i);
    }
  }
  return prime_list;
}

int calculate_gcd(int a, int b) {
  if (a < b) {
    swap(a, b);
  }
  if (b < 1) {
    return 0;
  }
  int r;
  while ((r = a % b) != 0) {
    a = b;
    b = r;
  }
  return b;
}

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

  int num = 1e6 + 1;
  divisor.resize(num);
  for (int i = 0; i < num; i++) {
    divisor[i] = -1;
  }
  vector<int> prime_list = make_prime_list(num);

  vector<int> used(num, -1);
  bool is_pairwise_coprime = true;
  for (int i = 0; i < n; i++) {
    int idx = a[i];
    while (divisor[idx] != 1) {
      if (used[divisor[idx]] != -1 && used[divisor[idx]] != i) {
        is_pairwise_coprime = false;
      }
      used[divisor[idx]] = i;
      idx /= divisor[idx];
    }
  }

  int s = a[0];
  for (int i = 0; i < n; i++) {
    s = calculate_gcd(s, a[i]);
  }
  bool is_setwise_coprime = true;
  if (s != 1) {
    is_setwise_coprime = false;
  }

  if (is_pairwise_coprime) {
    cout << "pairwise coprime" << endl;
  } else if (is_setwise_coprime) {
    cout << "setwise coprime" << endl;
  } else {
    cout << "not coprime" << endl;
  }

  return 0;
}