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

// 有點不耐煩會破壞偉大的計劃

#include <bits/stdc++.h>

#define append push_back

using namespace std;
using ll = long long;

const int N = 1e5 + 3;
const int M = 1e6 + 7;
const long long MOD = 1e9 + 7;
const long long INF = 1e18 + 9;
const int SM = 1e3 + 5;
const int logN = 22, BL = 400;
const double EPS = 1e-9, PI = acos(-1);
const int dx[] = {1, 0, 0, -1, -1, 1, -1, 1};
const int dy[] = {0, 1, -1, 0, -1, 1, 1, -1};

void debug() { cerr << "\\n"; }
template <typename Type1, typename... Type2> void debug(Type1 a, Type2... b) {
  cerr << a << " ";
  debug(b...);
}

long long lp[M], cnt[M];
vector<long long> pr;

int main() {
  //    freopen(".in", "r", stdin);
  //    freopen(".out", "w", stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);

  long long n, all_gcd = 0;
  cin >> n;

  vector<long long> a(n);

  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    all_gcd = __gcd(all_gcd, a[i]);
  }

  for (long long i = 2; i < M; i++) {
    if (!lp[i]) {
      lp[i] = i;
      pr.append(i);
    }
    for (long long j = 0;
         j < ll(pr.size()) && pr[j] <= lp[i] && i * 1ll * pr[j] < M; j++) {
      lp[i * 1ll * pr[j]] = pr[j];
    }
  }

  for (long long i = 0; i < n; i++) {
    long long k = a[i];
    while (k > 1) {
      long long dv = lp[k];
      cnt[dv]++;
      while (k % dv == 0) {
        k /= dv;
      }
    }
  }

  long long ans = (all_gcd == 1 ? 1 : 0);

  for (long long i = 1; i < M; i++) {
    if (cnt[i] > 1) {
      ans = (all_gcd == 1 ? 2 : 0);
      break;
    }
  }

  if (!ans)
    cout << "not coprime";
  else if (ans == 1)
    cout << "pairwise coprime";
  else
    cout << "setwise coprime";

  return 0;
}


Error: WA

Fixed submission:

// 有點不耐煩會破壞偉大的計劃

#include <bits/stdc++.h>

#define append push_back

using namespace std;
using ll = long long;

const int N = 1e5 + 3;
const int M = 1e6 + 7;
const long long MOD = 1e9 + 7;
const long long INF = 1e18 + 9;
const int SM = 1e3 + 5;
const int logN = 22, BL = 400;
const double EPS = 1e-9, PI = acos(-1);
const int dx[] = {1, 0, 0, -1, -1, 1, -1, 1};
const int dy[] = {0, 1, -1, 0, -1, 1, 1, -1};

void debug() { cerr << "\\n"; }
template <typename Type1, typename... Type2> void debug(Type1 a, Type2... b) {
  cerr << a << " ";
  debug(b...);
}

long long lp[M], cnt[M];
vector<long long> pr;

int main() {
  //    freopen(".in", "r", stdin);
  //    freopen(".out", "w", stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);

  long long n, all_gcd = 0;
  cin >> n;

  vector<long long> a(n);

  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    all_gcd = __gcd(all_gcd, a[i]);
  }

  for (long long i = 2; i < SM; i++) {
    if (!lp[i]) {
      lp[i] = i;
      pr.append(i);
    }
    for (long long j = 0;
         j < ll(pr.size()) && pr[j] <= lp[i] && i * 1ll * pr[j] < SM; j++) {
      lp[i * 1ll * pr[j]] = pr[j];
    }
  }

  for (long long i = 0; i < n; i++) {
    long long k = a[i];
    while (k > 1) {
      long long dv = lp[k];
      cnt[dv]++;
      while (k % dv == 0) {
        k /= dv;
      }
    }
  }

  long long ans = (all_gcd == 1 ? 1 : 0);

  for (long long i = 1; i < M; i++) {
    if (cnt[i] > 1) {
      ans = (all_gcd == 1 ? 2 : 0);
      break;
    }
  }

  if (!ans)
    cout << "not coprime";
  else if (ans == 1)
    cout << "pairwise coprime";
  else
    cout << "setwise coprime";

  return 0;
}