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

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

const int N = 1000100;
int prime[N];
int a[1000100] = {0};
int cnt = 1;
bool vis[N];

void prepare(int n) {
  for (int i = 2; i <= n; ++i) {
    if (vis[i] == 0) {
      prime[cnt] = i;
      cnt++;
    }
    for (int j = 1; j < cnt; ++j) {
      if (i * prime[j] > n)
        break;
      vis[i * prime[j]] = 1;
      if (i * prime[j] == 0)
        break;
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  prepare(N);
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    int x;
    cin >> x;
    int ex = x;
    if (vis[x] == 0)
      a[x]++;
    for (int i = 1; i < cnt; ++i) {
      if (prime[i] * prime[i] <= x && x % prime[i] == 0) {
        a[prime[i]]++;
        while (x % prime[i] == 0)
          x /= prime[i];
      }
      if (prime[i] * prime[i] > x)
        break;
    }
    if (vis[x] == 0 && x != ex)
      a[x]++;
  }
  int flag = 0, flagn = 0;
  //	for(int i = 1;i<=15;++i) cout<<a[i]<<" ";
  //	cout<<endl;

  for (int i = 2; i < 1000100; i++) {
    if (a[i] >= 2)
      flag++;
    if (a[i] == n)
      flagn++;
  }

  if (flag == 0)
    cout << "pairwise coprime" << endl;
  else if (flagn == 0)
    cout << "setwise coprime" << endl;
  else
    cout << "not coprime" << endl;
  //	for(int i = 1;i<cnt;++i)  cout<<prime[i]<<endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define soort(v) sort(v.begin(), v.end())
#define foor(i, n) for (int i = 0; i < n; i++)
#define vecprint(v)                                                            \\
  {                                                                            \\
    for (int i = 0; i < v.size(); i++)                                         \\
      cout << v[i] << " ";                                                     \\
  }
#define arrprint(v, n)                                                         \\
  {                                                                            \\
    for (int i = 0; i < n; i++)                                                \\
      cout << v[i] << " ";                                                     \\
  }
long long int gcd(long long int a, long long int b) {
  if (a % b == 0)
    return b;
  return gcd(b, a % b);
}
long long int lcm(long long int a, long long int b) {
  return (a * b) / gcd(a, b);
}

int arr[20];
bool flag1 = false;

vector<long long> primes;
void SieveOfEratosthenes(int n = 1000000) {

  vector<bool> prime(n + 1, true);

  for (int p = 2; p * p <= n; p++) {

    if (prime[p] == true) {

      for (int i = p * p; i <= n; i += p)
        prime[i] = false;
    }
  }

  for (int p = 2; p <= n; p++)
    if (prime[p])
      primes.push_back(p);
}

void trial_division4(long long n) {
  vector<long long> factorization;
  map<int, int> mp;
  for (long long d : primes) {
    if (d * d > n)
      break;
    while (n % d == 0) {
      // factorization.push_back(d);
      // cout << d;
      if (mp[d] == 0) {
        if (arr[d] == 1) {
          flag1 = true;
          break;
        }
        arr[d] = 1;
        mp[d] = 1;
      }
      n /= d;
    }
  }
  if (n > 1) {
    if (mp[n] == 0) {
      if (arr[n] == 1) {
        flag1 = true;
      }
      arr[n] = 1;
      mp[n] = 1;
    }
  }
  // return factorization;
}

int main() {
  long long n, m;
  bool flag = false;
  cin >> n;
  SieveOfEratosthenes();
  vector<long long> v;
  v.resize(n);

  for (auto &it : v)
    cin >> it;

  m = __gcd(v[0], v[1]);

  trial_division4(v[0]);
  // cout << flag1 << v[0] << endl;
  // arrprint(arr,17);
  trial_division4(v[1]);

  if (m > 1)
    flag = true;

  for (int i = 2; i < n; i++) {
    trial_division4(v[i]);
    m = __gcd(m, v[i]);
    if (m > 1)
      flag = true;
  }
  if (!flag1)
    cout << "pairwise coprime" << endl;
  else if (m > 1) {
    cout << "not coprime" << endl;
  } else
    cout << "setwise coprime" << endl;
  // else
  // cout << ""

  return 0;
}