Problem p02660 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> PP;
#define MOD 1000000007
// #define MOD 998244353
#define INF 1145141919810893364
// #define INF 810114514
#define PI 3.141592653589
#define setdouble setprecision
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define OREP(i, n) for (int i = 1; i <= (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define GOODBYE                                                                \
  do {                                                                         \
    cout << "0" << endl;                                                       \
    return 0;                                                                  \
  } while (false)
#define MM << " " <<
#define Endl endl
#define debug false
#define debug2 false

// 素因数分解
std::vector<long long> prime_factorization(long long N) {
  std::vector<long long> R;
  if (N < 2)
    return R;
  for (int i = 2; i * i <= N; i++) {
    while (N % i == 0) {
      R.push_back(i);
      N /= i;
    }
  }
  if (N != 1) {
    R.push_back(N);
  }
  return R;
}

int main(void) {
  long long N;
  cin >> N;
  vector<long long> P = prime_factorization(N);
  map<long long, long long> M;
  REP(i, P.size()) { M[P[i]]++; }
  long long Ans = 0;
  REP(i, P.size()) {
    long long m = M[P[i]];
    long long B = m;
    if (m == 0)
      continue;
    M[P[i]] = 0;

    for (long long k = 1; k <= B; k++) {
      if (m >= k) {
        Ans++;
        m -= k;
      } else {
        break;
      }
    }
  }
  cout << Ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> PP;
#define MOD 1000000007
// #define MOD 998244353
#define INF 1145141919810893364
// #define INF 810114514
#define PI 3.141592653589
#define setdouble setprecision
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define OREP(i, n) for (int i = 1; i <= (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define GOODBYE                                                                \
  do {                                                                         \
    cout << "0" << endl;                                                       \
    return 0;                                                                  \
  } while (false)
#define MM << " " <<
#define Endl endl
#define debug false
#define debug2 false

// 素因数分解
std::vector<long long> prime_factorization(long long N) {
  std::vector<long long> R;
  if (N < 2)
    return R;
  for (long long i = 2; i * i <= N; i++) {
    while (N % i == 0) {
      R.push_back(i);
      N /= i;
    }
  }
  if (N != 1) {
    R.push_back(N);
  }
  return R;
}

int main(void) {
  long long N;
  cin >> N;
  vector<long long> P = prime_factorization(N);
  map<long long, long long> M;
  REP(i, P.size()) { M[P[i]]++; }
  long long Ans = 0;
  REP(i, P.size()) {
    long long m = M[P[i]];
    long long B = m;
    if (m == 0)
      continue;
    M[P[i]] = 0;

    for (long long k = 1; k <= B; k++) {
      if (m >= k) {
        Ans++;
        m -= k;
      } else {
        break;
      }
    }
  }
  cout << Ans << endl;
  return 0;
}