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

#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp> //required
#include <ext/pb_ds/tree_policy.hpp>     //required

using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long ll;
typedef vector<ll> VL;
typedef vector<int> VI;
typedef pair<ll, ll> PLL;
typedef pair<int, int> PII;
#define pb push_back

#define F first
#define S second
#define SZ(a) int((a).size())
#define ALL(a) a.begin(), a.end()
#define fr(i, x, y) for (ll i = x; i < y; i++)
#define frr(i, x, y) for (ll i = x - 1; i >= y; i--)
#define inf 1e18 + 1
const ll mod = 1000000007;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll power(ll a, ll b) {
  ll ans = 1;
  while (b != 0) {
    if (b & 1) {
      ans *= a;
    }
    a = a * a;
    b >>= 1;
  }
  return ans;
}
ll powerm(ll a, ll b) {
  ll ans = 1;
  while (b != 0) {
    if (b & 1) {
      ans *= a;
      ans %= mod;
    }
    a = a * a;
    a %= mod;
    b >>= 1;
  }
  return ans % mod;
}
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
ll sum(ll n) {
  ll ans = n * (n + 1) / 2;
  return ans;
}
void solve() {
  ll n;
  cin >> n;
  VL prime;
  ll p = 0;
  while (n % 2 == 0) {
    n /= 2;
    p++;
  }
  prime.pb(p);
  for (int i = 3; i <= sqrt(n); i += 2) {
    ll ct = 0;
    while (n % i == 0) {
      n /= i;
      ct++;
    }
    if (ct != 0)
      prime.pb(ct);
  }
  if (n > 2) {
    prime.pb(1);
  }
  ll fct = 0;
  fr(i, 0, SZ(prime)) {
    ll x = prime[i];
    if (x == 0)
      continue;
    ll low = 1, high = 100;
    ll ans = -1;
    while (low <= high) {
      ll mid = (low + high) / 2;
      if (sum(mid) > x) {
        high = mid - 1;
      } else {
        ans = mid;
        low = mid + 1;
      }
    }
    if (ans != -1)
      fct += ans;
  }
  cout << fct << endl;
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  ll t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}
/* stuff you should look for
 * int overflow, array bounds
 * special cases (n=1?)
 * do smth instead of nothing and stay organized
 * WRITE STUFF DOWN
 */
// BITMASK:
// 1)When some constrall is of the order of 15-20, think of bitmask DP.
// 2)When some constrall is around 40, try out meet in the middle
// 3) See Strings,palindromes,prefix,suffix etc -> KMP,Z algorithm

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp> //required
#include <ext/pb_ds/tree_policy.hpp>     //required

using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long ll;
typedef vector<ll> VL;
typedef vector<int> VI;
typedef pair<ll, ll> PLL;
typedef pair<int, int> PII;
#define pb push_back

#define F first
#define S second
#define SZ(a) int((a).size())
#define ALL(a) a.begin(), a.end()
#define fr(i, x, y) for (ll i = x; i < y; i++)
#define frr(i, x, y) for (ll i = x - 1; i >= y; i--)
#define inf 1e18 + 1
const ll mod = 1000000007;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll power(ll a, ll b) {
  ll ans = 1;
  while (b != 0) {
    if (b & 1) {
      ans *= a;
    }
    a = a * a;
    b >>= 1;
  }
  return ans;
}
ll powerm(ll a, ll b) {
  ll ans = 1;
  while (b != 0) {
    if (b & 1) {
      ans *= a;
      ans %= mod;
    }
    a = a * a;
    a %= mod;
    b >>= 1;
  }
  return ans % mod;
}
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
ll sum(ll n) {
  ll ans = n * (n + 1) / 2;
  return ans;
}
void solve() {
  ll n;
  cin >> n;
  VL prime;
  for (int i = 2; i * i <= n; i++) {
    ll ct = 0;
    while (n % i == 0) {
      n /= i;
      ct++;
    }
    if (ct != 0)
      prime.pb(ct);
  }
  if (n != 1) {
    prime.pb(1);
  }
  ll fct = 0;
  fr(i, 0, SZ(prime)) {
    ll x = prime[i];
    if (x == 0)
      continue;
    ll low = 1, high = 1e4;
    ll ans = -1;
    while (low <= high) {
      ll mid = (low + high) / 2;
      if (sum(mid) > x) {
        high = mid - 1;
      } else {
        ans = mid;
        low = mid + 1;
      }
    }
    if (ans != -1)
      fct += ans;
  }
  cout << fct << endl;
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  ll t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}
/* stuff you should look for
 * int overflow, array bounds
 * special cases (n=1?)
 * do smth instead of nothing and stay organized
 * WRITE STUFF DOWN
 */
// BITMASK:
// 1)When some constrall is of the order of 15-20, think of bitmask DP.
// 2)When some constrall is around 40, try out meet in the middle
// 3) See Strings,palindromes,prefix,suffix etc -> KMP,Z algorithm