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

#include <bits/stdc++.h>

using namespace std;

struct Sieve {
  vector<int> isprime;

  vector<int> minpfactor;

  Sieve(int MAX) : isprime(MAX, true), minpfactor(MAX, 0) {
    isprime[0] = false;
    isprime[1] = false;
    int nsqrt = ceil(sqrt(MAX) + 0.1);
    for (int p = 2; p < nsqrt; p++) {
      if (!isprime[p])
        continue;
      minpfactor[p] = p;
      for (int mult = p * p; mult < MAX; mult += p) {
        isprime[mult] = false;
        minpfactor[mult] = p;
      }
    }
    for (int p = nsqrt; p < MAX; p++)
      if (isprime[p])
        minpfactor[p] = p;
  }
};

vector<int> fast_prime_factorize(int a, vector<int> &minprimefactor) {
  vector<int> pfactors;
  while (a > 1) {
    pfactors.push_back(minprimefactor[a]);
    a /= minprimefactor[a];
  }
  return pfactors;
}

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];

  Sieve es(1100000);

  vector<int> numbers(1100000);
  for (auto &&ai : a) {
    vector<int> pfactors = fast_prime_factorize(ai, es.minpfactor);
    int prevp = 0; // 素数の重複を除く
    for (auto &&pf : pfactors) {
      if (pf == prevp)
        continue;
      prevp = pf;
      numbers[pf]++;
    }
  }

  auto fold = [numbers](int m) -> bool {
    return accumulate(numbers.begin(), numbers.end(), true,
                      [m](bool lhsb, int rhs) -> bool {
                        bool rhsb = rhs < m;
                        return lhsb && rhsb;
                      });
  };

  bool ispairwise = fold(2);
  bool issetwise = fold(n);

  string answer;
  if (ispairwise)
    answer = "pairwise coprime";
  else if (issetwise)
    answer = "setwise coprime";
  else
    answer = "not coprime";

  cout << answer << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

struct Sieve {
  vector<int> isprime;

  vector<int> minpfactor;

  Sieve(int MAX) : isprime(MAX, true), minpfactor(MAX, 0) {
    isprime[0] = false;
    isprime[1] = false;
    int nsqrt = ceil(sqrt(MAX) + 0.1);
    for (int p = 2; p < nsqrt; p++) {
      if (!isprime[p])
        continue;
      minpfactor[p] = p;
      for (int mult = p * p; mult < MAX; mult += p) {
        isprime[mult] = false;
        minpfactor[mult] = p;
      }
    }
    for (int p = nsqrt; p < MAX; p++)
      if (isprime[p])
        minpfactor[p] = p;
  }
};

vector<int> fast_prime_factorize(int a, vector<int> &minprimefactor) {
  vector<int> pfactors;
  while (a > 1) {
    pfactors.push_back(minprimefactor[a]);
    a /= minprimefactor[a];
  }
  return pfactors;
}

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];

  Sieve es(1000001);

  vector<int> numbers(100001);
  for (auto &&ai : a) {
    vector<int> pfactors = fast_prime_factorize(ai, es.minpfactor);
    int prevp = 0; // 素数の重複を除く
    for (auto &&pf : pfactors) {
      if (pf == prevp)
        continue;
      prevp = pf;
      numbers[pf]++;
    }
  }

  auto fold = [numbers](int m) -> bool {
    return accumulate(numbers.begin(), numbers.end(), true,
                      [m](bool lhsb, int rhs) -> bool {
                        bool rhsb = rhs < m;
                        return lhsb && rhsb;
                      });
  };

  bool ispairwise = fold(2);
  bool issetwise = fold(n);

  string answer;
  if (ispairwise)
    answer = "pairwise coprime";
  else if (issetwise)
    answer = "setwise coprime";
  else
    answer = "not coprime";

  cout << answer << endl;

  return 0;
}