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

/*
  GOOD is not Enough
  You've got to be GREAT.
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
// #include<boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;
using namespace __gnu_pbds;
using namespace std;
#define int long long
#define mod 1000000007
#define FAST ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define f(i, n) for (int i = 0; i < n; i++)
#define fp(i, k, n) for (int i = k; i <= n; i++)
#define fr(i, k, n) for (int i = k; i >= n; i--)
#define pb push_back
#define pii pair<int, int>
#define dbg(x) cout << (#x) << " is " << (x) << '\\n'
#define F first
#define S second
#define sz(x) (int)(x).size()
#define lb lower_bound
#define ub upper_bound
#define all(a) a.begin(), a.end()
typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

bool isPrime(int n) {
  if (n == 2 || n == 3)
    return true;
  if (n <= 1)
    return false;
  fp(i, 2, (int)sqrt(n)) {
    if (n % i == 0)
      return false;
  }
  return true;
}
void solve() {
  int n;
  cin >> n;
  int ans = 0;
  if (n == 1) {
    cout << 0 << '\\n';
    return;
  }
  if (isPrime(n)) {
    cout << 1 << '\\n';
    return;
  }
  fp(i, 2, sqrt(n)) {
    int cnt = 0;
    //   if(i==3)
    //     cout << "h" << n << '\\n';
    if ((n % i) == 0) {
      //   dbg(i);
      while (n % i == 0) {
        cnt++;
        n /= i;
        //   dbg(n);
      }
      ans += ((int)sqrt(8 * cnt + 1) - 1) / 2;
    }
  }
  if (n > 1)
    ans++;
  cout << ans << '\\n';
  return;
}

signed main() {
  FAST int T = 1;
  // cin >> T;
  while (T--) {
    solve();
  }
  return 0;
}


Error: TLE

Fixed submission:

/*
  GOOD is not Enough
  You've got to be GREAT.
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
// #include<boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;
using namespace __gnu_pbds;
using namespace std;
#define int long long
#define mod 1000000007
#define FAST ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define f(i, n) for (int i = 0; i < n; i++)
#define fp(i, k, n) for (int i = k; i <= n; i++)
#define fr(i, k, n) for (int i = k; i >= n; i--)
#define pb push_back
#define pii pair<int, int>
#define dbg(x) cout << (#x) << " is " << (x) << '\\n'
#define F first
#define S second
#define sz(x) (int)(x).size()
#define lb lower_bound
#define ub upper_bound
#define all(a) a.begin(), a.end()
typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

const int MAXM = 10000005;
vector<int> prime;
bool is_composite[MAXM];

void sieve(int n) {
  fill(is_composite, is_composite + n, false);
  for (int i = 2; i < n; ++i) {
    if (!is_composite[i])
      prime.push_back(i);
    for (int j = 0; j < prime.size() && i * prime[j] < n; ++j) {
      is_composite[i * prime[j]] = true;
      if (i % prime[j] == 0)
        break;
    }
  }
}

bool isPrime(int n) {
  if (n == 2 || n == 3)
    return true;
  if (n <= 1)
    return false;
  fp(i, 2, (int)sqrt(n)) {
    if (n % i == 0)
      return false;
  }
  return true;
}
void solve() {
  int n;
  cin >> n;
  int ans = 0;
  if (n == 1) {
    cout << 0 << '\\n';
    return;
  }
  if (isPrime(n)) {
    cout << 1 << '\\n';
    return;
  }
  sieve((int)10000000);
  //  cout <<  prime[prime.size()-1];
  for (auto i : prime) {
    int cnt = 0;
    //   if(i==3)
    //     cout << "h" << n << '\\n';
    if ((n % i) == 0) {
      //   dbg(i);
      while (n % i == 0) {
        cnt++;
        n /= i;
        //   dbg(n);
      }
      ans += ((int)sqrt(8 * cnt + 1) - 1) / 2;
    }
  }
  fp(i, prime.back(), n) {
    int cnt = 0;
    //   if(i==3)
    //     cout << "h" << n << '\\n';
    if ((n % i) == 0) {
      //   dbg(i);
      while (n % i == 0) {
        cnt++;
        n /= i;
        //   dbg(n);
      }
      ans += ((int)sqrt(8 * cnt + 1) - 1) / 2;
    }
  }
  cout << ans << '\\n';
  return;
}

signed main() {
  FAST int T = 1;
  // cin >> T;
  while (T--) {
    solve();
  }
  return 0;
}