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

#include <bits/stdc++.h>
using namespace std;

#define int long long
#define forn(i, a, n) for (int i = a; i < n; i++)

int n;
int a[1000001];
vector<int> v;
map<int, int> mp;
int isp[1000001];

void buildprime() {
  isp[0] = isp[1] = 1;
  forn(i, 2, 1000001) {
    if (isp[i])
      continue;
    for (int j = i + i; j < 1000001; j += i)
      isp[j] = 1;
  }
  forn(i, 2, 1000001) if (!isp[i]) v.push_back(i);
}

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

bool ispairwise() {
  vector<int> w;
  forn(i, 0, n) {
    int x = a[i];
    forn(i, 0, v.size()) {
      if (v[i] * v[i] > x)
        break;
      if (x % v[i] == 0) {
        mp[v[i]]++;
        w.push_back(v[i]);
        while (x % v[i] == 0) {
          x /= v[i];
        }
      }
    }
    if (x != 1 || x == i) {
      mp[x]++;
      w.push_back(x);
    }
  }
  sort(w.begin(), w.end());
  int mx = w[w.size() - 1];
  forn(i, 0, v.size()) {
    if (v[i] > mx)
      break;
    if (mp[v[i]] > 1)
      return false;
  }
  return true;
}

bool issetwise() {
  int res = a[0];
  forn(i, 0, n) { res = gcd(res, a[i]); }
  return res == 1;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);

  buildprime();
  cin >> n;
  forn(i, 0, n) cin >> a[i];
  bool isp = ispairwise();
  bool iss = issetwise();
  if (isp) {
    cout << "pairwise coprime" << endl;
    return 0;
  } else if (iss) {
    cout << "setwise coprime" << endl;
    return 0;
  }
  cout << "not coprime" << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define int long long
#define forn(i, a, n) for (int i = a; i < n; i++)

int n;
int a[1000001];
vector<int> v;
map<int, int> mp;
int isp[1000001];

void buildprime() {
  isp[0] = isp[1] = 1;
  forn(i, 2, 1000001) {
    if (isp[i])
      continue;
    for (int j = i + i; j < 1000001; j += i)
      isp[j] = 1;
  }
  forn(i, 2, 1000001) if (!isp[i]) v.push_back(i);
}

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

bool ispairwise() {
  vector<int> w;
  forn(i, 0, n) {
    int x = a[i];
    forn(i, 0, v.size()) {
      if (v[i] * v[i] > x)
        break;
      if (x % v[i] == 0) {
        mp[v[i]]++;
        w.push_back(v[i]);
        while (x % v[i] == 0) {
          x /= v[i];
        }
      }
    }
    if (x != 1) {
      mp[x]++;
      w.push_back(x);
    }
  }
  sort(w.begin(), w.end());
  int mx = w[w.size() - 1];
  forn(i, 0, v.size()) {
    if (v[i] > mx)
      break;
    if (mp[v[i]] > 1)
      return false;
  }
  return true;
}

bool issetwise() {
  int res = a[0];
  forn(i, 0, n) { res = gcd(res, a[i]); }
  return res == 1;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);

  buildprime();
  cin >> n;
  forn(i, 0, n) cin >> a[i];
  bool isp = ispairwise();
  bool iss = issetwise();
  if (isp) {
    cout << "pairwise coprime" << endl;
    return 0;
  } else if (iss) {
    cout << "setwise coprime" << endl;
    return 0;
  }
  cout << "not coprime" << endl;
  return 0;
}