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

// BISMILLAHIR RAHMANIR RAHIM
// RABBI ZIDNI ILMA
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ff first
#define ss second
#define endl "\\n"
#define inf (1LL << 61)
#define pi acos(-1)
#define mod 1000000007
#define all(x) x.begin(), x.end()
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cout << fixed << setprecision(10);
#define debug(x) cout << "[" << #x << ": " << x << "]\\n"
#define debug2(x, y)                                                           \\
  cout << "[" << #x << ": " << x << "]"                                        \\
       << "  [" << #y << ": " << y << "]\\n"
#define debug3(x, y, z)                                                        \\
  cout << "[" << #x << ": " << x << "]"                                        \\
       << "  [" << #y << ": " << y << "]"                                      \\
       << "  [" << #z << ": " << z << "]\\n"
#define debug4(x, y, z, k)                                                     \\
  cout << "[" << #x << ": " << x << "]"                                        \\
       << "  [" << #y << ": " << y << "]"                                      \\
       << "  [" << #z << ": " << z << "]"                                      \\
       << "  [" << #k << ": " << k << "]\\n"

const ll N = 1e5 + 9;

void genPrime(vector<ll> &prime, ll upto) {
  prime.push_back(2);
  vector<bool> yes(upto + 1, 1);
  for (ll i = 3; i * i <= upto; i += 2) {
    if (yes[i] == 1) {
      for (ll j = i * i; j <= upto; j += 2 * i)
        yes[j] = 0;
    }
  }
  for (ll i = 3; i <= upto; i += 2) {
    if (yes[i])
      prime.push_back(i);
  }
}

int main() {
  fastio vector<ll> primes;
  genPrime(primes, 1010);

  ll n;
  cin >> n;
  vector<ll> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  vector<bool> yes((int)(1e6 + 9));
  bool pairwise = 1;
  vector<ll> copy_a = a;
  for (int i = 0; i < n; i++) {
    for (ll p : primes) {
      if (a[i] % p == 0) {
        while (a[i] % p == 0) {
          a[i] /= p;
        }
        if (yes[p])
          pairwise = 0;
        yes[p] = 1;
      }
    }
    if (a[i] > 1) {
      if (yes[a[i]])
        pairwise = 0;
      yes[a[i]] = 1;
    }
  }
  if (pairwise) {
    cout << "pairwise coprime";
    return 0;
  }
  a = copy_a;
  bool setwise = 1;
  ll gcd = a[0];
  for (int i = 1; i < n; i++) {
    gcd = __gcd(gcd, a[i]);
    // debug(gcd);
  }
  if (gcd > 1)
    setwise = 0;
  if (setwise) {
    cout << "setwise coprime";
    return 0;
  }
  cout << "not coprime";
}


Error: WA

Fixed submission:

// BISMILLAHIR RAHMANIR RAHIM
// RABBI ZIDNI ILMA
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ff first
#define ss second
#define endl "\\n"
#define inf (1LL << 61)
#define pi acos(-1)
#define mod 1000000007
#define all(x) x.begin(), x.end()
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cout << fixed << setprecision(10);
#define debug(x) cout << "[" << #x << ": " << x << "]\\n"
#define debug2(x, y)                                                           \\
  cout << "[" << #x << ": " << x << "]"                                        \\
       << "  [" << #y << ": " << y << "]\\n"
#define debug3(x, y, z)                                                        \\
  cout << "[" << #x << ": " << x << "]"                                        \\
       << "  [" << #y << ": " << y << "]"                                      \\
       << "  [" << #z << ": " << z << "]\\n"
#define debug4(x, y, z, k)                                                     \\
  cout << "[" << #x << ": " << x << "]"                                        \\
       << "  [" << #y << ": " << y << "]"                                      \\
       << "  [" << #z << ": " << z << "]"                                      \\
       << "  [" << #k << ": " << k << "]\\n"

const ll N = 1e5 + 9;

void genPrime(vector<ll> &prime, ll upto) {
  prime.push_back(2);
  vector<bool> yes(upto + 1, 1);
  for (ll i = 3; i * i <= upto; i += 2) {
    if (yes[i] == 1) {
      for (ll j = i * i; j <= upto; j += 2 * i)
        yes[j] = 0;
    }
  }
  for (ll i = 3; i <= upto; i += 2) {
    if (yes[i])
      prime.push_back(i);
  }
}

int main() {
  fastio vector<ll> primes;
  genPrime(primes, 1010);

  ll n;
  cin >> n;
  vector<ll> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  vector<bool> yes(1010);
  bool pairwise = 1;
  for (int i = 0; i < n; i++) {
    bool isPrime = 1;
    for (ll p : primes) {
      if (a[i] % p == 0) {
        isPrime = 0;
        if (yes[p])
          pairwise = 0;
        yes[p] = 1;
      }
    }
    if (isPrime) {
      if (yes[a[i]])
        pairwise = 0;
      yes[a[i]] = 1;
    }
  }
  if (pairwise) {
    cout << "pairwise coprime";
    return 0;
  }
  bool setwise = 1;
  ll gcd = a[0];
  for (int i = 1; i < n; i++) {
    gcd = __gcd(gcd, a[i]);
  }
  if (gcd > 1)
    setwise = 0;
  if (setwise) {
    cout << "setwise coprime";
    return 0;
  }
  cout << "not coprime";
}