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

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define FOR_LT(i, beg, end) for (int i = (int)(beg); i < (int)(end); i++)
#define FOR_LE(i, beg, end) for (int i = (int)(beg); i <= (int)(end); i++)
#define FOR_DW(i, beg, end) for (int i = (int)(beg); (int)(end) <= i; i--)
#define REP(n) for (int repeat_index = 0; repeat_index < (int)n; repeat_index++)

using namespace std;

bitset<1000001> primes;
// sqrt(n) までの素数を求める。 10^12 まで対応
void create_primes(int64_t n) {
  primes.flip();

  int64_t u_b = sqrt(n + 1);
  assert(u_b < 1000002);

  {
    for (int64_t j = 4; j <= u_b; j += 2) {
      primes.reset(j);
    }
  }
  for (int64_t i = 3; i <= u_b; i += 2) {
    if (!primes.test(i))
      continue;

    for (int64_t j = i * 2; j <= u_b; j += i) {
      primes.reset(j);
    }
  }
}

struct FactorInfo {
  int64_t v;
  int64_t c;
};

// n の素因数分解を行う。
vector<FactorInfo> make_factors(int64_t n) {
  create_primes(n);

  int64_t v = n;
  int64_t limit = sqrt(n + 1);

  vector<FactorInfo> ans;
  FOR_LE(i, 2, limit) {
    if (!primes.test(i))
      continue;

    int64_t count = 0;
    while (v % i == 0) {
      v /= i;
      count++;
    }

    if (count != 0) {
      ans.push_back({i, count});
    }
  }

  if (v != 1) {
    ans.push_back({v, 1});
  }

  return ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << fixed << setprecision(20);

  int64_t n;
  cin >> n;

  vector<FactorInfo> factors = make_factors(n);

  int64_t ans = 0;
  for (auto f : factors) {
    int i = 1;
    int r = f.c;
    while (i <= r) {
      r -= i;
      ans++;
      i++;
    }
  }

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define FOR_LT(i, beg, end) for (int i = (int)(beg); i < (int)(end); i++)
#define FOR_LE(i, beg, end) for (int i = (int)(beg); i <= (int)(end); i++)
#define FOR_DW(i, beg, end) for (int i = (int)(beg); (int)(end) <= i; i--)
#define REP(n) for (int repeat_index = 0; repeat_index < (int)n; repeat_index++)

using namespace std;

bitset<1000001> primes;
// sqrt(n) までの素数を求める。 10^12 まで対応
void create_primes(int64_t n) {
  primes.flip();

  int64_t u_b = sqrt(n + 1);
  assert(u_b < 1000002);

  {
    for (int64_t j = 4; j <= u_b; j += 2) {
      primes.reset(j);
    }
  }
  for (int64_t i = 3; i <= u_b; i += 2) {
    if (!primes.test(i))
      continue;

    for (int64_t j = i * 2; j <= u_b; j += i) {
      primes.reset(j);
    }
  }
}

int64_t search(int64_t n, vector<int64_t> &nums, int i) {
  int64_t ans = 0;
  for (int j = i; j < nums.size(); j++) {
    int64_t num = nums[j];

    if (n % num == 0) {
      ans = max(ans, search(n / num, nums, j + 1) + 1);
    }
  }
  return ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << fixed << setprecision(20);

  int64_t n;
  cin >> n;
  create_primes(n);

  vector<int64_t> nums;
  FOR_LE(i, 2, n) {
    if (primes.test(i)) {
      nums.push_back(i);
      int64_t p = i;
      while (p * i < n) {
        nums.push_back(p);
        p *= i;
      }
    }
  }
  sort(nums.begin(), nums.end());

  int64_t ans = search(n, nums, 0);
  cout << ans << endl;

  return 0;
}