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

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
// std::numeric_limits<int>::max(), min()
// std::setprecision(X)
// std::islower(), std::isupper(), std::tolower, std::toupper
// std::accumulate(m.begin(), m.end(), 0LL);
// std::sort(begin(),end()) // 0, 1, 2, 3 ,4 (default, less);
// std::priority_queue<int, vector<int>, less<int>> que; // 4, 3, 2, 1 (defualt,
// less) : if ( std::regex_match(S, std::regex("(dream|dreamer|erase|eraser)*"))
// ) std::cout << "YES" << std::endl;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()

using ll = long long;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

std::map<int64_t, int> prime_factor(int64_t n) {
  std::map<int64_t, int> ret;
  for (int64_t i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      ret[i]++;
      n /= i;
    }
  }
  if (n != 1)
    ret[n] = 1;
  return ret;
}

int main(void) {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  ll N;
  std::cin >> N;

  auto p = prime_factor(N);

  int ans = 0;
  for (auto &&s : p) {
    ans += (int)((std::sqrt(8 * s.second + 1) - 1.) / 2.);
  }
  std::cout << ans << std::endl;
  return 0;
}


Error: TLE

Fixed submission:


#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
// std::numeric_limits<int>::max(), min()
// std::setprecision(X)
// std::islower(), std::isupper(), std::tolower, std::toupper
// std::accumulate(m.begin(), m.end(), 0LL);
// std::sort(begin(),end()) // 0, 1, 2, 3 ,4 (default, less);
// std::priority_queue<int, vector<int>, less<int>> que; // 4, 3, 2, 1 (defualt,
// less) : if ( std::regex_match(S, std::regex("(dream|dreamer|erase|eraser)*"))
// ) std::cout << "YES" << std::endl;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()

using ll = long long;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

std::vector<int> e;

std::vector<ll> pFactorization(ll n) {
  std::vector<ll> rtn;
  for (int i = 2; i * i <= n; i++) {
    if (n % i != 0)
      continue;
    rtn.push_back(i);
    int cnt = 0;
    while (n % i == 0) {
      n /= i;
      cnt++;
    }
    e.push_back(cnt);
  }
  if (n != 1) {
    rtn.push_back(n);
    e.push_back(1);
  }
  return rtn;
}

int main(void) {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  ll N;
  std::cin >> N;

  auto P = pFactorization(N);

  int ans = 0;
  for (int i = 0; i < sz(P); i++) {
    ans += (int)((std::sqrt(8 * e[i] + 1) - 1.) / 2.);
  }
  std::cout << ans << std::endl;
  return 0;
}