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)

const int max_n = 1e6 + 9;
const int nax = 1e6 + 1;

void solve() {
  vector<int> spf(nax);
  for (ll i = 2; i < nax; ++i) {
    if (!spf[i]) {
      spf[i] = i;
      for (ll j = i * i; j < nax; j += i) {
        if (!spf[j])
          spf[j] = i;
      }
    }
  }
  int n;
  cin >> n;
  int pc = 1, sc = 0;
  bitset<nax> f;
  vector<int> v;
  auto fail = [&](int x) {
    v.clear();
    while (x > 1) {
      int t = spf[x];
      // cout << t << ' ';
      if (f[t])
        return 1;
      v.emplace_back(t);
      x /= t;
    }
    for (auto p : v)
      f[p] = 1;
    return 0;
  };
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    int a;
    cin >> a;
    // cout << i << ": ";
    if (fail(a))
      pc = 0;
    sc = __gcd(sc, a);
    // cout << '\\n';
  }
  if (pc)
    cout << "pairwise coprime\\n";
  else if (sc == 1)
    cout << "setwise coprime\\n";
  else
    cout << "not coprime\\n";
}

// #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;
        }
      }
    }
  }

  //    for (int i = 0; i < N && check; i++) {
  //        int x = v[i];
  //        while (D[x] > 1) {
  //            int f = D[x];
  //            cnt[f]++;
  //            if (cnt[f] > 1) {
  //                check = 0;
  //                break;
  //            }
  //            while (x % f == 0) {
  //                x /= f;
  //            }
  //        }
  //    }
  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;
  };

  rep(i, N) {
    if (fail(v[i]))
      check = 0;
  }

  if (check) {
    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;
}