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

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <utility>
#include <vector>
const double PI = acos(-1);
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
const int mod = 1000000007;

// map<string>a
class mint {
  long long x;

public:
  mint(long long x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint &a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint &a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint &a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint &a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint &a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint &a) const {
    mint res(*this);
    return res *= a;
  }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }
  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint &a) { return (*this) *= a.inv(); }
  mint operator/(const mint &a) const {
    mint res(*this);
    return res /= a;
  }

  friend ostream &operator<<(ostream &os, const mint &m) {
    os << m.x;
    return os;
  }
};
int ctoi(char c) {
  if (c >= '0' && c <= '9') {
    return c - '0';
  }
  return 0;
}
// 組み合わせ
void recursive_comb(vector<int> indexes, int s, int rest,
                    std::function<void(vector<int>)> f) {
  if (rest == 0) {
    f(indexes);
  } else {
    if (s < 0)
      return;
    recursive_comb(indexes, s - 1, rest, f);
    indexes[rest - 1] = s;
    recursive_comb(indexes, s - 1, rest - 1, f);
  }
}

// nCkの組み合わせに対して処理を実行する
void foreach_comb(int n, int k, std::function<void(vector<int>)> f) {
  vector<int> indexes(k);
  recursive_comb(indexes, n - 1, k, f);
}

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

int main() {
  ll N;
  cin >> N;
  ll tmp = N;
  ll check = 0;
  map<ll, ll> m;
  if (N == 1) {

    cout << 0;
    return 0;
  }
  for (int i = 2; i <= tmp; i++) {

    while (1) {
      if (N % i == 0) {
        N = N / i;
        m[i]++;
        check = 1;
      } else {
        break;
      }
    }
    if (N == 1) {
      break;
    }
    if (pow(tmp, 0.5) < i) {
      check = 1;
      m[100000000000]++;
      break;
    }
  }
  if (check == 0) {
    cout << 1;
    return 0;
  }
  ll ans = 0;
  for (auto i : m) {
    for (int j = 1; j <= i.second; j++) {
      if (i.second >= (j + 1) * j / 2 && i.second < (j + 1) * (j + 2) / 2) {
        ans += j;
        break;
      }
    }
  }
  cout << ans;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <utility>
#include <vector>
const double PI = acos(-1);
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
const int mod = 1000000007;

// map<string>a
class mint {
  long long x;

public:
  mint(long long x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint &a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint &a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint &a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint &a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint &a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint &a) const {
    mint res(*this);
    return res *= a;
  }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }
  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint &a) { return (*this) *= a.inv(); }
  mint operator/(const mint &a) const {
    mint res(*this);
    return res /= a;
  }

  friend ostream &operator<<(ostream &os, const mint &m) {
    os << m.x;
    return os;
  }
};
int ctoi(char c) {
  if (c >= '0' && c <= '9') {
    return c - '0';
  }
  return 0;
}
// 組み合わせ
void recursive_comb(vector<int> indexes, int s, int rest,
                    std::function<void(vector<int>)> f) {
  if (rest == 0) {
    f(indexes);
  } else {
    if (s < 0)
      return;
    recursive_comb(indexes, s - 1, rest, f);
    indexes[rest - 1] = s;
    recursive_comb(indexes, s - 1, rest - 1, f);
  }
}

// nCkの組み合わせに対して処理を実行する
void foreach_comb(int n, int k, std::function<void(vector<int>)> f) {
  vector<int> indexes(k);
  recursive_comb(indexes, n - 1, k, f);
}

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

int main() {
  ll N;
  cin >> N;
  ll tmp = N;
  ll check = 0;
  map<ll, ll> m;
  if (N == 1) {

    cout << 0;
    return 0;
  }
  for (int i = 2; i <= N; i++) {
    while (1) {
      if (N % i == 0) {
        N = N / i;
        m[i]++;
        check = 1;
      } else {
        break;
      }
    }
  }
  if (check == 0) {
    cout << 1;
    return 0;
  }
  ll ans = 0;
  for (auto i : m) {
    for (int j = 1; j <= i.second; j++) {
      if (i.second >= (j + 1) * j / 2 && i.second < (j + 1) * (j + 2) / 2) {
        ans += j;
        break;
      }
    }
  }
  cout << ans;
}