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

#include <bits/stdc++.h>
using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPr(i, n) for (int i = (n)-1; i >= 0; --i)
#define FORq(i, m, n) for (int i = (m); i <= (n); ++i)
#define FORqr(i, m, n) for (int i = (n); i >= (m); --i)
#define MP make_pair
#define SIN(x, S) (S.count(x) != 0)
#define M0(x) memset(x, 0, sizeof(x))
#define FILL(x, y) memset(x, y, sizeof(x))
#define MM(x) memset(x, -1, sizeof(x))
#define ALL(x) (x).begin(), (x).end()
#define DB(x) cerr << #x << " = " << x << endl
#define DB2(x, y)                                                              \\
  cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")\\n";
#define DEBUG                                                                  \\
  int x12345;                                                                  \\
  cin >> x12345;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef long long ll;
typedef long long integer;
const long long MOD = 1e9 + 7;
///////////////////////////////////////////////
// for template
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
///////////////////////////////////////////////
/// 🍈( '-' 🍈 |AC|

std::vector<int> sieve(int n) {
  std::vector<int> res(n);
  std::iota(res.begin(), res.end(), 0);
  for (int i = 2; i * i < n; ++i) {
    if (res[i] < i)
      continue;
    for (int j = i * i; j < n; j += i)
      if (res[j] == j)
        res[j] = i;
  }
  return res;
}

std::vector<int> factor(int n, const std::vector<int> &min_factor) {
  // min_factor は sieve() で得られたものとする
  std::vector<int> res;
  while (n > 1) {
    res.push_back(min_factor[n]);
    n /= min_factor[n];
    // 割った後の値についても素因数を知っているので順次求まる
  }
  return res;
}

int main() {
  int N;
  cin >> N;
  vector<int> exists(1000002, -1);
  set<ll> setwisec;
  bool pairwise = true;
  bool setwise = false;
  vector<int> sieves = sieve(1000001);
  vector<ll> A(N);
  REP(i, N) { cin >> A[i]; }

  REP(i, N) {
    // for (auto x : setwisec){
    //     DB2(i,x);
    // }

    for (auto x : setwisec) {
      if (A[i] % x != 0)
        setwisec.erase(x);
    }

    vector<int> D = factor(A[i], sieves);

    REP(j, D.size()) {
      ll NowDiv = D[j];
      if (NowDiv == 1)
        continue;
      // DB2(j, NowDiv);
      if (i == 0) {
        setwisec.insert(NowDiv);
      }

      if ((exists[NowDiv] != i) and (exists[NowDiv] != -1))
        pairwise = false;
      exists[NowDiv] = i;
    }
  }
  if (setwisec.empty())
    setwise = true;

  if (pairwise) {
    puts("pairwise coprime");
  } else if (setwise) {
    puts("setwise coprime");
  } else {
    puts("not coprime");
  }
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPr(i, n) for (int i = (n)-1; i >= 0; --i)
#define FORq(i, m, n) for (int i = (m); i <= (n); ++i)
#define FORqr(i, m, n) for (int i = (n); i >= (m); --i)
#define MP make_pair
#define SIN(x, S) (S.count(x) != 0)
#define M0(x) memset(x, 0, sizeof(x))
#define FILL(x, y) memset(x, y, sizeof(x))
#define MM(x) memset(x, -1, sizeof(x))
#define ALL(x) (x).begin(), (x).end()
#define DB(x) cerr << #x << " = " << x << endl
#define DB2(x, y)                                                              \\
  cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")\\n";
#define DEBUG                                                                  \\
  int x12345;                                                                  \\
  cin >> x12345;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef long long ll;
typedef long long integer;
const long long MOD = 1e9 + 7;
///////////////////////////////////////////////
// for template
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
///////////////////////////////////////////////
/// 🍈( '-' 🍈 |AC|

std::vector<int> sieve(int n) {
  std::vector<int> res(n);
  std::iota(res.begin(), res.end(), 0);
  for (int i = 2; i * i < n; ++i) {
    if (res[i] < i)
      continue;
    for (int j = i * i; j < n; j += i)
      if (res[j] == j)
        res[j] = i;
  }
  return res;
}

std::vector<int> factor(int n, const std::vector<int> &min_factor) {
  // min_factor は sieve() で得られたものとする
  std::vector<int> res;
  while (n > 1) {
    res.push_back(min_factor[n]);
    n /= min_factor[n];
    // 割った後の値についても素因数を知っているので順次求まる
  }
  return res;
}

int main() {
  int N;
  cin >> N;
  vector<int> exists(1000002, -1);
  set<ll> setwisec;
  bool pairwise = true;
  bool setwise = false;
  vector<int> sieves = sieve(1000000);
  vector<ll> A(N);
  REP(i, N) { cin >> A[i]; }

  REP(i, N) {
    // for (auto x : setwisec){
    //     DB2(i,x);
    // }

    for (auto x : setwisec) {
      if (A[i] % x != 0)
        setwisec.erase(x);
    }

    vector<int> D = factor(A[i], sieves);

    REP(j, D.size()) {
      ll NowDiv = D[j];
      if (NowDiv == 1)
        continue;
      // DB2(j, NowDiv);
      if (i == 0) {
        setwisec.insert(NowDiv);
      }

      if ((exists[NowDiv] != i) and (exists[NowDiv] != -1))
        pairwise = false;
      exists[NowDiv] = i;
    }
  }
  if (setwisec.empty())
    setwise = true;

  if (pairwise) {
    puts("pairwise coprime");
  } else if (setwise) {
    puts("setwise coprime");
  } else {
    puts("not coprime");
  }
}