Problem p02660 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

// #define cerr if(false)cerr
#define watch(x) cerr << "> " << #x << ": " << x << "\n";

using namespace std;

template <typename T>
std::ostream &operator<<(std::ostream &out, vector<T> &v) {
  for (typename vector<T>::size_type i = 0; i < v.size(); ++i)
    out << v[i] << " ";
  out << "\n";
  return out;
}
template <typename T, typename N>
std::ostream &operator<<(std::ostream &out, vector<pair<T, N>> &v) {
  for (size_t i = 0; i < v.size(); ++i)
    out << "(" << v[i].first << ", " << v[i].second << ") ";
  out << "\n";
  return out;
}
template <typename T>
std::ostream &operator<<(std::ostream &out, vector<vector<T>> &v) {
  for (size_t i = 0; i < v.size(); ++i) {
    for (size_t j = 0; j < v[i].size(); ++j) {
      out << v[i][j] << " ";
    }
    out << "\n";
  }
  return out;
}

int main() {

  std::ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  long long n;
  cin >> n;

  map<long long, int> prime_factors_count;
  while (n % 2 == 0) {
    ++prime_factors_count[2];
    n >>= 1;
  }

  for (int i = 3; i * i <= n; i += 2) {
    while (n % i == 0) {
      ++prime_factors_count[i];
      n /= i;
    }
  }

  if (n > 2) {
    ++prime_factors_count[n];
  }

  int ans = 0;
  for (auto it = prime_factors_count.begin(); it != prime_factors_count.end();
       ++it) {
    int occs = it->second;
    int use = 1;
    while (occs >= use) {
      occs -= use;
      ++ans;
      ++use;
    }
  }

  cout << ans << "\n";

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

// #define cerr if(false)cerr
#define watch(x) cerr << "> " << #x << ": " << x << "\n";

using namespace std;

template <typename T>
std::ostream &operator<<(std::ostream &out, vector<T> &v) {
  for (typename vector<T>::size_type i = 0; i < v.size(); ++i)
    out << v[i] << " ";
  out << "\n";
  return out;
}
template <typename T, typename N>
std::ostream &operator<<(std::ostream &out, vector<pair<T, N>> &v) {
  for (size_t i = 0; i < v.size(); ++i)
    out << "(" << v[i].first << ", " << v[i].second << ") ";
  out << "\n";
  return out;
}
template <typename T>
std::ostream &operator<<(std::ostream &out, vector<vector<T>> &v) {
  for (size_t i = 0; i < v.size(); ++i) {
    for (size_t j = 0; j < v[i].size(); ++j) {
      out << v[i][j] << " ";
    }
    out << "\n";
  }
  return out;
}

int main() {

  std::ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  long long n;
  cin >> n;

  map<long long, int> prime_factors_count;
  while (n % 2 == 0) {
    ++prime_factors_count[2];
    n >>= 1;
  }

  for (long long i = 3; i * i <= n; i += 2) {
    while (n % i == 0) {
      ++prime_factors_count[i];
      n /= i;
    }
  }

  if (n > 2) {
    ++prime_factors_count[n];
  }

  int ans = 0;
  for (auto it = prime_factors_count.begin(); it != prime_factors_count.end();
       ++it) {
    int occs = it->second;
    int use = 1;
    while (occs >= use) {
      occs -= use;
      ++ans;
      ++use;
    }
  }

  cout << ans << "\n";

  return 0;
}