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

#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 (ll 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;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ms(a, x) memset(a, x, sizeof(a))
#define fore(i, a, n) for (ll ll i = a; i < n; i++)
#define ford(i, a, n) for (ll ll i = n - 1; i >= a; i--)
#define si(a) scanf("%d", &a)
#define sl(a) scanf("%lld", &a)
#define sii(a, b) scanf("%d%d", &a, &b)
#define siii(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define sll(a, b) scanf("%lld%lld", &a, &b)
#define slll(a, b, c) scanf("%lld%lld%lld", &a, &b, &c)
#define debug(a) cout << a << endl
#define pr(a) printf("%d ", a)
#define endl '\\n'
#define pi acos(-1.0)
#define tr t[root]
#define lson t[root << 1]
#define rson t[root << 1 | 1]
#define IO ios::sync_with_stdio(false), cin.tie(0)
#define ull unsigned ll ll
#define py puts("Yes")
#define pn puts("No")
#define pY puts("YES")
#define pN puts("NO")
#define re(i, a, b) for (int i = a; i <= b; ++i)
#define de(i, a, b) for (int i = a; i >= b; --i)
const double eps = 1e-8;
inline int sgn(const double &x) { return x < -eps ? -1 : x > eps; }
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int MAXN = 2e5 + 5;
template <class t> inline void cmin(t &a, t b) { ((a > b) && (a = b)); }
template <class t> inline void cmax(t &a, t b) { ((a < b) && (a = b)); }
const ll V = 1e6 + 5;
const ll E = 4e6 + 5;
const ll mode = 1e9 + 7;
ll weight[E], to[E], nex[E];
ll cnt = 0;
ll n, m, dis[V];
ll head[V];
bool vis[V];
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

ll multi(ll a, ll b, ll p) { // 快速乘法
  ll ret = 0;
  while (b) {
    if (b & 1)
      ret = (ret + a) % p;
    a = (a + a) % p;
    b >>= 1;
  }
  return ret;
}

ll Pow(ll a, ll b, ll mode) {
  ll sum = 1;
  a = a % mode;
  while (b > 0) {
    if (b % 2 == 1) // 判断是否是奇数，是奇数的话将多出来的数事先乘如sum
      sum = sum * a % mode;
    b >>= 1;
    a = a * a % mode; // 不断的两两合并再取模，减小a和b的规模
  }
  return sum;
}
void add(ll u, ll v, ll w) {
  cnt++;
  to[cnt] = v;
  weight[cnt] = w;
  nex[cnt] = head[u];
  head[u] = cnt;
}
void init() {
  cnt = 0;
  memset(head, -1, sizeof(head));
}
void SPFA(ll s) {
  memset(dis, 0x3f3f3f3f, sizeof(dis));
  memset(vis, 0, sizeof(vis));
  dis[s] = 0;
  vis[s] = 1;
  queue<ll> q;
  q.push(s);

  while (!q.empty()) {
    ll now = q.front();

    q.pop();
    vis[now] = 0;
    for (int i = head[now]; i != -1; i = nex[i]) {

      if (dis[to[i]] > dis[now] + weight[i]) {
        dis[to[i]] = dis[now] + weight[i];
        if (!vis[to[i]]) {
          q.push(to[i]);
          vis[to[i]] = 1;
        }
      }
    }
  }
}

map<ll, bool> p;
vector<ll> vt;
void findPrime(ll MAX) {
  for (int i = 2; i < MAX; i++) {
    if (!p[i]) {
      vt.push_back(i);
      for (int j = i + i; j < MAX; j += i) {
        p[j] = 1;
      }
    }
  }
}
map<ll, ll> mp;
vector<ll> st;
int main() {
  ll n;
  sl(n);
  findPrime(n + 1);
  for (auto it : vt) {
    if (n % it == 0) {
      st.push_back(it);
      while (n % it == 0) {
        n /= it;
        mp[it]++;
      }
    }
  }
  if (n > 1) {
    mp[n]++;
    st.push_back(n);
  }
  ll ans = 0;
  ll tot = st.size();
  for (ll i = 0; i < tot; i++) {
    ll j = 1;
    while (1) {
      if (mp[st[i]] >= j)
        ans++, mp[st[i]] -= j;
      else
        break;
      j++;
    }
  }
  printf("%d\\n", ans);
  return 0;
}