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

#include <algorithm>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;

#define intmax INT_MAX
#define lmax LONG_MAX
#define uintmax UINT_MAX
#define ulmax ULONG_MAX
#define llmax LLONG_MAX
#define ll long long
#define rep(i, a, N) for ((i) = (a); (i) < (N); (i)++)
#define rrp(i, N, a) for ((i) = (N)-1; (i) >= (a); (i)--)
#define llfor ll i, j, k
#define sc(a) cin >> a
#define pr(a) cout << a << endl
#define pY puts("YES")
#define pN puts("NO")
#define py puts("Yes")
#define pn puts("No")
#define pnn printf("\\n")
#define all(a) a.begin(), a.end()
#define push(a, b) (a).push_back(b)
#define llvec vector<vector<ll>>
#define charvec vector<vector<char>>
#define sizeoof(a, b) (a, vector<ll>(b))
#define llpvec vector<pair<ll, ll>>
/*繰り上げ除算*/ ll cei(ll x, ll y) {
  ll ans = x / y;
  if (x % y != 0)
    ans++;
  return ans;
}
/*最大公約数*/ ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
/*最小公倍数*/ ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
/*n乗*/ ll llpow(ll x, ll n) {
  ll i, ans = 1;
  rep(i, 0, n) ans *= x;
  return ans;
}
/*階乗*/ ll fact(ll x) {
  ll i, ans = 1;
  rep(i, 0, x) ans *= (x - i);
  return ans;
}
/*nCr*/ ll ncr(ll n, ll r) { return fact(n) / fact(r) / fact(n - r); }
/*nPr*/ ll npr(ll n, ll r) { return fact(n) / fact(n - r); }
/*primejudge*/ bool prime(ll a) {
  if (a <= 1)
    return false;
  ll i;
  for (i = 2; i * i <= a; i++) {
    if (a % i == 0)
      return false;
  }
  return true;
}
/*約数*/ vector<ll> div(ll a) {
  vector<ll> b;
  ll i;
  rep(i, 1, a) {
    if (!a % i)
      push(b, a);
  }
  return b;
}
/*素因数分解*/
llfor; ///////////////////////////////////////////////////////////
int main() {
  ll n;
  sc(n);

  bool jd = prime(n);
  if (jd == true) {
    puts("1");
    return 0;
  }
  ll tmp = n;
  ll b = 2;
  ll ans = 0;
  ll cnt = 0;
  ll sm = 0;

  while (b * b <= n) {
    if (tmp % b == 0) {
      tmp /= b;
      cnt++;
    } else {
      rep(j, 0, cnt + 5) {
        if ((j * (j + 1)) / 2 <= cnt) {
          sm = j;
        }
      }
      ans += sm;
      // cout<<b<<" "<<cnt<<" "<<ans<<" "<<tmp<<endl;
      b++;
      cnt = 0;
    }
    if (tmp == 1 && cnt == 0)
      break;
  }
  if (tmp != 1)
    ans++;
  pr(ans);

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;

#define intmax INT_MAX
#define lmax LONG_MAX
#define uintmax UINT_MAX
#define ulmax ULONG_MAX
#define llmax LLONG_MAX
#define ll long long
#define rep(i, a, N) for ((i) = (a); (i) < (N); (i)++)
#define rrp(i, N, a) for ((i) = (N)-1; (i) >= (a); (i)--)
#define llfor ll i, j, k
#define sc(a) cin >> a
#define pr(a) cout << a << endl
#define pY puts("YES")
#define pN puts("NO")
#define py puts("Yes")
#define pn puts("No")
#define pnn printf("\\n")
#define all(a) a.begin(), a.end()
#define push(a, b) (a).push_back(b)
#define llvec vector<vector<ll>>
#define charvec vector<vector<char>>
#define sizeoof(a, b) (a, vector<ll>(b))
#define llpvec vector<pair<ll, ll>>
/*繰り上げ除算*/ ll cei(ll x, ll y) {
  ll ans = x / y;
  if (x % y != 0)
    ans++;
  return ans;
}
/*最大公約数*/ ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
/*最小公倍数*/ ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
/*n乗*/ ll llpow(ll x, ll n) {
  ll i, ans = 1;
  rep(i, 0, n) ans *= x;
  return ans;
}
/*階乗*/ ll fact(ll x) {
  ll i, ans = 1;
  rep(i, 0, x) ans *= (x - i);
  return ans;
}
/*nCr*/ ll ncr(ll n, ll r) { return fact(n) / fact(r) / fact(n - r); }
/*nPr*/ ll npr(ll n, ll r) { return fact(n) / fact(n - r); }
/*primejudge*/ bool prime(ll a) {
  if (a <= 1)
    return false;
  ll i;
  for (i = 2; i * i <= a; i++) {
    if (a % i == 0)
      return false;
  }
  return true;
}
/*約数*/ vector<ll> div(ll a) {
  vector<ll> b;
  ll i;
  rep(i, 1, a) {
    if (!a % i)
      push(b, a);
  }
  return b;
}
/*素因数分解*/
llfor; ///////////////////////////////////////////////////////////
int main() {
  ll n;
  sc(n);

  bool jd = prime(n);
  if (jd == true) {
    puts("1");
    return 0;
  }
  ll tmp = n;
  ll b = 2;
  ll ans = 0;
  ll cnt = 0;
  ll sm = 0;

  while (b <= n) {
    if (tmp % b == 0) {
      tmp /= b;
      cnt++;
    } else {
      rep(j, 0, cnt + 5) {
        if ((j * (j + 1)) / 2 <= cnt) {
          sm = j;
        }
      }
      ans += sm;
      // cout<<b<<" "<<cnt<<" "<<ans<<" "<<tmp<<endl;
      b++;
      cnt = 0;
    }
    if (tmp == 1 && cnt == 0)
      break;
  }
  pr(ans);

  return 0;
}