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

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

int my_gcd(int n, int m) {
  if (n < m)
    swap(n, m);
  if (m == 0)
    return n;
  return my_gcd(m, n % m);
}

signed main() {
  // cout << fixed << setprecision(10) << flush;
  // ここまでの WA はどこで時間がかかっているかの実験です
  // cin が悪そうだという結論が出ました
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  vector<int> a(n);
  int maxa = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    maxa = (maxa < a[i]) ? a[i] : maxa;
  }

  vector<int> primes;
  vector<int> min_prime_factor(maxa + 1, 0);

  primes.push_back(2);
  for (int j = 2; j <= maxa; j += 2) {
    min_prime_factor[j] = 2;
  }

  int i;
  for (i = 3; i * i <= maxa; i += 2) {
    if (min_prime_factor[i] != 0)
      continue;
    primes.push_back(i);
    for (int j = i; j <= maxa; j += i) {
      if (min_prime_factor[j] == 0) {
        min_prime_factor[j] = i;
      }
    }
  }

  for (; i <= maxa; i += 2) {
    if (min_prime_factor[i] == 0) {
      primes.push_back(i);
      min_prime_factor[i] = i;
    }
  }
  int ggcd = a[0];
  for (int i = 1; i < n; i++) {
    ggcd = my_gcd(a[i], ggcd);
    if (ggcd == 1)
      break;
  }

  if (ggcd != 1) {
    cout << "not coprime" << endl;
    return 0;
  }

  map<int, int> mp_prime;
  for (int j = 0; j < primes.size(); j++) {
    mp_prime[primes[j]] = j;
  }

  vector<bool> c(primes.size(), false);
  for (int i = 0; i < n; i++) {
    while (a[i] != 1) {
      int p = min_prime_factor[a[i]];
      if (c[mp_prime[p]] != false) {
        cout << "setwise coprime" << endl;
        return 0;
      }
      c[mp_prime[p]] = true;
      a[i] /= p;
      while (a[i] % p == 0)
        a[i] /= p;
    }
  }
  cout << "pairwise coprime" << endl;

  return 0;
}

Error: 0

Fixed submission:

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

int my_gcd(int n, int m) {
  if (n < m)
    swap(n, m);
  if (m == 0)
    return n;
  return my_gcd(m, n % m);
}

signed main() {
  // cout << fixed << setprecision(10) << flush;
  // ここまでの WA はどこで時間がかかっているかの実験です
  // cin が悪そうだという結論が出ました
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  vector<int> a(n);
  int maxa = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    maxa = (maxa < a[i]) ? a[i] : maxa;
  }

  vector<int> primes;
  vector<int> min_prime_factor(maxa + 1, 0);

  primes.push_back(2);
  for (int j = 2; j <= maxa; j += 2) {
    min_prime_factor[j] = 2;
  }

  int i;
  for (i = 3; i * i <= maxa; i += 2) {
    if (!min_prime_factor[i])
      continue;
    primes.push_back(i);
    for (int j = 2 * i; j <= maxa; j += i) {
      if (min_prime_factor[j] == 0) {
        min_prime_factor[j] = i;
      }
    }
  }

  for (; i <= maxa; i += 2) {
    if (min_prime_factor[i] == 0) {
      primes.push_back(i);
      min_prime_factor[i] = i;
    }
  }

  int ggcd = a[0];
  for (int i = 1; i < n; i++) {
    ggcd = my_gcd(a[i], ggcd);
    if (ggcd == 1)
      break;
  }

  if (ggcd != 1) {
    cout << "not coprime" << endl;
    return 0;
  }

  map<int, int> mp_prime;
  for (int j = 0; j < primes.size(); j++) {
    mp_prime[primes[j]] = j;
  }

  vector<bool> c(primes.size(), false);
  for (int i = 0; i < n; i++) {
    while (a[i] != 1) {
      int p = min_prime_factor[a[i]];
      if (c[mp_prime[p]] != false) {
        cout << "setwise coprime" << endl;
        return 0;
      }
      c[mp_prime[p]] = true;
      a[i] /= p;
      while (a[i] % p == 0)
        a[i] /= p;
    }
  }
  cout << "pairwise coprime" << endl;

  return 0;
}