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

#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef long double ld;

#define Pb push_back
#define Mp make_pair

#define ff first
#define ss second

#define Max(x, y, z) max(x, max(y, z))
#define Min(x, y, z) min(x, min(y, z))

#define INF LONG_LONG_MAX
#define MINF LONG_LONG_MIN

#define endl "\\n"
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)

#define db1(x) cout << #x << " : " << x << endl;
#define db2(x, y) cout << #x << " : " << x << "\\t" << #y << " : " << y << endl;
#define db3(x, y, z)                                                           \\
  cout << #x << " : " << x << "\\t" << #y << " : " << y << "\\t" << #z << " : "  \\
       << z << endl;

#define int ll

const int mod = 1e9 + 7;

void solve() {
  int n;
  cin >> n;
  int ans = 0;
  for (int i = 2; (i * i) <= n; i++) {
    int cnt = 0;
    while (n % i == 0) {
      n /= i;
      cnt++;
    }
    int l = 1, r = (cnt / 2) + 1;
    int p = 0;
    if (cnt == 0)
      continue;
    while (l <= r) {
      int mid = (l + r) / 2;
      int s = mid * 1LL * (mid + 1) / 2;
      if (s == cnt) {
        p = mid;
        break;
      }
      if (s < cnt) {
        p = mid;
        l = mid + 1;
      } else {
        r = mid - 1;
      }
    }
    ans += p;
    if (n < i)
      break;
  }
  if (n != 0 && n != 1)
    ans++;
  cout << ans;
}

signed main() {
  IOS;
  // freopen("input.txt", "rt", stdin);
  // freopen("output.txt", "wt", stdout);
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
/*==============================================================================================================================================================*/


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef long double ld;

#define Pb push_back
#define Mp make_pair

#define ff first
#define ss second

#define Max(x, y, z) max(x, max(y, z))
#define Min(x, y, z) min(x, min(y, z))

#define INF LONG_LONG_MAX
#define MINF LONG_LONG_MIN

#define endl "\\n"
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)

#define db1(x) cout << #x << " : " << x << endl;
#define db2(x, y) cout << #x << " : " << x << "\\t" << #y << " : " << y << endl;
#define db3(x, y, z)                                                           \\
  cout << #x << " : " << x << "\\t" << #y << " : " << y << "\\t" << #z << " : "  \\
       << z << endl;

const int mod = 1e9 + 7;
const ll N = 1e6 + 5;
vector<ll> f(N);

void solve() {
  ll n;
  cin >> n;
  int ans = 0;
  for (int i = 2; (i * i) <= n; i++) {
    int cnt = 0;
    while (n % i == 0) {
      n /= i;
      cnt++;
    }
    ans += (upper_bound(f.begin(), f.end(), cnt) - f.begin()) - 1;
    if (n < i)
      break;
  }
  if (n != 0 && n != 1)
    ans++;
  cout << ans;
}

signed main() {
  IOS;
  // freopen("input.txt", "rt", stdin);
  // freopen("output.txt", "wt", stdout);
  int t = 1;
  // cin >> t;
  for (int i = 1; i < 1e6; i++) {
    f[i] = i * 1LL * (i + 1) / 2;
  }
  while (t--) {
    solve();
  }
  return 0;
}
/*==============================================================================================================================================================*/