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

#include <bits/stdc++.h>
using namespace std;
#define pr(args...)                                                            \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
  }
#define all(v) (v).begin(), (v).end()
#define int long long
#define sz(v) (int)(v).size()
#define mod 1000000007 // ((a- b)% mod+ mod)% mod

void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
}

const int N = 1000001;
vector<int> p;
bitset<10000001> b;

void sevie() {
  b[0] = b[1] = 1;
  for (int i = 2; (i * i) <= N; i++) {
    if (!b[i]) {
      for (int j = i * 2; j <= N; j += i)
        b[j] = 1;
    }
  }
  for (int i = 2; i <= N; i++)
    if (!b[i])
      p.push_back(i);
}

map<int, int> mp;

bool prime_factor(int nm) {
  int pf = p[0], c = 0;
  while (pf * pf <= nm) {
    if (nm % pf == 0 && mp.count(pf))
      return 1;
    else if (nm % pf == 0) {
      mp[pf]++;
    }
    while (nm % pf == 0) {
      nm /= pf;
    }
    pf = p[++c];
  }
  if (nm > 1) {
    if (mp.count(nm))
      return 1;
    else
      mp[nm]++;
  }
  return 0;
}

void solve() {
  sevie();

  int n;
  cin >> n;
  int gc = 0;

  vector<int> v(n);

  for (int i = 0; i < n; i++) {
    cin >> v[i];
    gc = __gcd(gc, v[i]);
  }

  if (gc != 1) {
    cout << "not coprime\\n";
  } else {
    for (int i = 0; i < n; i++) {
      if (prime_factor(v[i])) {
        cout << "setwise coprime\\n";
        return;
      }
    }
    cout << "pairwise coprime\\n";
  }
}

int32_t main() {
  ios::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);
  int T = 1;
  // cin >> T;
  while (T--) {
    solve();
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define pr(args...)                                                            \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
  }
#define all(v) (v).begin(), (v).end()
#define int long long
#define sz(v) (int)(v).size()
#define mod 1000000007 // ((a- b)% mod+ mod)% mod

void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
}

void solve() {
  int n;
  cin >> n;
  int gc = 0;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
    gc = __gcd(gc, v[i]);
  }
  if (gc != 1) {
    cout << "not coprime\\n";
  } else {
    map<int, int> mp;
    auto calc = [&](int nm) -> bool {
      for (int i = 2; i <= nm; i++) {
        if (nm % i == 0 && mp.count(i)) {
          return 1;
        } else {
          if (nm % i == 0) {
            mp[i]++;
            while (nm % i == 0)
              nm /= i;
          }
        }
      }
      if (nm > 1) {
        if (mp.count(nm))
          return 1;
        else
          mp[nm]++;
      }
      return 0;
    };

    for (int i = 0; i < n; i++) {
      if (calc(v[i])) {
        cout << "setwise coprime\\n";
        exit(1);
      }
    }
    cout << "pairwise coprime\\n";
  }
}

int32_t main() {
  ios::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);
  int T = 1;
  // cin >> T;
  while (T--) {
    solve();
  }
  return 0;
}