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

#include <bits/stdc++.h>
#define rep(i, s, n) for (int i = (s); i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define debug(x) cout << #x << ": " << x << endl
using namespace std;
using ll = long long;
const ll MOD = 1e9 + 7;
const ll LINF = LLONG_MAX;
const int INF = INT_MAX;
const double PI = acos(-1);
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  rep(i, 0, n) cin >> a[i];

  // 高速素因数分解
  vector<ll> spf(1000010, 0);
  rep(i, 0, spf.size()) spf[i] = i;
  for (ll i = 2; i * i < (ll)spf.size(); i++) {
    if (spf[i] != i)
      continue;
    for (ll j = i * i; j < (ll)spf.size(); j += i) {
      if (spf[j] == j)
        spf[j] = i;
    }
  }
  // rep(i,0,30) cout << spf[i] << endl;

  bool pairwise = true;
  // 前のと違うのにかぶってたらだめ
  vector<ll> div(1000010, -1);
  rep(i, 0, n) {
    ll dv = a[i];
    ll prev = 0;
    while (dv != 1) {
      if (div[spf[dv]] != -1 && prev != spf[dv]) {
        pairwise = false;
        break;
      }
      prev = spf[dv];
      div[spf[dv]] = 1;
      dv /= spf[dv];
    }
    if (!pairwise)
      break;
  }
  // 判定
  bool not_coprime = false;
  ll gcd = a[0];
  rep(i, 0, n) { gcd = __gcd(a[i], gcd); }
  if (gcd > 1) {
    not_coprime = true;
  }

  if (not_coprime == true) {
    cout << "not coprime" << endl;
  } else if (pairwise) {
    cout << "pairwise coprime" << endl;
  } else {
    cout << "setwise coprime" << endl;
  }
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, s, n) for (int i = (s); i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define debug(x) cout << #x << ": " << x << endl
using namespace std;
using ll = long long;
const ll MOD = 1e9 + 7;
const ll LINF = LLONG_MAX;
const int INF = INT_MAX;
const double PI = acos(-1);
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  rep(i, 0, n) cin >> a[i];

  // 高速素因数分解
  vector<ll> spf(1000010, 0);
  rep(i, 0, spf.size()) spf[i] = i;
  rep(i, 2, spf.size()) {
    if (spf[i] != i)
      continue;
    for (ll j = i * i; j < (ll)spf.size(); j += i) {
      if (spf[j] == j)
        spf[j] = i;
    }
  }
  // rep(i,0,30) cout << spf[i] << endl;

  bool pairwise = true;
  // 前のと違うのにかぶってたらだめ
  vector<ll> div(1000010, -1);
  rep(i, 0, n) {
    ll dv = a[i];
    ll prev = a[i];
    while (dv != 1) {
      if (div[spf[dv]] != -1 && prev != spf[dv]) {
        pairwise = false;
        break;
      }
      prev = spf[dv];
      div[spf[dv]] = 1;
      dv /= spf[dv];
    }
    if (!pairwise)
      break;
  }
  // 判定
  bool not_coprime = false;
  ll gcd = a[0];
  rep(i, 0, n) { gcd = __gcd(a[i], gcd); }
  if (gcd > 1) {
    not_coprime = true;
  }

  if (not_coprime == true) {
    cout << "not coprime" << endl;
  } else if (pairwise) {
    cout << "pairwise coprime" << endl;
  } else {
    cout << "setwise coprime" << endl;
  }
}