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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

// #include <bits/stdc++.h>
#define maxs(x, y) x = max(x, y)
#define mins(x, y) x = min(x, y)
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define repr(i, n) for (int i = (n)-1; i >= 0; i--)
#define FOR(i, i0, n) for (int(i) = (i0); (i) < (n); (i)++)
#define FORR(i, i0, n) for (int(i) = (n)-1; (i) >= (i0); (i)--)
#define SORT(x) sort(x.begin(), x.end())
#define SORTR(x) sort(x.begin(), x.end(), greater<int>())
#define rn return
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#include <time.h>
typedef std::pair<int, int> pii;
using namespace std;
using ll = long long;
#define PI 3.14159265358979323846264338327950L
const int mod = 1000000007;

void solve() {
  ll n;
  cin >> n;

  map<int, int> ps;

  int c2 = 0;
  while (n % 2 == 0) {
    c2++;
    n = n / 2;
  }
  if (c2)
    ps[2] = c2;

  for (ll i = 3; i * i <= n; i = i + 2) {
    int c = 0;
    while (n % i == 0) {
      c++;
      n = n / i;
    }
    if (c)
      ps[i] = c;
  }
  if (n != 1)
    ps[n] = 1;

  int ans = 0;

  for (auto e : ps) {
    int t = e.second;
    int i = 1;
    while (t) {
      while (t - i <= i && t - i > 0) {
        i++;
      }

      while (t - i > i || t - i == 0) {
        t -= i;
        i++;
        ans++;
      }
    }
  }
  cout << ans;
}

int main() {
  int T;
  T = 1;
  while (T--) {
    solve();
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

// #include <bits/stdc++.h>
#define maxs(x, y) x = max(x, y)
#define mins(x, y) x = min(x, y)
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define repr(i, n) for (int i = (n)-1; i >= 0; i--)
#define FOR(i, i0, n) for (int(i) = (i0); (i) < (n); (i)++)
#define FORR(i, i0, n) for (int(i) = (n)-1; (i) >= (i0); (i)--)
#define SORT(x) sort(x.begin(), x.end())
#define SORTR(x) sort(x.begin(), x.end(), greater<int>())
#define rn return
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#include <time.h>
typedef std::pair<int, int> pii;
using namespace std;
using ll = long long;
#define PI 3.14159265358979323846264338327950L
const int mod = 1000000007;

void solve() {
  ll n;
  cin >> n;
  map<int, int> ps;

  int c2 = 0;
  while (n % 2 == 0) {
    c2++;
    n = n / 2;
  }
  if (c2)
    ps[2] = c2;

  for (int i = 3; i * i <= n; i = i + 2) {
    int c = 0;
    while (n % i == 0) {
      c++;
      n = n / i;
    }
    if (c)
      ps[i] = c;
  }
  if (n != 1)
    ps[n] = 1;

  int ans = 0;

  for (auto e : ps) {
    int t = e.second;
    int i = 1;
    while (t) {
      while (t - i <= i && t - i != 0) {
        i++;
      }

      while (t - i > i || t - i == 0) {
        t -= i;
        i++;
        ans++;
      }
    }
  }
  cout << ans;
}

int main() {
  int T;
  T = 1;
  while (T--) {
    solve();
  }
}