Problem p02574 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using ll = long long;
#define FOR(i, k, n) for (ll i = (k); i < (n); i++)
#define FORe(i, k, n) for (ll i = (k); i <= (n); i++)
#define FORr(i, k, n) for (ll i = (k)-1; i > (n); i--)
#define FORre(i, k, n) for (ll i = (k)-1; i >= (n); i--)
#define REP(i, n) FOR(i, 0, n)
#define REPr(i, n) FORre(i, n, 0)
#define ALL(x) (x).begin(), (x).end()
#define ALLr(x) (x).rbegin(), (x).rend()
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
using namespace std;

const int INF = 1001001001;

struct Sieve {
  ll n;
  vector<ll> f, primes;
  Sieve(ll n = 1) : n(n), f(n + 1, 0) {
    f[0] = f[1] = -1;
    for (ll i = 2; i <= n; i++) {
      if (f[i])
        continue;
      primes.emplace_back(i);
      f[i] = i;
      for (ll j = i * i; j <= n; j += i)
        if (!f[j])
          f[j] = i;
    }
  }
  bool isPrime(const ll x) { return f[x] == x; }
  vector<ll> factorList(ll x) {
    vector<ll> res;
    while (x != 1) {
      res.emplace_back(f[x]);
      x /= f[x];
    }
    return res;
  }
  vector<pair<ll, ll>> factor(const ll x) {
    vector<ll> fl = factorList(x);
    if (fl.size() == 0)
      return {};
    vector<pair<ll, ll>> res(1, pair<ll, ll>(fl[0], 0));
    for (ll p : fl) {
      if (res.back().first == p)
        res.back().second++;
      else
        res.emplace_back(pair<ll, ll>(p, 1));
    }
    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; }

int main(void) {
  ll n;
  cin >> n;
  vector<ll> a(n);
  REP(i, n) cin >> a[i];
  Sieve s(100000);
  map<ll, ll> mp;
  REP(i, n) {
    auto f = s.factor(a[i]);
    for (auto p : f) {
      mp[p.first]++;
    }
  }
  bool pc = true;
  for (auto p : mp) {
    if (p.second > 1)
      pc = false;
  }
  if (pc) {
    cout << "pairwise coprime" << endl;
    return 0;
  }
  ll g = gcd(a[0], a[1]);
  REP(i, n - 2) g = gcd(g, a[i + 2]);
  if (g == 1)
    cout << "setwise coprime" << endl;
  else
    cout << "not coprime" << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using ll = long long;
#define FOR(i, k, n) for (ll i = (k); i < (n); i++)
#define FORe(i, k, n) for (ll i = (k); i <= (n); i++)
#define FORr(i, k, n) for (ll i = (k)-1; i > (n); i--)
#define FORre(i, k, n) for (ll i = (k)-1; i >= (n); i--)
#define REP(i, n) FOR(i, 0, n)
#define REPr(i, n) FORre(i, n, 0)
#define ALL(x) (x).begin(), (x).end()
#define ALLr(x) (x).rbegin(), (x).rend()
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
using namespace std;

const int INF = 1001001001;

struct Sieve {
  ll n;
  vector<ll> f, primes;
  Sieve(ll n = 1) : n(n), f(n + 1, 0) {
    f[0] = f[1] = -1;
    for (ll i = 2; i <= n; i++) {
      if (f[i])
        continue;
      primes.emplace_back(i);
      f[i] = i;
      for (ll j = i * i; j <= n; j += i)
        if (!f[j])
          f[j] = i;
    }
  }
  bool isPrime(const ll x) { return f[x] == x; }
  vector<ll> factorList(ll x) {
    vector<ll> res;
    while (x != 1) {
      res.emplace_back(f[x]);
      x /= f[x];
    }
    return res;
  }
  vector<pair<ll, ll>> factor(const ll x) {
    vector<ll> fl = factorList(x);
    if (fl.size() == 0)
      return {};
    vector<pair<ll, ll>> res(1, pair<ll, ll>(fl[0], 0));
    for (ll p : fl) {
      if (res.back().first == p)
        res.back().second++;
      else
        res.emplace_back(pair<ll, ll>(p, 1));
    }
    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; }

int main(void) {
  ll n;
  cin >> n;
  vector<ll> a(n);
  REP(i, n) cin >> a[i];
  Sieve s(1000000);
  map<ll, ll> mp;
  REP(i, n) {
    auto f = s.factor(a[i]);
    for (auto p : f) {
      mp[p.first]++;
    }
  }
  bool pc = true;
  for (auto p : mp) {
    if (p.second > 1)
      pc = false;
  }
  if (pc) {
    cout << "pairwise coprime" << endl;
    return 0;
  }
  ll g = gcd(a[0], a[1]);
  REP(i, n - 2) g = gcd(g, a[i + 2]);
  if (g == 1)
    cout << "setwise coprime" << endl;
  else
    cout << "not coprime" << endl;
  return 0;
}