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

#include <bits/stdc++.h>
using namespace std;

#define int long long
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ms(a, b) memset((a), (b), sizeof(a))
#define len(a) (int)((a).size())
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define lb lower_bound
#define ub upper_bound
#define mine min_element
#define maxe max_element
#define fore(i, a, b) for (int i = (a), _b = (b); i < _b; ++i)
#define fort(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define ford(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)
#define bug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <class T> void __f(const char *name, T &&a) {
  cerr << name << ": " << a << '\\n';
}
template <class T, class... Ts> void __f(const char *names, T &&a, Ts &&...b) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << a << " /";
  __f(comma + 1, b...);
}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <class T> bool mini(T &a, const T &b) {
  return a > b ? (a = b, 1) : 0;
}
template <class T> bool maxi(T &a, const T &b) {
  return a < b ? (a = b, 1) : 0;
}

typedef long long ll;
typedef double db;
typedef pair<int, int> pi;
typedef pair<int, pi> ti;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pi> vpi;
typedef vector<ti> vti;
typedef vector<vpi> vvpi;

const int inf = (int)0x3f3f3f3f3f3f3f3f;
const int ninf = (int)0xc0c0c0c0c0c0c0c0;
const int M = (int)1e9 + 7;
const int N = 1e6;

int n, a[N + 3], p[N + 3], GCD = -1;
bool vis[N + 3];
vi pr;

void sieve() {
  fort(i, 2, N) {
    if (!p[i]) {
      p[i] = i;
      pr.pb(i);
    }
    for (int j = 0; j < len(pr) && i * pr[j] <= N; ++j) {
      p[i * pr[j]] = pr[j];
      if (i % pr[j] == 0)
        break;
    }
  }
}

signed main() {
  //    freopen("CP.inp", "r", stdin);
  //    freopen("CP.out", "w", stdout);
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  fort(i, 1, n) {
    cin >> a[i];
    if (GCD == -1)
      GCD = a[i];
    else
      GCD = __gcd(GCD, a[i]);
  }
  sieve();
  fort(i, 1, n) {
    while (a[i] != 1) {
      int tmp = p[a[i]];
      while (a[i] % tmp == 0)
        a[i] /= tmp;
      if (vis[tmp] == 1) {
        if (GCD == 1)
          cout << "setwise coprime\\n";
        else
          cout << "not coprime\\n";
        return 0;
      }
      vis[tmp] = 1;
    }
  }
  cout << "pairwise coprime\\n";
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define int long long
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ms(a, b) memset((a), (b), sizeof(a))
#define len(a) (int)((a).size())
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define lb lower_bound
#define ub upper_bound
#define mine min_element
#define maxe max_element
#define fore(i, a, b) for (int i = (a), _b = (b); i < _b; ++i)
#define fort(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define ford(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)
#define bug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <class T> void __f(const char *name, T &&a) {
  cerr << name << ": " << a << '\\n';
}
template <class T, class... Ts> void __f(const char *names, T &&a, Ts &&...b) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << a << " /";
  __f(comma + 1, b...);
}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <class T> bool mini(T &a, const T &b) {
  return a > b ? (a = b, 1) : 0;
}
template <class T> bool maxi(T &a, const T &b) {
  return a < b ? (a = b, 1) : 0;
}

typedef long long ll;
typedef double db;
typedef pair<int, int> pi;
typedef pair<int, pi> ti;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pi> vpi;
typedef vector<ti> vti;
typedef vector<vpi> vvpi;

const int inf = (int)0x3f3f3f3f3f3f3f3f;
const int ninf = (int)0xc0c0c0c0c0c0c0c0;
const int M = (int)1e9 + 7;
const int N = 1e3;

int n, a[N], p[N], GCD = -1;
bool vis[N];
vi pr;

void sieve() {
  fort(i, 2, N) {
    if (!p[i]) {
      p[i] = i;
      pr.pb(i);
    }
    for (int j = 0; j < len(pr) && i * pr[j] <= N; ++j) {
      p[i * pr[j]] = pr[j];
      if (i % pr[j] == 0)
        break;
    }
  }
}

signed main() {
  //    freopen("CP.inp", "r", stdin);
  //    freopen("CP.out", "w", stdout);
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  fort(i, 1, n) {
    cin >> a[i];
    if (GCD == -1)
      GCD = a[i];
    else
      GCD = __gcd(GCD, a[i]);
  }
  sieve();
  fort(i, 1, n) {
    while (a[i] != 1) {
      int tmp = p[a[i]];
      while (a[i] % tmp == 0)
        a[i] /= tmp;
      if (vis[tmp] == 1) {
        if (GCD == 1)
          cout << "setwise coprime\\n";
        else
          cout << "not coprime\\n";
        return 0;
      }
      vis[tmp] = 1;
    }
  }
  cout << "pairwise coprime\\n";
}