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

#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using ull = uint64_t;
const ll INF = 9e18;

void print() { cout << endl; }
template <typename Head, typename... Tail> void print(Head head, Tail... tail) {
  int size = sizeof...(Tail);
  cout << head;
  if (size > 0) {
    cout << " ";
  }
  print(tail...);
}

void print0() {}
template <typename Head, typename... Tail>
void print0(Head head, Tail... tail) {
  cout << head;
  print0(tail...);
}

vector<ll> mkprimes(ll N) {
  vector<bool> isprime(N + 1);
  vector<ll> primes;
  for (ll i = 0; i <= N; i++) {
    isprime[i] = true;
  }
  isprime[0] = false;
  isprime[1] = false;

  for (ll i = 2; i <= N; i++) {
    if (isprime[i]) {
      primes.push_back(i);
      for (ll j = 2 * i; j <= N; j += i) { // 素数iの倍数は素数ではない
        isprime[j] = false;
      }
    }
  }
  return primes;
}

set<ll> primefact(ll n, vector<ll> &primes, vector<set<ll>> &cache) {
  if (n <= 1) {
    set<ll> s;
    return s;
  }
  if (cache[n].size() > 0) {
    return cache[n];
  }
  for (ll p = 0; p < primes.size(); p++) {
    ll pr = primes[p];
    if (n % pr == 0) {
      set<ll> s = primefact(n / pr, primes, cache);
      s.insert(pr);
      cache[n] = s;
      return cache[n];
    }
  }
}

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  for (ll i = 0; i < N; i++) {
    cin >> A[i];
  }
  ll pmax = 1000002;
  auto primes = mkprimes(pmax);
  // vector<bool> isprime(pmax+1, false);
  vector<set<ll>> cache(pmax + 1);
  for (auto ppr : primes) {
    // isprime[ppr] = true;
    cache[ppr].insert(ppr);
  }
  vector<vector<ll>> factors(N);
  vector<ll> usedprimes(pmax + 1, 0);
  for (ll i = 0; i < N; i++) {
    ll n = A[i];
    for (ll pf : primefact(n, primes, cache)) {
      usedprimes[pf]++;
    }
  }
  ll maxused = 0;
  for (auto u : usedprimes) {
    maxused = max(u, maxused);
  }
  if (maxused == N) {
    print("not coprime");
  } else if (maxused >= 2) {
    print("setwise coprime");
  } else {
    print("pairwise coprime");
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using ull = uint64_t;
const ll INF = 9e18;

void print() { cout << endl; }
template <typename Head, typename... Tail> void print(Head head, Tail... tail) {
  int size = sizeof...(Tail);
  cout << head;
  if (size > 0) {
    cout << " ";
  }
  print(tail...);
}

void print0() {}
template <typename Head, typename... Tail>
void print0(Head head, Tail... tail) {
  cout << head;
  print0(tail...);
}

vector<ll> mkprimes(ll N) {
  vector<bool> isprime(N + 1);
  vector<ll> primes;
  for (ll i = 0; i <= N; i++) {
    isprime[i] = true;
  }
  isprime[0] = false;
  isprime[1] = false;

  for (ll i = 2; i <= N; i++) {
    if (isprime[i]) {
      primes.push_back(i);
      for (ll j = 2 * i; j <= N; j += i) { // 素数iの倍数は素数ではない
        isprime[j] = false;
      }
    }
  }
  return primes;
}

set<ll> primefact(ll n, vector<ll> &primes, vector<set<ll>> &cache) {
  if (cache[n].size() > 0) {
    return cache[n];
  }
  for (ll p = 0; p < primes.size(); p++) {
    ll pr = primes[p];
    if (n % pr == 0) {
      set<ll> s = primefact(n / pr, primes, cache);
      s.insert(pr);
      cache[n] = s;
      return cache[n];
    }
  }
}

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  for (ll i = 0; i < N; i++) {
    cin >> A[i];
  }
  ll pmax = 1000002;
  auto primes = mkprimes(pmax);
  // vector<bool> isprime(pmax+1, false);
  vector<set<ll>> cache(pmax + 1);
  for (auto ppr : primes) {
    // isprime[ppr] = true;
    cache[ppr].insert(ppr);
  }
  vector<vector<ll>> factors(N);
  vector<ll> usedprimes(pmax + 1, 0);
  for (ll i = 0; i < N; i++) {
    ll n = A[i];
    for (ll pf : primefact(n, primes, cache)) {
      usedprimes[pf]++;
    }
  }
  ll maxused = 0;
  for (auto u : usedprimes) {
    maxused = max(u, maxused);
  }
  if (maxused == N) {
    print("not coprime");
  } else if (maxused >= 2) {
    print("setwise coprime");
  } else {
    print("pairwise coprime");
  }
}