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

#include <bits/stdc++.h>

using namespace std;

#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define pb push_back
#define endl '\\n'
#define snd second
#define fst first
#define fastio cin.tie(NULL), cout.sync_with_stdio(true)

typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef pair<ll, int> ii;
typedef pair<ii, int> iii;

const int mod = 1e9 + 7;
const int N = 200005;

bool isPrime(ll n) {
  if (n < 5 || n % 2 == 0 || n % 3 == 0)
    return (n == 2 || n == 3);
  ll maxP = sqrt(n) + 2;
  for (ll p = 5; p < maxP; p += 6)
    if (n % p == 0 || n % (p + 2) == 0)
      return false;
  return true;
}

void solve() {
  ll n;
  cin >> n;
  set<pair<ll, ll>> p;
  for (ll i = 2; i <= sqrt(n); i++) {
    if (n % i == 0) {
      if (i == n / i and i != 1 and isPrime(i))
        p.insert({i, i});
      else {
        if (i != 1 and isPrime(i))
          p.insert({i, i});
        if (n / i != 1 and isPrime(n / i))
          p.insert({n / i, n / i});
      }
    }
  }
  if (p.empty()) {
    cout << (n > 1 ? 1 : 0) << endl;
    return;
  }
  ll cnt = 0;
  while (true) {
    while (!p.empty() and n % (*(p.begin())).fst != 0)
      p.erase(p.begin());
    if (p.empty()) {
      cout << cnt << endl;
      return;
    }
    ll temp = (*(p.begin())).fst, v = (*(p.begin())).snd;
    n /= temp;
    p.erase(p.begin());
    p.insert({temp * v, v});
    cnt++;
    if (n == 1) {
      cout << cnt << endl;
      return;
    }
  }
}

int main() {
  fastio;
  int t = 1;
  // cin >> t;
  while (t--)
    solve();
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define pb push_back
#define endl '\\n'
#define snd second
#define fst first
#define fastio cin.tie(NULL), cout.sync_with_stdio(true)

typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef pair<ll, int> ii;
typedef pair<ii, int> iii;

const int mod = 1e9 + 7;
const int N = 200005;

bool isPrime(ll n) {
  if (n < 5 || n % 2 == 0 || n % 3 == 0)
    return (n == 2 || n == 3);
  ll maxP = sqrt(n) + 2;
  for (ll p = 5; p < maxP; p += 6)
    if (n % p == 0 || n % (p + 2) == 0)
      return false;
  return true;
}

void solve() {
  ll n;
  cin >> n;
  set<pair<ll, ll>> p;
  for (int i = 2; i <= sqrt(n); i++) {
    if (n % i == 0) {
      if (i == n / i and i != 1 and isPrime(i))
        p.insert({i, i});
      else {
        if (i != 1 and isPrime(i))
          p.insert({i, i});
        if (n / i != 1 and isPrime(n / i))
          p.insert({n / i, n / i});
      }
    }
  }
  if (p.empty()) {
    cout << (n > 1 ? 1 : 0) << endl;
    return;
  }
  ll cnt = 0;
  while (true) {
    while (n % (*(p.begin())).fst != 0)
      p.erase(p.begin());
    ll temp = (*(p.begin())).fst, v = (*(p.begin())).snd;
    n /= temp;
    // cout <<temp <<" "<< p.size() <<" " << n << " "<< endl;
    p.erase(p.begin());
    p.insert({temp * v, v});
    cnt++;
    if (n == 1 or n == temp) {
      cout << cnt << endl;
      return;
    }
  }
  cout << 0 << endl;
}

int main() {
  fastio;
  int t = 1;
  // cin >> t;
  while (t--)
    solve();
  return 0;
}