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

/* Simplicity and Goodness */

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>

using namespace std;
// using namespace __gnu_pbds;
// typedef tree<int, null_type, less<int>, rb_tree_tag,
// tree_order_statistics_node_update> indexed_set;

void my_dbg() { cout << endl; }
template <typename Arg, typename... Args> void my_dbg(Arg A, Args... B) {
  cout << ' ' << A;
  my_dbg(B...);
}
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", my_dbg(__VA_ARGS__)

#define scn(n) scanf("%d", &n)
#define lscn(n) scanf("%lld", &n)
#define pri(n) printf("%d ", (int)(n))
#define prin(n) printf("%d\\n", (int)(n))
#define lpri(n) printf("%lld ", n)
#define lprin(n) printf("%lld\\n", n)
#define rep(i, a, b) for (int i = a; i < (int)b; i++)
#define pb push_back
#define mp make_pair
#define F first
#define S second

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;

const int inf = INT_MAX;
const int ninf = INT_MIN;
const int mod = 1e9 + 7;
const int maxN = 1e6 + 2;

vi find_prime(int val) {
  vi ret;

  for (int i = 2; i * i <= val; i++) {
    if (val % i == 0) {
      ret.pb(i);
      while (val % i == 0)
        val /= i;
    }
  }

  if (val > 1)
    ret.pb(val);

  return ret;
}

void solve() {
  int n;
  scn(n);

  vi a(n), fa(maxN, 0);

  bool pairwise = 1;

  rep(i, 0, n) {
    scn(a[i]);

    vi here = find_prime(a[i]);

    for (int it : here) {
      if (fa[it] != 0)
        pairwise = 0;
      fa[it] = 1;
    }
  }

  if (pairwise) {
    puts("pairwise coprime");
    exit(0);
  }

  int g = a[0];

  rep(i, 1, n) g = __gcd(g, a[i]);

  if (g == 1) {
    puts("setwise coprime");
    exit(0);
  }

  puts("not coprime");
}

int main() {
  int t = 1;
  // scn(t);

  while (t--) {
    solve();
  }
  return 0;
}


Error: 0

Fixed submission:

/* Simplicity and Goodness */

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>

using namespace std;
// using namespace __gnu_pbds;
// typedef tree<int, null_type, less<int>, rb_tree_tag,
// tree_order_statistics_node_update> indexed_set;

void my_dbg() { cout << endl; }
template <typename Arg, typename... Args> void my_dbg(Arg A, Args... B) {
  cout << ' ' << A;
  my_dbg(B...);
}
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", my_dbg(__VA_ARGS__)

#define scn(n) scanf("%d", &n)
#define lscn(n) scanf("%lld", &n)
#define pri(n) printf("%d ", (int)(n))
#define prin(n) printf("%d\\n", (int)(n))
#define lpri(n) printf("%lld ", n)
#define lprin(n) printf("%lld\\n", n)
#define rep(i, a, b) for (int i = a; i < (int)b; i++)
#define pb push_back
#define mp make_pair
#define F first
#define S second

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;

const int inf = INT_MAX;
const int ninf = INT_MIN;
const int mod = 1e9 + 7;
const int maxN = 2e5 + 2;

vi find_prime(int val) {
  vi ret;

  for (int i = 2; i * i <= val; i++) {
    if (val % i == 0) {
      ret.pb(i);
      while (val % i == 0)
        val /= i;
    }
  }

  if (val > 1)
    ret.pb(val);

  return ret;
}

void solve() {
  int n;
  scn(n);

  vi a(n), fa(maxN, 0);

  bool pairwise = 1;

  rep(i, 0, n) {
    scn(a[i]);

    vi here = find_prime(a[i]);

    for (int it : here) {
      if (fa[it] != 0)
        pairwise = 0;
      fa[it] = 1;
    }
  }

  if (pairwise) {
    puts("pairwise coprime");
    exit(0);
  }

  int g = a[0];

  rep(i, 1, n) g = __gcd(g, a[i]);

  if (g == 1) {
    puts("setwise coprime");
    exit(0);
  }

  puts("not coprime");
}

int main() {
  int t = 1;
  // scn(t);

  while (t--) {
    solve();
  }
  return 0;
}