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

#include <bits/stdc++.h>
using namespace std;

vector<long long int> primes;
bool isprime[2000009];
int ndiv[1000009];

void div() {
  long long int sum = 0;
  for (int i = 1; sum <= 1000000; i++) {
    sum += i;
    ndiv[sum] = i;
  }
  int c = 1;
  for (int i = 1; i <= 1000000; i++) {
    if (ndiv[i] == 0)
      ndiv[i] = c;
    else
      c = ndiv[i];
  }
}
void sieve() {
  for (int i = 3; i <= 2000005; i += 2) {
    if (isprime[i] == 0) {
      for (int j = i * 2; j <= 2000005; j += i)
        isprime[j] = 1;
    }
  }
  primes.push_back(2);
  for (int i = 3; i < 2000005; i += 2)
    if (isprime[i] == 0)
      primes.push_back(i);
}

int main() {
  /// sieve();
  /// div();

  long long num;
  map<long long int, long long int> mp;
  vector<long long int> st;
  while (cin >> num) {
    long int sum = 0;
    for (long long int i = 2; i * i <= num; i++) {
      if (num % i == 0) {
        int c = 0;
        /// st.push_back(i);
        while (num % i == 0) {
          c++;
          num /= i;
        }
        int k = 1;
        while (c >= k) {
          c -= k;
          k++;
          sum++;
        }
        /// sum+= ndiv[c];
      }
    }
    if (num > 1) {
      sum++;
    }
    cout << sum << endl;
    mp.clear();
    st.clear();
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define IO ios::sync_with_stdio(false), cin.tie(0)
#define endl '\\n'
#define pi acos(-1.0)
#define lowbit(x) ((x) & (-(x)))
#define debug(x) cout << x << '\\n'
#define pii pair<ll, pair<ll, ll>>
using namespace std;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
// const ll mod = 1e9+7;
ll mod;
const double eps = 1e-6;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll mul(ll a, ll b) {
  ll ans = 0;
  a %= mod, b %= mod;
  while (b) {
    if (b & 1)
      ans = (ans + a) % mod;
    a = (a * 2) % mod;
    b >>= 1;
  }
  return ans;
}
ll powmod(ll a, ll b) {
  ll ans = 1, pow = a;
  while (b) {
    if (b & 1)
      ans = (ans * pow) % mod;
    pow = (pow * pow) % mod;
    b >>= 1;
  }
  return ans;
}
//(b/a)%p=b*a^(p-2)%p
int pr(ll num) { // 判素
  if (num == 1)
    return 0;
  if (num == 2 || num == 3)
    return 1;
  if (num % 6 != 1 && num % 6 != 5)
    return 0;
  int tmp = sqrt(num);
  for (int i = 5; i <= tmp; i += 6)
    if (num % i == 0 || num % (i + 2) == 0)
      return 0;
  return 1;
}
int Day(int y, int m, int d) {
  if (m == 1 || m == 2) {
    m += 12;
    y--;
  }
  int w = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7;
  return w + 1;
}
int pri[1000005];
void getpri() {
  memset(pri, true, sizeof(pri));
  pri[0] = pri[1] = 0;
  for (int i = 2; i <= 1000000; i++) {
    if (pri[i]) {
      for (int j = 2 * i; j <= 1000000; j += i)
        pri[j] = 0;
    }
  }
}
inline int read() {
  int c = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    c = c * 10 + ch - '0';
    ch = getchar();
  }
  return c * f;
}
// int pre[100005];
// void init(){
//     for(int i=1; i<=100005;++i) pre[i]=i;
// }
// int getf(int x){
//     if(pre[x] == x) return x;
//     else return pre[x] = getf(pre[x]);
// }
// void join(int x,int y){
//     int a = getf(x);
//     int b = getf(y);
//     if(a!=b) pre[b]=a;
// }
// 大小写转换
// String s;
// Form(s.begin(),s.end(),s.begin(),::toupper);
// Form(s.begin(),s.end(),s.begin(),::tolower);
//(char)toupper('a')
//(char)tolower('B')
// #pragma comment(linker, "/STACK:1024000000,1024000000")//外挂开栈
// stable_sort 排序不处理相同元素
// map<int,int>::reverse_iterator rit;
// for(rit=mp.rbegin();rit!=mp.rend();rit++)
// vec动态二维数组
// vector<vector<int>> g(26,vector<int>(100005));
// 初始化vec里面套了n个vec
// lower_bound(begin,end,num) 有序数列中返回第一个>=num的数字的地址
// upper_bound( begin,end,num) 返回第一个>num的数字的地址
const int maxn = 3e5 + 5;
int main() {
  IO;
  ll x;
  cin >> x;
  ll ans = 0;
  map<ll, ll> mp;
  for (int j = 2; j * j <= x; ++j) {
    while (x % j == 0) {
      mp[j]++;
      x /= j;
    }
  }
  if (x > 1)
    mp[x]++;
  if (mp.empty()) {
    cout << 0 << endl;
  } else {
    for (auto i : mp) {
      for (ll j = 1; j < 10000; ++j) {
        if ((1 + j) * j / 2 > i.second) {
          ans += j - 1;
          break;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}