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

// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
const long long int mod1 = 1000000007;
const long long int mod2 = 998244353;
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
#define pp pop_back
#define ff first
#define ss second
#define ll long long int
#define ull unsigned long long int
#define all(x) x.begin(), x.end()
#define sz(x) x.size()
#define FOR(i, a, b) for (int i = (a), _##i = (b); i <= _##i; ++i)
#define FORD(i, a, b) for (int i = (a), _##i = (b); i >= _##i; --i)
#define REP(i, a) for (int i = 0, _##i = (a); i < _##i; ++i)
#define REPD(i, n) for (int i = (n)-1; i >= 0; --i)
#define pll pair<ll, ll>
#define ln endl
ll gcd(ll a, ll b) {
  ll c;
  while (b) {
    c = b;
    b = a % b;
    a = c;
  }
  return a;
}
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
ll power1(ll a, ll b) {
  ll out = 1;
  while (b) {
    if (b & 1) {
      out *= a;
    }
    b >>= 1;
    a *= a;
  }
  return out;
}
ll power2(ll a, ll b, ll m) {
  a %= m;
  ll out = 1;
  while (b) {
    if (b & 1) {
      out *= a;
      out %= m;
    }
    b >>= 1;
    a = (a * a) % m;
  }
  return out;
}
ll modi(ll x, ll p) { return power2(x, p - 2, p); }
__int128 fun(__int128 a, __int128 b, __int128 mod) {
  return (a * a + b) % mod + 1;
}

__int128 pollard_rho(__int128 num, __int128 comp) {
  if (num == 1)
    return 1;
  if (num % 2 == 0)
    return 2;

  srand(time(0));
  __int128 y = rand() % num + 1, c = rand() % num + 1, x, gcd = 1,
           sq = sqrt(num) + 100;
  x = y;

  int count = 0;

  while (gcd == 1) {
    if (count >= sq)
      return num;
    count++;
    x = fun(x, c, num);
    y = fun(fun(y, c, num), c, num);
    if (x == y)
      gcd = num;
    else
      gcd = __gcd(abs(x - y), num);
    if (gcd != 1) {
      if (gcd == num || gcd == comp) {
        y = rand() % num + 1;
        x = y;
        gcd = 1;
      } else {
        break;
      }
    }
  }

  return gcd;
}

__int128 modexp(__int128 base, __int128 power, __int128 mod = 1000000007) {
  __int128 res = 1;
  while (power) {
    if (power & 1)
      res = (res * base) % mod;
    power >>= 1;
    base = (base * base) % mod;
  }
  return res;
}

bool miller_rabin(__int128 num, __int128 k) {
  __int128 s = 0, d = num - 1, one = 1, temp;
  while (!(d & 1))
    d >>= 1, s++;

  bool flag = true;
  temp = modexp(k, d, num);
  if (temp == 1 || temp == num - 1)
    return 1;
  for (int i = 0; i < s; i++) {
    temp = (temp * temp) % num;
    if (temp == 1)
      return 0;
    if (temp == num - 1)
      return 1;
  }
  return 0;
}

bool prime(__int128 num) {
  vector<int> A{2,  3,  5,  7,  11, 13, 17, 19, 23, 29,
                31, 37, 41, 43, 47, 53, 59, 61, 67};
  for (auto x : A)
    if (x == num)
      return 1;
  if (!(num & 1))
    return 0;

  bool flag = true;
  for (auto x : A)
    flag &= miller_rabin(num, x);
  return flag;
}
vector<ll> factor;
void prime_factorization(__int128 x) {
  if (x == 0)
    return;

  while (x != 1) {
    __int128 y = pollard_rho(x, 0);
    int count = 1;
    while (!prime(y))
      y = pollard_rho(y, y);
    x /= y;
    while (x % y == 0) {
      x /= y;
      count++;
    }
    factor.push_back(count);
  }
}
int main() {
  fast;
  ll tt = 1;
  // cin >> tt;
  while (tt--) {
    ll n;
    cin >> n;
    vector<ll> v;
    ll cnt = 0;
    if (n == 1) {
      cout << 0 << endl;
      return 0;
    }
    prime_factorization(n);
    ll out = 0;
    for (auto q : factor) {
      ll qw = (sqrtl(1.0 + 8.0 * (long double)q) - 1.0) / 2.0;
      while (((qw + 1) * (qw + 2)) / 2 <= q) {
        qw++;
      }
      out += qw;
    }
    cout << out << endl;
  }
}

Error: TLE

Fixed submission:

// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
const long long int mod1 = 1000000007;
const long long int mod2 = 998244353;
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
#define pp pop_back
#define ff first
#define ss second
#define ll long long int
#define ull unsigned long long int
#define all(x) x.begin(), x.end()
#define sz(x) x.size()
#define FOR(i, a, b) for (int i = (a), _##i = (b); i <= _##i; ++i)
#define FORD(i, a, b) for (int i = (a), _##i = (b); i >= _##i; --i)
#define REP(i, a) for (int i = 0, _##i = (a); i < _##i; ++i)
#define REPD(i, n) for (int i = (n)-1; i >= 0; --i)
#define pll pair<ll, ll>
#define ln endl
ll gcd(ll a, ll b) {
  ll c;
  while (b) {
    c = b;
    b = a % b;
    a = c;
  }
  return a;
}
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
ll power1(ll a, ll b) {
  ll out = 1;
  while (b) {
    if (b & 1) {
      out *= a;
    }
    b >>= 1;
    a *= a;
  }
  return out;
}
ll power2(ll a, ll b, ll m) {
  a %= m;
  ll out = 1;
  while (b) {
    if (b & 1) {
      out *= a;
      out %= m;
    }
    b >>= 1;
    a = (a * a) % m;
  }
  return out;
}
ll modi(ll x, ll p) { return power2(x, p - 2, p); }
__int128 fun(__int128 a, __int128 b, __int128 mod) {
  return (a * a + b) % mod + 1;
}

__int128 pollard_rho(__int128 num, __int128 comp) {
  if (num == 1)
    return 1;
  if (num % 2 == 0)
    return 2;

  srand(time(0));
  __int128 y = rand() % num + 1, c = rand() % num + 1, x, gcd = 1,
           sq = sqrt(num) + 100;
  x = y;

  int count = 0;

  while (gcd == 1) {
    if (count >= sq)
      return num;
    count++;
    x = fun(x, c, num);
    y = fun(fun(y, c, num), c, num);
    if (x == y)
      gcd = num;
    else
      gcd = __gcd(abs(x - y), num);
    if (gcd != 1) {
      if (gcd == num || gcd == comp) {
        y = rand() % num + 1;
        x = y;
        gcd = 1;
      } else {
        break;
      }
    }
  }

  return gcd;
}

__int128 modexp(__int128 base, __int128 power, __int128 mod = 1000000007) {
  __int128 res = 1;
  while (power) {
    if (power & 1)
      res = (res * base) % mod;
    power >>= 1;
    base = (base * base) % mod;
  }
  return res;
}

bool miller_rabin(__int128 num, __int128 k) {
  __int128 s = 0, d = num - 1, one = 1, temp;
  while (!(d & 1))
    d >>= 1, s++;

  bool flag = true;
  temp = modexp(k, d, num);
  if (temp == 1 || temp == num - 1)
    return 1;
  for (int i = 0; i < s; i++) {
    temp = (temp * temp) % num;
    if (temp == 1)
      return 0;
    if (temp == num - 1)
      return 1;
  }
  return 0;
}

bool prime(__int128 num) {
  vector<int> A{2,  3,  5,  7,  11, 13, 17, 19, 23, 29,
                31, 37, 41, 43, 47, 53, 59, 61, 67};
  for (auto x : A)
    if (x == num)
      return 1;
  if (!(num & 1))
    return 0;

  bool flag = true;
  for (auto x : A)
    flag &= miller_rabin(num, x);
  return flag;
}
vector<pair<__int128, int>> factor;
void prime_factorization(__int128 x) {
  if (x == 0)
    return;

  while (x != 1) {
    __int128 y = pollard_rho(x, 0);
    int count = 1;
    while (!prime(y))
      y = pollard_rho(y, y);
    x /= y;
    while (x % y == 0) {
      x /= y;
      count++;
    }
    factor.push_back({y, count});
  }
}
int main() {
  fast;
  ll tt = 1;
  // cin >> tt;
  while (tt--) {
    ll n;
    cin >> n;
    vector<ll> v;
    ll cnt = 0;
    if (n == 1) {
      cout << 0 << endl;
      return 0;
    }
    if (n % 2 == 0) {
      while (n % 2 == 0) {
        n /= 2;
        cnt++;
      }
      v.pb(cnt);
    }

    for (int i = 3; i * i <= n; i += 2) {
      cnt = 0;
      if (n % i == 0) {
        while (n % i == 0) {
          cnt++;
          n /= i;
        }
        v.pb(cnt);
      }
    }
    if (n > 2) {
      v.pb(1);
    }
    ll out = 0;
    for (auto q : v) {
      ll qw = (sqrtl(1.0 + 8.0 * (long double)q) - 1.0) / 2.0;
      while (((qw + 1) * (qw + 2)) / 2 <= q) {
        qw++;
      }
      out += qw;
    }
    cout << out << endl;
  }
}