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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define int long long int
#define mod 1000000007
#define string_mod 2549536629329_base_255
#define pb push_back
#define F first
#define S second
#define ff first
#define endl "\\n"
#define ss second
#define all(v) v.begin(), v.end()
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
int power(int x, unsigned int y, int p) {
  int res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res % p;
}
int lcm(int a, int b) { return a / __gcd(a, b) * b; }
int spf[1000001];
void sieve(int n) {
  for (int i = 0; i <= n; i++) {
    spf[i] = i;
  }
  for (int i = 2; i * i <= n; i++) {
    if (spf[i] == i) {
      for (int j = i * i; j < n; j += i) {
        if (spf[j] == j) {
          spf[j] = i;
        }
      }
    }
  }
}
int mp[1000001];
void solve() {
  sieve(1000000);
  int n;
  cin >> n;
  int a[n];
  int f = 1;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    int j = a[i];

    while (j != 1) {
      if (mp[spf[j]] == 1) {
        f = 0;
      }
      mp[spf[j]] = 1;
      int c = spf[j];
      while (j != 1 && j % c == 0) {
        j /= spf[j];
      }
    }
  }
  if (f) {
    cout << "pairwise coprime" << endl;
    return;
  }
  int g = a[0];
  for (int i = 1; i < n; i++) {
    g = __gcd(g, a[i]);
  }
  if (g == 1) {
    cout << "setwise coprime" << endl;
    return;
  }
  cout << "not coprime" << endl;
  return;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int tc;
  tc = 1;
  // cin >> tc;
  while (tc--) {
    solve();
  }

  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define int long long int
#define mod 1000000007
#define string_mod 2549536629329_base_255
#define pb push_back
#define F first
#define S second
#define ff first
#define endl "\\n"
#define ss second
#define all(v) v.begin(), v.end()
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
int power(int x, unsigned int y, int p) {
  int res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res % p;
}
int lcm(int a, int b) { return a / __gcd(a, b) * b; }
int spf[1000001];
void sieve(int n) {
  for (int i = 0; i <= n; i++) {
    spf[i] = i;
  }
  for (int i = 2; i * i <= n; i++) {
    if (spf[i] == i) {
      for (int j = i * i; j <= n; j += i) {
        if (spf[j] == j) {
          spf[j] = i;
        }
      }
    }
  }
}
int mp[1000001];
void solve() {
  sieve(1000000);
  int n;
  cin >> n;
  int a[n];
  int f = 1;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    int j = a[i];

    while (j != 1) {
      //   cerr << spf[j] << " ";
      if (mp[spf[j]] == 1) {
        f = 0;
      }
      mp[spf[j]] = 1;
      while (j != 1 && j % spf[j] == 0) {
        j /= spf[j];
      }
    }
  }
  if (f) {
    cout << "pairwise coprime" << endl;
    return;
  }
  int g = a[0];
  for (int i = 1; i < n; i++) {
    g = __gcd(g, a[i]);
  }
  if (g == 1) {
    cout << "set coprime" << endl;
    return;
  }
  cout << "not coprime" << endl;
  return;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int tc;
  tc = 1;
  cin >> tc;
  while (tc--) {
    solve();
  }

  return 0;
}