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

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

int prime[1000000 + 1];
int seen[1000000 + 1] = {0};
int arr[1000000];

void SieveOfEratosthenes(int n) {
  // Create a boolean array "prime[0..n]" and initialize
  // all entries it as true. A value in prime[i] will
  // finally be false if i is Not a prime, else true.

  prime[1] = 2;
  for (int p = 2; p <= n; p++)
    prime[p] = p;
  for (int p = 2; p * p <= n; p++) {
    // If prime[p] is not changed, then it is a prime
    if (prime[p] == p) {
      // Update all multiples of p greater than or
      // equal to the square of it
      // numbers which are multiple of p and are
      // less than p^2 are already been marked.
      for (int i = p * p; i <= n; i += p)
        if (prime[i] == i)
          prime[i] = p;
    }
  }
  // for(int i=2; i<=100 ;i++)
  // cout<<i<<" "<<prime[i]<<" ";
}

int main() {
  SieveOfEratosthenes(1000000);

  int n;
  string answer;
  bool ans = true;
  cin >> n;
  int i, j;
  int gc = 0;
  unordered_set<int> st;
  for (i = 0; i < n; i++) {
    cin >> arr[i];
    gc = __gcd(gc, arr[i]);
  }

  for (i = 0; i < n; i++) {
    if (arr[i] == 1)
      continue;
    while (arr[i] != 1) {
      int pf = prime[arr[i]];
      if (seen[prime[arr[i]]] == 1) {
        ans = false;
        break;
      }
      seen[prime[arr[i]]] = 1;
      // cout<<arr[i]<<" "<<prime[arr[i]]<<endl;
      while (arr[i] % pf == 0) {
        arr[i] /= pf;
        // cout<<arr[i]<<" "<<prime[arr[i]]<<endl;
      }
    }
    if (ans == false)
      break;
  }
  if (ans == true)
    answer = "pairwise coprime\\n";
  else if (gc == 1)
    answer = "setwise coprime\\n";
  else
    answer = "not coprime\\n";
  cout << answer;
}


Error: -8

Fixed submission:

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

int prime[1000000 + 1];
int seen[(int)1e6 + 1] = {0};
int arr[1000000];

template <typename S, typename T>
void erase_if(S &container, T first, T last, std::function<bool(T)> checker) {
  while (first != last) {
    if (checker(first)) {
      first = container.erase(first);
    } else
      first++;
  }
}
void SieveOfEratosthenes(int n) {
  // Create a boolean array "prime[0..n]" and initialize
  // all entries it as true. A value in prime[i] will
  // finally be false if i is Not a prime, else true.

  for (int p = 2; p <= n; p++)
    prime[p] = p;
  for (int p = 2; p * p <= n; p++) {
    // If prime[p] is not changed, then it is a prime
    if (prime[p] == p) {
      // Update all multiples of p greater than or
      // equal to the square of it
      // numbers which are multiple of p and are
      // less than p^2 are already been marked.
      for (int i = p * p; i <= n; i += p)
        prime[i] = p;
    }
  }
  // for(int i=2; i<=100 ;i++)
  // cout<<i<<" "<<prime[i]<<" ";
}

int main() {

  SieveOfEratosthenes(1000000);

  int n;
  string answer;
  bool ans = true;
  cin >> n;
  int i, j;
  int gc = 0;
  unordered_set<int> st;
  for (i = 0; i < n; i++) {
    cin >> arr[i];
    gc = __gcd(gc, arr[i]);
  }

  for (i = 0; i < n; i++) {
    if (arr[i] == 1)
      continue;
    while (arr[i] != 1) {
      if (seen[prime[arr[i]]] == 1) {
        ans = false;
        break;
      }
      seen[prime[arr[i]]] = 1;
      while (arr[i] % prime[arr[i]] == 0)
        arr[i] /= prime[arr[i]];
    }
    if (ans == false)
      break;
  }
  if (ans == true)
    answer = "pairwise coprime\\n";
  else if (gc == 1)
    answer = "setwise coprime\\n";
  else
    answer = "not coprime\\n";
  cout << answer;
}