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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
const ll mod = 1e9 + 7;
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
ll gcd2(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  ll xx, yy;
  ll res = gcd2(b, a % b, xx, yy);
  x = yy;
  y = xx - yy * (a / b);
  return res;
}

// ll nCr(ll n, ll r) {
//     return ( ( ( (fac[n] * powmod(fac[n-r], mod-2)) % mod ) * powmod(fac[r],
//     mod-2) ) % mod );
// }
// const ll sz = 1e6 + 5;
// vector<ll> fac(sz, 1);

// * ALways PASS arrays, strings etc to functions BY REFERENCE

set<int> ps;

bool func(int val) {
  int x = val;
  for (int i = 2; i * i <= val; i++) {
    if (x % i != 0)
      continue;

    if (ps.count(i))
      return false;
    else
      ps.insert(i);

    while (x % i == 0)
      x /= i;
  }

  if (x != 1) {
    if (ps.count(x))
      return false;
    else
      ps.insert(x);
  }

  return true;
}

int main() {
  // for (int i = 1; i < sz; i++)
  //     fac[i] = (fac[i-1] * i) % mod;

  int t = 1;
  // cin >> t;

  while (t--) {
    int n;
    cin >> n;

    vector<int> arr(n);
    for (auto &x : arr)
      cin >> x;

    bool f = true;
    for (int i = n - 1; i >= 0; i--) {
      if (!func(arr[i])) {
        f = false;
        break;
      }
    }

    if (f) {
      cout << "pairwise coprime" << endl;
      return 0;
    }

    int g = arr[0];
    for (int i = 1; i < n; i++)
      g = gcd(g, arr[i]);

    if (g == 1) {
      cout << "setwise coprime" << endl;
      return 0;
    }

    cout << "not coprime" << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
const ll mod = 1e9 + 7;
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
ll gcd2(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  ll xx, yy;
  ll res = gcd2(b, a % b, xx, yy);
  x = yy;
  y = xx - yy * (a / b);
  return res;
}

// ll nCr(ll n, ll r) {
//     return ( ( ( (fac[n] * powmod(fac[n-r], mod-2)) % mod ) * powmod(fac[r],
//     mod-2) ) % mod );
// }
// const ll sz = 1e6 + 5;
// vector<ll> fac(sz, 1);

// * ALways PASS arrays, strings etc to functions BY REFERENCE

int main() {
  // for (int i = 1; i < sz; i++)
  //     fac[i] = (fac[i-1] * i) % mod;

  int t = 1;
  // cin >> t;

  while (t--) {
    int n;
    cin >> n;

    vector<int> arr(n);
    for (auto &x : arr)
      cin >> x;

    vector<bitset<1005>> bs(n);
    for (int k = 0; k < n; k++) {
      int x = arr[k];
      for (int i = 2; i * i <= arr[k]; i++) {
        if (x % i != 0)
          continue;

        bs[k][i] = 1;
        while (x % i == 0)
          x /= i;
      }
      if (x != 1)
        bs[k][x] = 1;
    }

    vector<bitset<1005>> ors = bs;
    for (int i = n - 2; i >= 0; i--)
      ors[i] |= ors[i + 1];

    // for (auto x: ors)
    //     cout << x << endl;

    bool f = true;
    for (int i = 0; i < n - 1; i++) {
      if ((bs[i] & ors[i + 1]).any()) {
        f = false;
        break;
      }
    }

    if (f) {
      cout << "pairwise coprime" << endl;
      return 0;
    }

    int g = arr[0];
    for (int i = 1; i < n; i++)
      g = gcd(g, arr[i]);

    if (g == 1) {
      cout << "setwise coprime" << endl;
      return 0;
    }

    cout << "not coprime" << endl;
  }

  return 0;
}