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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define reps(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; --i)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; --i)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define repc2(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define inf 2e9
#define linf 9000000000000000000ll
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;

int n;
vector<int> v;
vector<int> min_pf;

void prime_factorize_init(int n) {
  min_pf.resize(n + 1, 0);
  for (int i = 2; i <= n; i++) {
    if (min_pf.at(i) == 0) {
      for (int mul = i; mul <= n; mul += i) {
        min_pf.at(mul) = i;
      }
    }
  }
}

void prime_factorize(int n, vector<int> &res) {
  if (n <= 1)
    return;
  while (n != 1) {
    res.push_back(min_pf.at(n));
    n /= min_pf.at(n);
  }
  res.erase(unique(res.begin(), res.end()), res.end());
}
vector<int> b(1000009);
int main() {
  cin >> n;
  v.resize(n);
  prime_factorize_init(1000000);
  rep(i, n) scanf("%d", &v[i]);
  bool is_pc = true;
  rep(i, n) {
    vector<int> pf;
    prime_factorize(v[i], pf);
    for (int p : pf) {
      b[p]++;
      if (b[p] > 1) {
        is_pc = false;
        break;
      }
    }
    if (!is_pc)
      break;
  }
  if (is_pc) {
    cout << "pairwise coprime" << endl;
  } else {
    if (accumulate(v.begin(), v.end(), 0,
                   [](int m, int n) { return __gcd(m, n); }) == 1) {
      cout << "setwise coprime" << endl;
    } else {
      cout << "not coprime" << endl;
    }
  }

  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check: __n (which is 3) >= this->size() (which is 0)


Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define reps(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; --i)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; --i)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define repc2(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define inf 2e9
#define linf 9000000000000000000ll
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;

int n;
vector<int> v;
vector<int> min_pf;

void prime_factorize_init(int n) {
  min_pf.resize(n + 1, 0);
  for (int i = 2; i <= n; i++) {
    if (min_pf.at(i) == 0) {
      for (int mul = i; mul <= n; mul += i) {
        min_pf.at(mul) = i;
      }
    }
  }
}

void prime_factorize(int n, vector<int> &res) {
  if (n <= 1)
    return;
  while (n != 1) {
    res.push_back(min_pf.at(n));
    n /= min_pf.at(n);
  }
}
vector<int> b(1000009);
int main() {
  cin >> n;
  v.resize(n);
  rep(i, n) scanf("%d", &v[i]);
  bool is_pc = true;
  rep(i, n) {
    vector<int> pf;
    prime_factorize(v[i], pf);
    for (int p : pf) {
      b[p]++;
      if (b[p] > 1) {
        is_pc = false;
        break;
      }
    }
    if (!is_pc)
      break;
  }
  if (is_pc) {
    cout << "pairwise coprime" << endl;
  } else {
    if (accumulate(v.begin(), v.end(), 0,
                   [](int m, int n) { return __gcd(m, n); }) == 1) {
      cout << "setwise coprime" << endl;
    } else {
      cout << "not coprime" << endl;
    }
  }

  return 0;
}