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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repit(it, li) for (auto it = li.begin(); it != li.end(); it++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

struct Sieve {
  int n;
  vector<int> f, primes;
  Sieve(int n = 1) : n(n), f(n + 1) {
    f[0] = f[1] = -1;
    for (ll i = 2; i <= n; ++i) {
      if (f[i])
        continue;
      primes.push_back(i);
      f[i] = i;
      for (ll j = i * i; j <= n; j += i) {
        if (!f[j])
          f[j] = i;
      }
    }
  }
  bool isPrime(int x) { return f[x] == x; }
  vector<int> factorList(int x) {
    vector<int> res;
    while (x != 1) {
      res.push_back(f[x]);
      x /= f[x];
    }
    return res;
  }
  vector<P> factor(int x) {
    vector<int> fl = factorList(x);
    if (fl.size() == 0)
      return {};
    vector<P> res;
    int pre = -1;
    for (int p : fl) {
      if (pre == p) {
        res.back().second++;
      } else {
        res.emplace_back(p, 1);
      }
      pre = p;
    }
    return res;
  }
};

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

  bool pc = true, sc = true;
  vector<bool> deta(1000005);
  vector<int> inter, ninter, li;
  bool first = true;
  Sieve si(1000005);

  rep(i, n) {
    auto vp = si.factor(a[i]);
    for (auto p : vp) {
      int pr = p.first;
      if (deta[pr])
        pc = false;
      deta[pr] = true;

      if (first)
        ninter.push_back(pr);
      else {
        auto it = lower_bound(inter.begin(), inter.end(), pr);
        if (it != inter.end() && (*it) == pr)
          ninter.push_back(pr);
      }
    }
    inter.clear();
    for (int v : ninter)
      inter.push_back(v);
    ninter.clear();
    if (first)
      first = false;
  }

  sc = inter.empty();
  if (pc)
    cout << "pairwise coprime" << endl;
  else if (sc)
    cout << "setwise coprime" << endl;
  else
    cout << "not coprime" << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repit(it, li) for (auto it = li.begin(); it != li.end(); it++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

struct Sieve {
  int n;
  vector<int> f, primes;
  Sieve(int n = 1) : n(n), f(n + 1) {
    f[0] = f[1] = -1;
    for (ll i = 2; i <= n; ++i) {
      if (f[i])
        continue;
      primes.push_back(i);
      f[i] = i;
      for (ll j = i * i; j <= n; j += i) {
        if (!f[j])
          f[j] = i;
      }
    }
  }
  bool isPrime(int x) { return f[x] == x; }
  vector<int> factorList(int x) {
    vector<int> res;
    while (x != 1) {
      res.push_back(f[x]);
      x /= f[x];
    }
    return res;
  }
  vector<P> factor(int x) {
    vector<int> fl = factorList(x);
    if (fl.size() == 0)
      return {};
    vector<P> res;
    int pre = -1;
    for (int p : fl) {
      if (pre == p) {
        res.back().second++;
      } else {
        res.emplace_back(p, 1);
      }
      pre = p;
    }
    return res;
  }
};

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

  bool pc = true, sc = true;
  vector<bool> deta(n);
  vector<int> inter;
  bool first = true;
  Sieve si(1000005);
  rep(i, n) {
    vector<int> ninter;
    auto vp = si.factor(a[i]);
    for (auto p : vp) {
      int pr = p.first;
      if (deta[pr])
        pc = false;
      deta[pr] = true;

      if (first)
        ninter.push_back(pr);
      else {
        auto it = lower_bound(inter.begin(), inter.end(), pr);
        if (it != inter.end() && (*it) == pr)
          ninter.push_back(pr);
      }
    }
    inter.clear();
    for (int v : ninter)
      inter.push_back(v);
    if (first)
      first = false;
  }

  sc = inter.empty();
  if (pc)
    cout << "pairwise coprime" << endl;
  else if (sc)
    cout << "setwise coprime" << endl;
  else
    cout << "not coprime" << endl;

  return 0;
}