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

#include <bits/stdc++.h>
using namespace std;
#define gc getchar_unlocked
#define fo(i, n) for (i = 0; i < n; i++)
#define Fo(i, k, n) for (i = k; k < n ? i < n : i > n; k < n ? i += 1 : i -= 1)
#define ll long long
#define si(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define ss(s) scanf("%s", s)
#define pi(x) printf("%d\\n", x)
#define pl(x) printf("%lld\\n", x)
#define ps(s) printf("%s\\n", s)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for (auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
typedef vector<pl> vpl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
int mpow(int base, int exp);
void ipgraph(int m);
void dfs(int u, int par);
const int mod = 1000000007;
const int N = 3e6, M = N;
//=======================

vi g[N];
int a[N];

int gcd(int a, int b) {
  if (a == 0) {
    return b;
  }
  return gcd(b % a, a);
}

void solve() {
  int i, j, k, g, n;
  cin >> n;
  set<int> s;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  g = a[0];
  for (i = 0; i < n; i++) {
    g = gcd(g, a[i]);
  }
  if (g != 1) {
    cout << "not coprime" << endl;
    return;
  }
  for (j = 0; j < n; j++) {
    vector<int> prime_factors;
    for (i = 2; i * i <= a[j]; i++) {
      if (a[j] % i == 0) {
        prime_factors.pb(i);
        while (a[j] % i == 0) {
          a[j] /= i;
        }
      }
    }
    if (a[j] != 1) {
      prime_factors.pb(a[j]);
    }
    for (auto factor : prime_factors) {
      if (s.find(factor) != s.end()) {
        cout << "setwise coprime" << endl;
        return;
      }
      s.insert(factor);
    }
  }
  cout << "pairwise coprime" << endl;
}

int main() {
  ios_base::sync_with_stdio(0);
  int i, t = 1;
  // cin>>t;
  for (i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}

int mpow(int base, int exp) {
  base %= mod;
  int result = 1;
  while (exp > 0) {
    if (exp & 1)
      result = ((ll)result * base) % mod;
    base = ((ll)base * base) % mod;
    exp >>= 1;
  }
  return result;
}

void ipgraph(int n, int m) {
  int i, u, v;
  while (m--) {
    cin >> u >> v;
    g[u - 1].pb(v - 1);
    g[v - 1].pb(u - 1);
  }
}

void dfs(int u, int par) {
  for (int v : g[u]) {
    if (v == par)
      continue;
    dfs(v, u);
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define gc getchar_unlocked
#define fo(i, n) for (i = 0; i < n; i++)
#define Fo(i, k, n) for (i = k; k < n ? i < n : i > n; k < n ? i += 1 : i -= 1)
#define ll long long
#define si(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define ss(s) scanf("%s", s)
#define pi(x) printf("%d\\n", x)
#define pl(x) printf("%lld\\n", x)
#define ps(s) printf("%s\\n", s)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for (auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
typedef vector<pl> vpl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
int mpow(int base, int exp);
void ipgraph(int m);
void dfs(int u, int par);
const int mod = 1000000007;
const int N = 3e5, M = N;
//=======================

vi g[N];
int a[N];

void solve() {
  int i, j, k, p, q, r, x, y, u, v, n, m = 0, c = 0, pair = 1, set = 1;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    m = max(a[i], m);
  }
  for (i = 2; i <= sqrt(m); i++) {
    c = 0;
    for (j = 0; j < n; j++) {
      if (a[j] % i == 0) {
        c++;
      }
    }
    if (c > 1) {
      pair = 0;
    }
    if (c == n) {
      set = 0;
      break;
    }
  }
  if (pair) {
    cout << "pairwise coprime" << endl;
    return;
  }
  if (set) {
    cout << "setwise coprime" << endl;
    return;
  }
  cout << "not coprime" << endl;
}

int main() {
  ios_base::sync_with_stdio(0);
  int i, t = 1;
  // cin>>t;
  for (i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}

int mpow(int base, int exp) {
  base %= mod;
  int result = 1;
  while (exp > 0) {
    if (exp & 1)
      result = ((ll)result * base) % mod;
    base = ((ll)base * base) % mod;
    exp >>= 1;
  }
  return result;
}

void ipgraph(int n, int m) {
  int i, u, v;
  while (m--) {
    cin >> u >> v;
    g[u - 1].pb(v - 1);
    g[v - 1].pb(u - 1);
  }
}

void dfs(int u, int par) {
  for (int v : g[u]) {
    if (v == par)
      continue;
    dfs(v, u);
  }
}