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

#include <bits/stdc++.h>
#define endl "\\n"
#define pb push_back
#define ll long long
#define d1(x) cerr << #x << "--> " << x << endl
#define d2(x, y) cerr << #x << "--> " << x << " | " << #y << "--> " << y << endl
#define d3(x, y, z)                                                            \\
  cerr << #x << "--> " << x << " | " << #y << "--> " << y << " | " << #z       \\
       << "--> " << z << endl
#define d4(x, y, z, w)                                                         \\
  cerr << #x << "--> " << x << " | " << #y << "--> " << y << " | " << #z       \\
       << "--> " << z << " | " << #w << "--> " << w << endl
#define vpll vector<pair<ll, ll>>
#define F first
#define S second
#define T                                                                      \\
  ll tt;                                                                       \\
  cin >> tt;                                                                   \\
  while (tt--)
using namespace std;
ll mode = 1e9 + 7;
const long long maxn = 2e5 + 5;
const long long inf = 99999999999;
ll __lcm(ll a, ll b) { return (a * b) / __gcd(a, b); }
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll getRand(ll l, ll r) {
  uniform_int_distribution<ll> uid(l, r);
  return uid(rng);
}

ll power(ll x, ll y) {
  ll p = mode, res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}

void solve() {

  ll n;
  cin >> n;
  ll ans = 0;
  map<ll, ll> mp;
  while (n % 2 == 0) {
    mp[2]++;
    n /= 2;
  }
  for (ll i = 3; i * i <= n; i += 2) {
    if (n % i == 0) {
      while (n % i == 0) {
        mp[i]++;
        n /= i;
      }
    }
  }

  if (n > 1) {
    mp[n]++;
  }

  for (auto i : mp) {
    ll l = 1, r = i.S;
    ll mx = 1;
    while (l <= r) {
      ll mid = (l + r) / 2;
      ll tmp = (mid * (mid + 1)) / 2;
      if (tmp <= i.S) {
        mx = max(mx, mid);
        l = mid + 1;
      } else {
        r = mid - 1;
      }
    }

    ans += mx;
  }
  cout << ans << endl;

  return;
}

int main() {

#ifndef ONLINE_JUDGE
  //	freopen("input.txt", "r", stdin);
#endif
  srand(time(0));

  ios::sync_with_stdio(0);
  cin.tie(0);

  // T{ solve(); }
  solve();

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define endl "\\n"
#define pb push_back
#define ll long long
#define d1(x) cerr << #x << "--> " << x << endl
#define d2(x, y) cerr << #x << "--> " << x << " | " << #y << "--> " << y << endl
#define d3(x, y, z)                                                            \\
  cerr << #x << "--> " << x << " | " << #y << "--> " << y << " | " << #z       \\
       << "--> " << z << endl
#define d4(x, y, z, w)                                                         \\
  cerr << #x << "--> " << x << " | " << #y << "--> " << y << " | " << #z       \\
       << "--> " << z << " | " << #w << "--> " << w << endl
#define vpll vector<pair<ll, ll>>
#define F first
#define S second
#define T                                                                      \\
  ll tt;                                                                       \\
  cin >> tt;                                                                   \\
  while (tt--)
using namespace std;
ll mode = 1e9 + 7;
const long long maxn = 2e5 + 5;
const long long inf = 99999999999;
ll __lcm(ll a, ll b) { return (a * b) / __gcd(a, b); }
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll getRand(ll l, ll r) {
  uniform_int_distribution<ll> uid(l, r);
  return uid(rng);
}

ll power(ll x, ll y) {
  ll p = mode, res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}

void solve() {

  ll n;
  cin >> n;
  ll ans = 0;
  map<ll, ll> mp;
  while (n % 2 == 0) {
    mp[2]++;
    n /= 2;
  }
  for (ll i = 3; i <= n; i += 2) {
    if (n % i == 0) {
      while (n % i == 0) {
        mp[i]++;
        n /= i;
      }
    }
  }
  for (auto i : mp) {
    ll l = 1, r = 1e9;
    ll mx = 1;
    while (l <= r) {
      ll mid = (l + r) / 2;
      ll tmp = (mid * (mid + 1)) / 2;
      if (tmp <= i.S) {
        mx = max(mx, mid);
        l = mid + 1;
      } else {
        r = mid - 1;
      }
    }
    ans += mx;
  }
  cout << ans << endl;

  return;
}

int main() {

#ifndef ONLINE_JUDGE
  //	freopen("input.txt", "r", stdin);
#endif
  srand(time(0));

  ios::sync_with_stdio(0);
  cin.tie(0);

  // T{ solve(); }
  solve();

  return 0;
}