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

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

namespace utils {
#define ALL(x) begin(x), end(x)
#define RALL(x) rbegin(x), rend(x)
///----- aliases
using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;
using ll = i64;
using ull = u64;

using f32 = float;
using f64 = double;

template <class T, class Compare>
using p_queue = priority_queue<T, vector<T>, Compare>;
template <class T> using min_queue = p_queue<T, greater<T>>;
template <class T> using max_queue = p_queue<T, less<T>>;

template <class T> inline bool CHMIN(T &X, const T &A) {
  if (X > A) {
    X = A;
    return true;
  }
  return false;
}
template <class T> inline bool CHMAX(T &X, const T &A) {
  if (X < A) {
    X = A;
    return true;
  }
  return false;
}

///----- vector I/O
template <class T> vector<T> VEC(size_t n, T t) { return vector<T>(n, t); }

template <class... Ts> auto VEC(size_t n, Ts... ts) {
  return vector<decltype(VEC(ts...))>(n, VEC(ts...));
}

template <class T> vector<T> VIN(istream &is, size_t n) {
  vector<T> v(n);
  for (int i = 0; i < n; ++i) {
    is >> v[i];
  } // end i
  return v;
}

template <class T> auto VIN(istream &is, size_t n, size_t ts...) {
  auto v = vector<decltype(VIN<T>(is, ts))>(n);
  for (auto &&x : v) {
    x = VIN<T>(is, ts);
  } // end x
  return v;
}

template <class T> bool VOUT(ostream &os, vector<T> &v) {
  for (auto &&x : v) {
    os << x << ' ';
  } // end x
  return !!os;
}

template <class T>
bool VOUT(ostream &os, vector<vector<T>> &v, char endc = '\\n') {
  for (auto &&vs : v) {
    VOUT(os, vs);
    os << endc;
  } // end vs
  return !!os;
}

///----- tuple I/O
template <class S, class T> istream &operator>>(istream &is, tuple<S, T> &t) {
  return is >> get<0>(t) >> get<1>(t);
}

template <class S, class T, class U>
istream &operator>>(istream &is, tuple<S, T, U> &t) {
  return is >> get<0>(t) >> get<1>(t) >> get<2>(t);
}

template <class S, class T>
ostream &operator<<(ostream &os, const tuple<S, T> &t) {
  return os << get<0>(t) << ' ' << get<1>(t);
}

template <class S, class T, class U>
ostream &operator<<(ostream &os, const tuple<S, T, U> &t) {
  return os << get<0>(t) << ' ' << get<1>(t) << ' ' << get<2>(t);
}

///----- constants
constexpr i64 INFLL = 1'000'000'000'000'000'020ll;
constexpr i32 INF = 1'000'000'009;
constexpr f64 PI = 3.14'159'265'358'979'323'846;
constexpr f64 EPS = 1e-12;
} // namespace utils
using namespace utils;

class solver {
  istream &is;
  ostream &os;

public:
  solver(istream &I, ostream &O) : is(I), os(O) {}

  int N;
  vector<int> A;
  bool input() {
    N = 0;
    is >> N;
    A = VIN<int>(is, N);
    return !!is;
  }

  ll gcd(ll a, ll b) {
    if (b == 0)
      return a;
    return gcd(b, a % b);
  }

  bool is_setwise() {
    ll g = gcd(A[0], A[1]);
    for (int i = 2; i < A.size(); ++i) {
      g = gcd(A[i], g);
    } // end i
    return g == 1;
  }

  vector<int> min_factor;

  void init_p() {
    int mx = 1'000'010;
    min_factor.resize(mx, INF);
    for (int i = 2; i < mx; ++i) {
      if (min_factor[i] == INF) {
        min_factor[i] = i;
        for (int j = i + i; j < mx; j += i) {
          CHMIN(min_factor[j], i);
        } // end j
      }
    } // end i
  }

  bool is_paiwise() {
    vector<int> exist(min_factor.size(), INF);

    for (int i = 0; i < A.size(); ++i) {
      int a = A[i];
      while (min_factor[a] != INF) {
        if (exist[a] < i or exist[min_factor[a]] < i)
          return false;
        exist[min_factor[a]] = i;
        exist[a] = i;
        a /= min_factor[a];
      }

    } // end i

    return true;
  }

  void run();
};

void solver::run() {
  if (!input())
    return;
  init_p();
  if (is_paiwise())
    cout << "pairwise coprime\\n";
  else if (is_setwise())
    cout << "setwise coprime\\n";
  else
    cout << "not coprime\\n";
}

int main(int argc, char *argv[]) {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << setprecision(16) << scientific;
#ifdef XELMH
  string test_cases = "test_E.txt";
  cerr << test_cases << " -->" << endl;
  auto fs = fstream(test_cases, fstream::in);
  int loop = 0;
  while (fs) {
    loop++;
    cout << '#' << loop << "#------\\n";
    solver(fs, cout).run();
  }
  if (loop <= 1) {
    cout << "===" << endl;
    while (cin)
      solver(cin, cout).run();
  }
#else
  solver(cin, cout).run();
#endif
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

namespace utils {
#define ALL(x) begin(x), end(x)
#define RALL(x) rbegin(x), rend(x)
///----- aliases
using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;
using ll = i64;
using ull = u64;

using f32 = float;
using f64 = double;

template <class T, class Compare>
using p_queue = priority_queue<T, vector<T>, Compare>;
template <class T> using min_queue = p_queue<T, greater<T>>;
template <class T> using max_queue = p_queue<T, less<T>>;

template <class T> inline bool CHMIN(T &X, const T &A) {
  if (X > A) {
    X = A;
    return true;
  }
  return false;
}
template <class T> inline bool CHMAX(T &X, const T &A) {
  if (X < A) {
    X = A;
    return true;
  }
  return false;
}

///----- vector I/O
template <class T> vector<T> VEC(size_t n, T t) { return vector<T>(n, t); }

template <class... Ts> auto VEC(size_t n, Ts... ts) {
  return vector<decltype(VEC(ts...))>(n, VEC(ts...));
}

template <class T> vector<T> VIN(istream &is, size_t n) {
  vector<T> v(n);
  for (int i = 0; i < n; ++i) {
    is >> v[i];
  } // end i
  return v;
}

template <class T> auto VIN(istream &is, size_t n, size_t ts...) {
  auto v = vector<decltype(VIN<T>(is, ts))>(n);
  for (auto &&x : v) {
    x = VIN<T>(is, ts);
  } // end x
  return v;
}

template <class T> bool VOUT(ostream &os, vector<T> &v) {
  for (auto &&x : v) {
    os << x << ' ';
  } // end x
  return !!os;
}

template <class T>
bool VOUT(ostream &os, vector<vector<T>> &v, char endc = '\\n') {
  for (auto &&vs : v) {
    VOUT(os, vs);
    os << endc;
  } // end vs
  return !!os;
}

///----- tuple I/O
template <class S, class T> istream &operator>>(istream &is, tuple<S, T> &t) {
  return is >> get<0>(t) >> get<1>(t);
}

template <class S, class T, class U>
istream &operator>>(istream &is, tuple<S, T, U> &t) {
  return is >> get<0>(t) >> get<1>(t) >> get<2>(t);
}

template <class S, class T>
ostream &operator<<(ostream &os, const tuple<S, T> &t) {
  return os << get<0>(t) << ' ' << get<1>(t);
}

template <class S, class T, class U>
ostream &operator<<(ostream &os, const tuple<S, T, U> &t) {
  return os << get<0>(t) << ' ' << get<1>(t) << ' ' << get<2>(t);
}

///----- constants
constexpr i64 INFLL = 1'000'000'000'000'000'020ll;
constexpr i32 INF = 1'000'000'009;
constexpr f64 PI = 3.14'159'265'358'979'323'846;
constexpr f64 EPS = 1e-12;
} // namespace utils
using namespace utils;

class solver {
  istream &is;
  ostream &os;

public:
  solver(istream &I, ostream &O) : is(I), os(O) {}

  int N;
  vector<int> A;
  bool input() {
    N = 0;
    is >> N;
    A = VIN<int>(is, N);
    return !!is;
  }

  ll gcd(ll a, ll b) {
    if (b == 0)
      return a;
    return gcd(b, a % b);
  }

  bool is_setwise() {
    ll g = gcd(A[0], A[1]);
    for (int i = 2; i < A.size(); ++i) {
      g = gcd(A[i], g);
    } // end i
    return g == 1;
  }

  vector<int> min_factor;

  void init_p() {
    int mx = 1'000'000;
    min_factor.resize(mx, INF);
    for (int i = 2; i < mx; ++i) {
      if (min_factor[i] == INF) {
        min_factor[i] = i;
        for (int j = i + i; j < mx; j += i) {
          CHMIN(min_factor[j], i);
        } // end j
      }
    } // end i
  }

  bool is_paiwise() {
    vector<int> exist(min_factor.size(), INF);

    for (int i = 0; i < A.size(); ++i) {
      int a = A[i];
      while (min_factor[a] != INF) {
        if (exist[a] < i or exist[min_factor[a]] < i)
          return false;
        exist[min_factor[a]] = i;
        exist[a] = i;
        a /= min_factor[a];
      }

    } // end i

    return true;
  }

  void run();
};

void solver::run() {
  if (!input())
    return;
  init_p();
  if (is_paiwise())
    cout << "pairwise coprime\\n";
  else if (is_setwise())
    cout << "setwise coprime\\n";
  else
    cout << "not prime\\n";
}

int main(int argc, char *argv[]) {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << setprecision(16) << scientific;
#ifdef XELMH
  string test_cases = "test_E.txt";
  cerr << test_cases << " -->" << endl;
  auto fs = fstream(test_cases, fstream::in);
  int loop = 0;
  while (fs) {
    loop++;
    cout << '#' << loop << "#------\\n";
    solver(fs, cout).run();
  }
  if (loop <= 1) {
    cout << "===" << endl;
    while (cin)
      solver(cin, cout).run();
  }
#else
  solver(cin, cout).run();
#endif
  return 0;
}