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

// Created by Nandu Vinodan
#include <bits/stdc++.h>

#define db1(x) cout << #x << "=" << x << '\\n'
#define db2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << '\\n'
#define db3(x, y, z)                                                           \\
  cout << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \\
       << '\\n'
#define rep(i, a, n) for (int i = a; i <= (n); ++i)
#define repD(i, a, n) for (int i = a; i >= (n); --i)
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define vi vector<int>
#define pi pair<int, int>
#define INF INT_MAX
#define _INF INT_MIN
#define TC 0

using namespace std;
using ll = long long;

#define MAXN 100001

int spf[MAXN];

void sieve() {
  spf[1] = 1;
  for (int i = 2; i < MAXN; i++)
    spf[i] = i;

  for (int i = 4; i < MAXN; i += 2)
    spf[i] = 2;

  for (int i = 3; i * i < MAXN; i++) {
    if (spf[i] == i) {
      for (int j = i * i; j < MAXN; j += i)
        if (spf[j] == j)
          spf[j] = i;
    }
  }
}

vector<int> getFactorization(ll x) {
  vector<int> ret;
  while (x != 1) {
    ret.push_back(spf[x]);
    x = x / spf[x];
  }
  return ret;
}

vector<ll> primeFactors(ll n) {
  vector<ll> res;
  while (n % 2 == 0) {
    res.push_back(2);
    n = n / 2;
  }
  for (ll i = 3; i * i <= n; i = i + 2) {
    while (n % i == 0) {
      res.push_back(i);
      n = n / i;
    }
  }
  if (n > 2)
    res.push_back(n);
  return res;
}

void testcase() {
  // sieve();
  ll n;
  cin >> n;
  vector<ll> fac = primeFactors(n);
  sort(fac.begin(), fac.end());
  int m = fac.size();

  int i = 0;
  int res = 0;
  while (i < m) {
    ll cur = fac[i];
    int ct = 0;
    while (i < m && cur == fac[i]) {
      ct++;
      i++;
    }
    int sum = 0, c = 0;
    while (true) {
      sum += c;
      if (sum > ct) {
        c--;
        break;
      }
      c++;
    }
    res += c;
  }
  cout << res;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  if (TC)
    cin >> t;
  while (t--) {
    testcase();
  }
  return 0;
}

Error: TLE

Fixed submission:

// Created by Nandu Vinodan
#include <bits/stdc++.h>

#define db1(x) cout << #x << "=" << x << '\\n'
#define db2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << '\\n'
#define db3(x, y, z)                                                           \\
  cout << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \\
       << '\\n'
#define rep(i, a, n) for (int i = a; i <= (n); ++i)
#define repD(i, a, n) for (int i = a; i >= (n); --i)
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define vi vector<int>
#define pi pair<int, int>
#define INF INT_MAX
#define _INF INT_MIN
#define TC 0

using namespace std;
using ll = long long;

#define MAXN 100001

int spf[MAXN];

void sieve() {
  spf[1] = 1;
  for (int i = 2; i < MAXN; i++)
    spf[i] = i;

  for (int i = 4; i < MAXN; i += 2)
    spf[i] = 2;

  for (int i = 3; i * i < MAXN; i++) {
    if (spf[i] == i) {
      for (int j = i * i; j < MAXN; j += i)
        if (spf[j] == j)
          spf[j] = i;
    }
  }
}

vector<int> getFactorization(ll x) {
  vector<int> ret;
  while (x != 1) {
    ret.push_back(spf[x]);
    x = x / spf[x];
  }
  return ret;
}

vector<ll> primeFactors(ll n) {
  vector<ll> res;
  while (n % 2 == 0) {
    res.push_back(2);
    n = n / 2;
  }
  for (ll i = 3; i * i <= n; i = i + 2) {
    while (n % i == 0) {
      res.push_back(i);
      n = n / i;
    }
  }
  if (n > 2)
    res.push_back(n);
  return res;
}

void testcase() {
  // sieve();
  ll n;
  cin >> n;
  vector<ll> fac = primeFactors(n);
  sort(fac.begin(), fac.end());
  int m = fac.size();
  int i = 0;
  int res = 0;
  while (i < m) {
    // db1(fac[i]);
    int cur = fac[i];
    int ct = 0;
    while (i < m && cur == fac[i]) {
      ct++;
      i++;
    }
    // db1(ct);
    int sum = 0, c = 0;
    while (true) {
      sum += c;
      if (sum > ct) {
        c--;
        break;
      }
      c++;
    }
    res += c;
  }
  cout << res;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  if (TC)
    cin >> t;
  while (t--) {
    testcase();
  }
  return 0;
}