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

#include <bits/stdc++.h>

using namespace std;

#define INF 1000000000
#define INFLL 0x3f3f3f3f3f3f3f3fLL
#define EPS 10e-9
#define MOD 1000000007
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define st first
#define nd second
#define sz(v) int(v.size())
#define all(X) (X).begin(), (X).end()
#define FOR(I, A, B) for (int I = A; I < B; I++)
#define RFOR(I, A, B) for (int I = A; I >= B; I--)

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<vii> vvii;
typedef vector<iii> viii;
typedef vector<ll> vll;

ll _sieve_size;
bitset<10000010> bs;
vll primes;

void sieve(ll upperbound) {
  _sieve_size = upperbound + 1;
  bs.set();
  bs[0] = bs[1] = 0;
  for (ll i = 2; i <= _sieve_size; i++) {
    if (bs[i]) {
      for (ll j = i * i; j <= _sieve_size; j += i) {
        bs[j] = 0;
      }
      primes.push_back(i);
    }
  }
}

map<ll, ll> getPrimeFact(ll n) {
  ll ind = 0, pf = primes[0];
  map<ll, ll> ans;
  while (pf * pf <= n) {
    while (n % pf == 0) {
      n /= pf;
      ans[pf]++;
    }
    if (ind + 1 == sz(primes))
      break;
    pf = primes[++ind];
  }
  if (n != 1)
    ans[n]++;
  return ans;
}

ll getans(ll e) {
  ll l = 1, r = 1000000;
  while (l < r) {
    ll mid = (l + r + 1) / 2;
    if ((mid * (mid + 1)) / 2 > e) {
      r = mid - 1;
    } else {
      l = mid;
    }
  }
  return l;
}

int main() {
  ll n;
  scanf("%lld", &n);
  sieve(1000000);
  map<ll, ll> fact = getPrimeFact(n);
  ll ans = 0;
  for (auto par : fact) {
    ll e = par.nd;
    // printf("e: %lld, getans: %lld\\n", e, getans(e));
    ans += getans(e);
  }
  printf("%lld\\n", ans);
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define INF 1000000000
#define INFLL 0x3f3f3f3f3f3f3f3fLL
#define EPS 10e-9
#define MOD 1000000007
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define st first
#define nd second
#define sz(v) int(v.size())
#define all(X) (X).begin(), (X).end()
#define FOR(I, A, B) for (int I = A; I < B; I++)
#define RFOR(I, A, B) for (int I = A; I >= B; I--)

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<vii> vvii;
typedef vector<iii> viii;
typedef vector<ll> vll;

ll _sieve_size;
bitset<10000010> bs;
vll primes;

void sieve(ll upperbound) {
  _sieve_size = upperbound + 1;
  bs.set();
  bs[0] = bs[1] = 0;
  for (ll i = 2; i <= _sieve_size; i++) {
    if (bs[i]) {
      for (ll j = i * i; j <= _sieve_size; j += i) {
        bs[j] = 0;
      }
      primes.push_back(i);
    }
  }
}

map<ll, ll> getPrimeFact(ll n) {
  ll ind = 0, pf = primes[0];
  map<ll, ll> ans;
  while (pf * pf <= n) {
    while (n % pf == 0) {
      n /= pf;
      ans[pf]++;
    }
    pf = primes[++ind];
  }
  if (n != 1)
    ans[n]++;
  return ans;
}

ll getans(ll e) {
  ll l = 1, r = 1000000;
  while (l < r) {
    ll mid = (l + r + 1) / 2;
    if ((mid * (mid + 1)) / 2 > e) {
      r = mid - 1;
    } else {
      l = mid;
    }
  }
  return l;
}

int main() {
  ll n;
  scanf("%lld", &n);
  sieve(1000000);
  map<ll, ll> fact = getPrimeFact(n);
  ll ans = 0;
  for (auto par : fact) {
    ll e = par.nd;
    // printf("e: %lld, getans: %lld\\n", e, getans(e));
    ans += getans(e);
  }
  printf("%lld\\n", ans);
  return 0;
}