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

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define FOR(i, min, max) for (int i = (min); i < (max); ++i)
#define FORE(i, min, max) for (int i = (min); i <= (max); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPV(vec, i) for (int i = 0; i < (int)(vec.size()); ++i)
#define FOR_EACH(vec, it)                                                      \\
  for (typeof((vec).begin()) it = (vec).begin(); it != (vec).end(); ++it)

using namespace std;
static const double EPS = 1e-12;
typedef long long ll;

int main(void) {
  ll n;
  cin >> n;
  map<ll, int> c;
  ll k = (ll)floor(sqrt((double)n) + EPS);
  for (ll i = 2; i <= k; ++i) {
    while (n % i == 0) {
      n /= i;
      c[i]++;
    }
  }
  if (n != 1)
    c[n]++;
  int ret = 0;
  for (auto &p : c) {
    int n = p.second;
    int v = 0;
    for (int m = 1; m * (m + 1) / 2 <= n; m++) {
      v = m;
    }
    assert(v > 0);
    ret += v;
  }
  cout << ret << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define FOR(i, min, max) for (int i = (min); i < (max); ++i)
#define FORE(i, min, max) for (int i = (min); i <= (max); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPV(vec, i) for (int i = 0; i < (int)(vec.size()); ++i)
#define FOR_EACH(vec, it)                                                      \\
  for (typeof((vec).begin()) it = (vec).begin(); it != (vec).end(); ++it)

using namespace std;
static const double EPS = 1e-12;
typedef long long ll;

bool isprime(ll n) {
  if (n < 2)
    return false;
  int k = (ll)floor(sqrt((double)n) + EPS);
  FORE(i, 2, k) {
    if (n % i == 0)
      return false;
  }
  return true;
}

vector<int> getPrime(int n) {
  vector<int> ret;
  FORE(i, 2, n) {
    bool ok = true;
    int k = (int)floor(sqrt((double)i) + EPS);
    for (unsigned int j = 0; j < ret.size() && ret[j] <= k; ++j) {
      if (i % ret[j] == 0) {
        ok = false;
        break;
      }
    }
    if (ok)
      ret.push_back(i);
  }
  return ret;
}

// 素因数分解したときの個数
int factnums(ll n) {
  int ret = 0;
  ll k = (ll)floor(sqrt((double)n) + EPS);
  for (ll i = 2; i <= k; ++i) {
    while (n % i == 0) {
      n /= i;
      ++ret;
    }
  }
  if (n != 1)
    ++ret;
  return ret;
}

// 素因数分解(ll)
vector<ll> factorize(ll n) {
  static vector<ll> primes;
  static ll k_max = 1;

  ll k = (ll)floor(sqrt((double)n) + EPS);
  for (ll i = k_max + 1; i <= k; ++i) {
    bool ok = true;
    REPV(primes, j) {
      if (i % primes[j] == 0) {
        ok = false;
        break;
      }
    }
    if (ok)
      primes.push_back(i);
  }

  k_max = max(k_max, k);

  vector<ll> ret;
  for (int i = 0; i < (int)primes.size() && primes[i] <= k; ++i) {
    while (n % primes[i] == 0) {
      n /= primes[i];
      ret.push_back(primes[i]);
    }
  }
  if (n != 1)
    ret.push_back(n);
  return ret;
}

int main(void) {
  ll N;
  cin >> N;
  vector<ll> w = factorize(N);
  map<ll, int> c;
  for (auto &n : w)
    c[n]++;

  int ret = 0;
  for (auto &p : c) {
    int n = p.second;
    int v = 0;
    for (int m = 1; m * (m + 1) / 2 <= n; m++) {
      v = m;
    }
    assert(v > 0);
    ret += v;
  }
  cout << ret << endl;
  return 0;
}