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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

const int MAX_N = 1e5 + 5;
const int MAX_L = 20; // ~ Log N
const long long MOD = 1e9 + 7;
const long long INF = 1e9 + 7;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<vi> vvi;

#define rep(i, n) for (int i = 0; i < n; ++i)
#define REP(i, k, n) for (i = k; i <= n; ++i)
#define REPR(i, k, n) for (int i = k; i >= n; --i)

#define LSOne(S) (S & (-S))
#define isBitSet(S, i) ((S >> i) & 1)

int max_n = 1e6 + 1;

void solve() {
  int N;
  cin >> N;
  vi v;
  rep(i, N) {
    int x;
    cin >> x;
    v.push_back(x);
  }

  vector<int> D(max_n, 0);
  vector<int> factors(max_n, 0);

  for (int i = 1; i < max_n; i++) {
    D[i] = i;
  }

  for (int i = 2; i <= 1000000; i++) {
    if (D[i] == i) {
      for (int j = 2; j * i <= 1000000; j++) {
        if (D[j * i] == j * i) {
          D[j * i] = i;
        }
      }
    }
  }

  bool pairWise = true;
  for (int i = 0; i < v.size() && pairWise; i++) {
    int x = v[i];
    while (D[x] != 1) {
      int f = D[x];
      if (factors[f]) {
        pairWise = false;
      }
      factors[f] = 1;
      while (x % f == 0) {
        x /= f;
      }
    }
  }
  if (pairWise) {
    cout << "pairwise coprime";
    return;
  }

  int d = v[0];
  for (int i = 1; i < v.size(); i++) {
    d = __gcd(d, v[i]);
  }
  if (d == 1) {
    cout << "setwise coprime";
  } else {
    cout << "not coprime";
  }
}

// #define LOCAL 1

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);

#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  solve();

  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

const int MAX_N = 1e5 + 5;
const int MAX_L = 20; // ~ Log N
const long long MOD = 1e9 + 7;
const long long INF = 1e9 + 7;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<vi> vvi;

#define rep(i, n) for (int i = 0; i < n; ++i)
#define REP(i, k, n) for (i = k; i <= n; ++i)
#define REPR(i, k, n) for (int i = k; i >= n; --i)

#define LSOne(S) (S & (-S))
#define isBitSet(S, i) ((S >> i) & 1)

const int max_n = 1e6 + 9;

void solve() {
  int N;
  cin >> N;
  //    vi v;
  //    rep(i, N) {
  //        int x;
  //        cin >> x;
  //        v.push_back(x);
  //    }

  vector<int> D(max_n);

  for (ll i = 2; i <= 100000; i++) {
    if (!D[i]) {
      D[i] = i;
      for (ll j = i * i; j <= 100000; j += i) {
        if (!D[j]) {
          D[j] = i;
        }
      }
    }
  }

  int check = 1;
  bitset<max_n> f;
  vi v2;
  auto fail = [&](int x) {
    v2.clear();
    while (x > 1) {
      int t = D[x];
      if (f[t])
        return 1;
      v2.emplace_back(t);
      x /= t;
    }
    for (auto p : v2)
      f[p] = true;
    return 0;
  };

  int d = 0;
  rep(i, N) {
    int a;
    cin >> a;
    d = __gcd(d, a);
    if (fail(a))
      check = 0;
    //        cout << i << " " << check << endl;
  }

  if (check) {
    cout << "pairwise coprime";
  } else if (d == 1) {
    cout << "setwise coprime";
  } else {
    cout << "not coprime";
  }
}

// #define LOCAL 1

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);

#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  solve();

  return 0;
}