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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define ll int64_t
#define str string
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rrep(i, n) for (ll i = 1; i <= n; i++)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mod 1000000007
#define PI 3.141592653589793
#define vec vector
#define dump(x) cerr << #x << "=" << x << endl
using namespace std;

#define INF 1000000000
#define MAX_V 10

bool compare_by_b(pair<string, ll> a, pair<string, ll> b) {
  if (a.second != b.second)
    return a.second < b.second;
  else
    return a.first < b.first;
}

bool my_compare(pair<ll, ll> a, pair<ll, ll> b) {
  if (a.first != b.first)
    return a.first < b.first;
  if (a.second != b.second)
    return a.second > b.second;
  else
    return true;
}

ll modpow(ll a, ll n, ll mod1) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod1;
    a = a * a % mod1;
    n >>= 1;
  }
  return res;
}

ll factorial(ll n) {
  ll x = 1;
  rrep(i, n)(x *= i) %= mod;
  return x;
}

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
const ll MAX = 5100000;
const ll MOD = 1000000007;

ll fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (ll i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}
// 二項係数計算
ll COM(ll n, ll k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

using graph = vector<vector<ll>>;
pair<pair<ll, ll>, ll> p[100010];

map<ll, int> factorize(ll n) {
  map<ll, int> res;

  for (ll i = 2; i * i <= n; i++) {
    if (n % i != 0) {
      continue;
    }
    res[i] = 0;
    while (n % i == 0) {
      n /= i;
      res[i]++;
    }
  }

  if (n != 1)
    res[n] = 1;
  return res;
}

int main() {
  ll n;
  cin >> n;

  auto f = factorize(n);

  int ans = 0;
  for (auto &e : f) {

    int k = e.second;
    int j = 1;
    while (k >= j) {
      ans++;
      k -= j;
      j++;
    }
  }

  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define ll int64_t
#define str string
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rrep(i, n) for (ll i = 1; i <= n; i++)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mod 1000000007
#define PI 3.141592653589793
#define vec vector
#define dump(x) cerr << #x << "=" << x << endl
using namespace std;

#define INF 1000000000
#define MAX_V 10

bool compare_by_b(pair<string, ll> a, pair<string, ll> b) {
  if (a.second != b.second)
    return a.second < b.second;
  else
    return a.first < b.first;
}

bool my_compare(pair<ll, ll> a, pair<ll, ll> b) {
  if (a.first != b.first)
    return a.first < b.first;
  if (a.second != b.second)
    return a.second > b.second;
  else
    return true;
}

ll modpow(ll a, ll n, ll mod1) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod1;
    a = a * a % mod1;
    n >>= 1;
  }
  return res;
}

ll factorial(ll n) {
  ll x = 1;
  rrep(i, n)(x *= i) %= mod;
  return x;
}

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
const ll MAX = 5100000;
const ll MOD = 1000000007;

ll fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (ll i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}
// 二項係数計算
ll COM(ll n, ll k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

using graph = vector<vector<ll>>;
pair<pair<ll, ll>, ll> p[100010];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll n, sum = 0;
  cin >> n;
  ll d = n;
  for (ll i = 2; i <= n / 2; i++) {
    if (d % i == 0) {
      d /= i;
      sum++;
    }
  }
  if (sum == 0 && n != 1)
    sum++;
  cout << sum << endl;
  return 0;
}