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

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

typedef long long ll;
template <class T> ll size(const T &x) { return x.size(); }
#define rep(i, a, b) for (ll i = (a); i < (b); ++i)
#define iter(it, c) for (auto it = (c).begin(); it != (c).end(); ++it)
#define pb push_back
#define fs first
#define sc second
#define MOD 1000000007
#define MOD2 998244353
#define INF 1000000000000000000ll
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
typedef vector<ii> vii;
template <class T> T smod(T a, T b) { return (a % b + b) % b; }

vi ans(2000000, 0);
int r = -1;
void primeFactors(ll n) {
  // Print the number of 2s that divide n
  while (n % 2 == 0) {
    // cout << 2 << " ";
    ans[2]++;
    n = n / 2;
  }

  // n must be odd at this point. So we can skip
  // one element (Note i = i +2)
  for (ll i = 3; i <= sqrt(n); i = i + 2) {
    // While i divides n, print i and divide n
    while (n % i == 0) {
      ans[i]++;
      n = n / i;
    }
  }

  // This condition is to handle the case when n
  // is a prime number greater than 2
  if (n > 2)
    r = n;
}

void solve() {
  ll n;
  cin >> n;
  primeFactors(n);
  // cout << "1" << endl;
  // rep(i,0,sqrt(n)+1) cout << primefact[i] << ' '; cout << endl;
  int cnt = 0;
  rep(p, 2, sqrt(n) + 1) {
    ll cnti = ans[p];
    bool f = true;
    if (cnti <= 0)
      f = false;
    ll i = 1;
    while (f) {
      bool s = false;
      if (cnti - i < i + 1)
        s = true;
      cnti -= i;
      cnt++;
      i++;
      if (s)
        break;
    }
  }
  if (r != -1)
    cnt++;
  cout << cnt << endl;
}

int main() {
  cin.sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  /*ll t;
  cin >> t;
  while(t--)*/
  solve();
  return 0;
}


Error: 0

Fixed submission:

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

typedef long long ll;
template <class T> ll size(const T &x) { return x.size(); }
#define rep(i, a, b) for (ll i = (a); i < (b); ++i)
#define iter(it, c) for (auto it = (c).begin(); it != (c).end(); ++it)
#define pb push_back
#define fs first
#define sc second
#define MOD 1000000007
#define MOD2 998244353
#define INF 1000000000000000000ll
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
typedef vector<ii> vii;
template <class T> T smod(T a, T b) { return (a % b + b) % b; }

vi primeFactors(ll n) {
  // Print the number of 2s that divide n
  vi ans(2000000, 0);
  while (n % 2 == 0) {
    // cout << 2 << " ";
    ans[2]++;
    n = n / 2;
  }

  // n must be odd at this point. So we can skip
  // one element (Note i = i +2)
  for (ll i = 3; i <= sqrt(n); i = i + 2) {
    // While i divides n, print i and divide n
    while (n % i == 0) {
      ans[i]++;
      n = n / i;
    }
  }

  // This condition is to handle the case when n
  // is a prime number greater than 2
  if (n > 2)
    ans[n]++;
  return ans;
}

void solve() {
  ll n;
  cin >> n;
  vi primefact = primeFactors(n);
  // cout << "1" << endl;
  // rep(i,0,sqrt(n)+1) cout << primefact[i] << ' '; cout << endl;
  int cnt = 0;
  rep(p, 2, sqrt(n) + 1) {
    ll cnti = primefact[p];
    bool f = true;
    if (cnti <= 0)
      f = false;
    ll i = 1;
    while (f) {
      bool s = false;
      if (cnti - i < i + 1)
        s = true;
      cnti -= i;
      cnt++;
      i++;
      if (s)
        break;
    }
  }
  cout << cnt << endl;
}

int main() {
  cin.sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  /*ll t;
  cin >> t;
  while(t--)*/
  solve();
  return 0;
}