Problem p02660 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<ll, ll> pll;
#define fs first
#define re resize
#define sc second
#define pb push_back
#define eb emplace_back
#define MP make_pair
#define all(x) x.begin(), x.end()
#define mod 1000000007
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define REP(i, n) FOR(i, 0, n - 1)
#define ins insert
#define mod 1000000007
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << std::endl;
  // use cerr if u want to display at the bottom
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif
#define endl "\n"
void fastIO() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}
ll modexpo(ll a, ll b) {
  if (a == 0)
    return 0;
  if (a == 1 or b == 0)
    return 1;
  if (b == 1)
    return a;
  if (b & 1)
    return (a % mod * modexpo(a, b - 1) % mod) % mod;
  return modexpo(a % mod * a % mod, b / 2) % mod;
}
void printv(vll v) {
  for (auto &x : v) {
    cout << x << " ";
  }
  cout << endl;
}
string to_string1(ll x) {
  stringstream ss;
  ss << x;
  return ss.str();
}
ll to_integer(string s) {
  stringstream ss(s);
  ll x;
  ss >> x;
  return x;
}
#define tp9 1000000000
const ll N = 1e12 + 5;
ld pi = 3.1415926535;
void printYes() { cout << "Yes\n"; }
void printNo() { cout << "No\n"; }
ll t, n, p;
string s;

ll solve(ll n) {
  ll ans = 0;
  map<ll, ll> mp;
  while (n % 2 == 0) {
    mp[2]++;
    n >>= 1;
  }
  for (ll i = 3; i <= n; i++) {
    while (n % i == 0) {
      mp[i]++;
      n /= i;
    }
  }
  if (n > 2) {
    mp[n]++;
  }
  for (auto p : mp) {
    ll j = p.sc, i = 1;
    while ((i * (i + 1) / 2) <= j) {
      i++;
    }
    ans += i - 1;
  }
  return ans;
}

int main() {
  ll n;
  while (cin >> n)
    cout << solve(n) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<ll, ll> pll;
#define fs first
#define re resize
#define sc second
#define pb push_back
#define eb emplace_back
#define MP make_pair
#define all(x) x.begin(), x.end()
#define mod 1000000007
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define REP(i, n) FOR(i, 0, n - 1)
#define ins insert
#define mod 1000000007
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << std::endl;
  // use cerr if u want to display at the bottom
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif
#define endl "\n"
void fastIO() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}
ll modexpo(ll a, ll b) {
  if (a == 0)
    return 0;
  if (a == 1 or b == 0)
    return 1;
  if (b == 1)
    return a;
  if (b & 1)
    return (a % mod * modexpo(a, b - 1) % mod) % mod;
  return modexpo(a % mod * a % mod, b / 2) % mod;
}
void printv(vll v) {
  for (auto &x : v) {
    cout << x << " ";
  }
  cout << endl;
}
string to_string1(ll x) {
  stringstream ss;
  ss << x;
  return ss.str();
}
ll to_integer(string s) {
  stringstream ss(s);
  ll x;
  ss >> x;
  return x;
}
#define tp9 1000000000
const ll N = 1e12 + 5;
ld pi = 3.1415926535;
void printYes() { cout << "Yes\n"; }
void printNo() { cout << "No\n"; }
ll t, n, p;
string s;

ll solve(ll n) {
  ll ans = 0;
  map<ll, ll> mp;
  while (n % 2 == 0) {
    mp[2]++;
    n >>= 1;
  }
  for (ll i = 3; i * i <= n; i++) {
    while (n % i == 0) {
      mp[i]++;
      n /= i;
    }
  }
  if (n > 2) {
    mp[n]++;
  }
  for (auto p : mp) {
    ll j = p.sc, i = 1;
    while ((i * (i + 1) / 2) <= j) {
      i++;
    }
    ans += i - 1;
  }
  return ans;
}

int main() {
  ll n;
  while (cin >> n)
    cout << solve(n) << endl;
  return 0;
}