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

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, a, b)                                                           \\
  for (long long i = (a); ((a) < (b) ? i <= (b) : i >= (b));                   \\
       ((a) < (b) ? ++i : --i))
#define SZ(v) ((int)(v).size())
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << #x << ": " << (x) << endl
using namespace std;
using llong = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using pii = pair<int, int>;
constexpr int INF = 1e9;
constexpr double EPS = 1e-9;
constexpr int MOD = 1e9 + 7;
template <class Type> void line(const Type &a) {
  int cnt = 0;
  for (const auto &elem : a) {
    cerr << (cnt++ ? ' ' : '>');
    cerr << elem;
  }
  cerr << endl;
}

template <typename Type> Type igcd(Type x, Type y) {
  return (y == 0) ? x : igcd(y, x % y);
}
template <typename Type> Type ilcm(Type x, Type y) {
  return x / igcd(x, y) * y;
}

/*エラトステネスの篩*/
struct Sieve {
  int n;    // n:=(要素数).
  vi fac;   // fac[i]:=(iの最大の素因数). fac[i]=iのとき,iは素数.
  vi prime; // prime:=(1~nの素数の集合).

  Sieve(int n_) : n(n_), fac(n + 1, 0) { // constructor.
    fac[0] = fac[1] = -1;
    for (int i = 2; i <= n; ++i) { // O(n log n).
      if (!fac[i]) {
        fac[i] = i;
        prime.push_back(i);
        for (int j = i + i; j <= n; j += i)
          fac[j] = i;
      }
    }
  }

  bool is_prime(int x) { return fac[x] == x; } // 素数判定.
  map<int, int> prime_factor(int x) {          // 素因数分解(結構高速).
    map<int, int> res;
    while (x > 1) {
      res[fac[x]]++;
      x /= fac[x];
    }
    return res;
  }
};

int main() {
  int n;
  cin >> n;

  vi a(n);
  REP(i, n) cin >> a[i];

  Sieve ob((int)1e6);
  bool jud = true;
  set<int> st;
  int g = a[0];
  REP(i, n) {
    g = igcd(g, a[i]);
    map<int, int> mp = ob.prime_factor(a[i]);
    for (auto elem : mp) {
      if (jud and st.find(elem.first) == st.end())
        st.insert(elem.first);
      else
        jud = false;
    }
  }

  int flag;
  string ans[] = {"pairwise coprime", "setwise coprime", "not coprime"};
  if (jud)
    flag = 0;
  else if (g == 1)
    flag = 1;
  else
    flag = 2;

  cout << ans[flag] << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, a, b)                                                           \\
  for (long long i = (a); ((a) < (b) ? i <= (b) : i >= (b));                   \\
       ((a) < (b) ? ++i : --i))
#define SZ(v) ((int)(v).size())
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << #x << ": " << (x) << endl
using namespace std;
using llong = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using pii = pair<int, int>;
constexpr int INF = 1e9;
constexpr double EPS = 1e-9;
constexpr int MOD = 1e9 + 7;
template <class Type> void line(const Type &a) {
  int cnt = 0;
  for (const auto &elem : a) {
    cerr << (cnt++ ? ' ' : '>');
    cerr << elem;
  }
  cerr << endl;
}

template <typename Type> Type igcd(Type x, Type y) {
  return (y == 0) ? x : igcd(y, x % y);
}
template <typename Type> Type ilcm(Type x, Type y) {
  return x / igcd(x, y) * y;
}

/*エラトステネスの篩*/
struct Sieve {
  int n;    // n:=(要素数).
  vi fac;   // fac[i]:=(iの最大の素因数). fac[i]=iのとき,iは素数.
  vi prime; // prime:=(1~nの素数の集合).

  Sieve(int n_) : n(n_), fac(n + 1, 0) { // constructor.
    fac[0] = fac[1] = -1;
    for (int i = 2; i <= n; ++i) { // O(n log n).
      if (!fac[i]) {
        fac[i] = i;
        prime.push_back(i);
        for (int j = i + i; j <= n; j += i)
          fac[j] = i;
      }
    }
  }

  bool is_prime(int x) { return fac[x] == x; } // 素数判定.
  map<int, int> prime_factor(int x) {          // 素因数分解(結構高速).
    map<int, int> res;
    while (x > 1) {
      res[fac[x]]++;
      x /= fac[x];
    }
    return res;
  }
};

int main() {
  string ans[] = {"pairwise coprime", "setwise coprime", "not coprime"};

  int n;
  cin >> n;

  vi a(n);
  REP(i, n) cin >> a[i];

  Sieve ob((int)1e6);
  bool jud = false;
  set<int> st; // 素因数の集合,共通の素因数の集合.
  int g = a[n - 1];
  FOR(i, n - 1, 0) {
    g = igcd(g, a[i]);
    int tmp = a[i];
    bool hoge = true;
    while (1) {
      if (ob.fac[tmp] == tmp) {
        if (st.find(ob.fac[tmp]) != st.end())
          hoge = false;
        st.insert(ob.fac[tmp]);
        break;
      } else {
        if (st.find(ob.fac[tmp]) != st.end())
          hoge = false;
        st.insert(ob.fac[tmp]);
        tmp /= ob.fac[tmp];
      }
    }
    // debug(hoge);
    if (i != n - 1 and hoge)
      jud = true;
  }
  // line(st);
  // debug(jud);
  // debug(g);

  int flag;
  if (jud)
    flag = 0;
  else if (g == 1)
    flag = 1;
  else
    flag = 2;

  cout << ans[flag] << endl;
}