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

#include <bits/stdc++.h>
using namespace std;
#define M ((int)1e9 + 7)
vector<int> 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.
  bool prime[n + 1];
  memset(prime, true, sizeof(prime));
  vector<int> an;
  for (int p = 2; p * p <= n; p++) {
    // If prime[p] is not changed, then it is a prime
    if (prime[p] == true) {
      // 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] = false;
    }
  }

  // Print all prime numbers
  for (int p = 2; p <= n; p++)
    if (prime[p])
      an.push_back(p); // cout << p << " ";
  return an;
}
int main() {
// your code goes here
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  int a[n], an = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    an = __gcd(an, a[i]);
  }
  if (an != 1) {
    cout << "not coprime";
    return 0;
  }
  bool vis[1000000];
  memset(vis, false, sizeof(vis));
  vector<int> pr = SieveOfEratosthenes(1000);
  bool pc = true;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < pr.size(); j++) {
      if (a[i] % pr[j])
        continue;
      if (vis[pr[j]])
        pc = false;
      else
        vis[pr[j]] = true;
      while (a[i] % pr[j] == 0)
        a[i] /= pr[j];
    }
    if (a[i] != 1) {
      if (vis[a[i]])
        pc = false;
      else
        vis[a[i]] = true;
    }
  }
  if (pc)
    cout << "pairwise coprime";
  else
    cout << "setwise coprime";
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define M ((int)1e9 + 7)
vector<int> 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.
  bool prime[n + 1];
  memset(prime, true, sizeof(prime));
  vector<int> an;
  for (int p = 2; p * p <= n; p++) {
    // If prime[p] is not changed, then it is a prime
    if (prime[p] == true) {
      // 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] = false;
    }
  }

  // Print all prime numbers
  for (int p = 2; p <= n; p++)
    if (prime[p])
      an.push_back(p); // cout << p << " ";
  return an;
}
int main() {
// your code goes here
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  int a[n], an = 0;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int vis[n];
  memset(vis, false, sizeof(vis));
  vector<int> pr = SieveOfEratosthenes(1000);
  bool pc = true;
  for (int i = 0; i < n; i++) {
    an = __gcd(an, a[i]);
    for (int j = 0; pr[j] * pr[j] <= a[i]; j++) {
      if (a[i] % pr[j])
        continue;
      if (vis[j])
        pc = false;
      else
        vis[j] = true;
    }
  }
  if (pc)
    cout << "pairwise coprime";
  else if (an == 1)
    cout << "setwise coprime";
  else
    cout << "not coprime";
  return 0;
}