Problem p02660 (C++) — buggy submission, verdict: Time Limit Exceeded

// People only find me interesting because they can't tell whether I'm serious
// or not #include <boost/multiprecision/cpp_int.hpp>
#include "bits/stdc++.h"
#include "ext/pb_ds/assoc_container.hpp"
#include "ext/pb_ds/tree_policy.hpp"
typedef long long int ll;
typedef double db;
typedef __int128 s128;
typedef __uint128_t u128;
typedef unsigned long long u64;
#define int long long int
#define P 998244353
#define Q 1000000006
#define sz(a) (ll) a.size()
#define lt(a) (ll) a.length()
#define pll pair<ll, ll>
#define f(i, a, b) for (ll i = (ll)a; i < (ll)b; i++)
#define bac(i, a, b) for (ll i = (ll)a; i >= (ll)b; i--)
#define mll map<ll, ll>
#define umll unordered_map<ll, ll>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(a) a.begin(), a.end()
#define F first
#define S second
#define teevra_gati                                                            \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define N 100005
#define PI 3.14159265359
#define inf LLONG_MAX
// using namespace boost::multiprecision;
using namespace __gnu_pbds;
using namespace std;
u64 binpower(u64 base, u64 e, u64 mod) {
  u64 result = 1;
  base %= mod;
  while (e) {
    if (e & 1)
      result = (u128)result * base % mod;
    base = (u128)base * base % mod;
    e >>= 1;
  }
  return result;
}

bool check_composite(u64 n, u64 a, u64 d, int s) {
  u64 x = binpower(a, d, n);
  if (x == 1 || x == n - 1)
    return false;
  for (int r = 1; r < s; r++) {
    x = (u128)x * x % n;
    if (x == n - 1)
      return false;
  }
  return true;
};

bool MillerRabin(
    u64 n,
    int iter = 5) { // returns true if n is probably prime, else returns false.
  if (n < 4)
    return n == 2 || n == 3;

  int s = 0;
  u64 d = n - 1;
  while ((d & 1) == 0) {
    d >>= 1;
    s++;
  }

  for (int i = 0; i < iter; i++) {
    int a = 2 + rand() % (n - 3);
    if (check_composite(n, a, d, s))
      return false;
  }
  return true;
}
void solve() {
  vector<bool> sieve(10000001, 1);
  sieve[1] = 0;
  vector<ll> primes;
  for (ll i = 2; i * i <= 10000000; i++) {
    if (sieve[i] == 1) {
      for (ll j = i; i * j <= 10000000; j++) {
        sieve[i * j] = 0;
      }
    }
  }
  f(i, 2, 10000001) {
    if (sieve[i] == 1)
      primes.push_back(i);
  }
  ll n, ans = 0;
  cin >> n;
  f(i, 0, sz(primes)) {
    ll p = primes[i];
    if (p * p * p > n)
      break;
    ll k = 0;
    while (n % p == 0) {
      n /= p;
      k++;
    }
    ll j = sqrt(2 * k);
    while (j * (j + 1) > 2 * k)
      j--;
    ans += max(0ll, j);
  }
  if (MillerRabin(n) == 1)
    ans += 1;
  else {
    ll t = sqrt(n);
    if (t * t == n && MillerRabin(t) == 1)
      ans += 1;
    else if (n > 1)
      ans += 2;
  }
  cout << ans;
}
signed main() {
  teevra_gati
#ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  solve();

  return 0;
}

Error: TLE

Fixed submission:

// People only find me interesting because they can't tell whether I'm serious
// or not #include <boost/multiprecision/cpp_int.hpp>
#include "bits/stdc++.h"
#include "ext/pb_ds/assoc_container.hpp"
#include "ext/pb_ds/tree_policy.hpp"
typedef long long int ll;
typedef double db;
typedef __int128 s128;
typedef __uint128_t u128;
typedef unsigned long long u64;
#define int long long int
#define P 998244353
#define Q 1000000006
#define sz(a) (ll) a.size()
#define lt(a) (ll) a.length()
#define pll pair<ll, ll>
#define f(i, a, b) for (ll i = (ll)a; i < (ll)b; i++)
#define bac(i, a, b) for (ll i = (ll)a; i >= (ll)b; i--)
#define mll map<ll, ll>
#define umll unordered_map<ll, ll>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(a) a.begin(), a.end()
#define F first
#define S second
#define teevra_gati                                                            \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define N 100005
#define PI 3.14159265359
#define inf LLONG_MAX
// using namespace boost::multiprecision;
using namespace __gnu_pbds;
using namespace std;

void solve() {
  ll n, ans = 0;
  cin >> n;
  for (ll i = 2; i <= n; i++) {
    if (n % i == 0) {
      ll k = 0;
      while (n % i == 0) {
        n /= i;
        k++;
      }
      ll j = sqrt(2 * k);
      while (j * (j + 1) > 2 * k)
        j--;
      ans += max(0ll, j);
      // cout<<i<<" "<<k<<endl;
    }
  }
  cout << ans;
}
signed main() {
  teevra_gati
#ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  solve();

  return 0;
}