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

#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
#define ll long long
#define si(n) scanf("%d", &n);
#define PI acos(-1.0)
#define sl(n) scanf("%lld", &n);
#define sf(n) scanf("%lf", &n);
#define ss(n) scanf("%s", n);
#define for0(i, n) for (ll i = 0; i < n; i++)
#define for1(i, n) for (ll i = 1; i <= n; i++)
#define forc(i, j, n) for (ll i = j; i < n; i++)
#define pb push_back
#define sor(n) sort(n.begin(), n.end())
#define sorcmp(n) sort(n.begin(), n.end(), cmp)
#define rev(n) reverse(n.begin(), n.end())
#define pi(x) printf("%d", x);
#define pl(x) printf("%lld", x);
#define pf(x) printf("%.10lf", x);
#define ps(x) printf("%s", x);
#define en() printf("\\n");
#define random_shuffle(r...) random_shuffle(r, [](int _) { return rand() % _; })
#define SQR(a) (a) * (a)
#define sp(x) fixed << setprecision(x)
#define mod 1000000007
#define endl '\\n'
#define pii pair<int, int>
#define pll pair<ll, ll>
#define bug cout << "Here" << endl
void fastio() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
template <typename T> T POW(T B, T P) {
  if (P == 0)
    return 1;
  if (P & 1)
    return B * POW(B, P - 1);
  else
    return SQR(POW(B, P / 2));
}
ll powmod(ll a, ll b) {
  ll ans = 1;
  while (b) {
    if (b & 1)
      ans = ans * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return ans;
}

bool check(ll n) {
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      return false;
    }
  }

  return true;
}

int main() {
  fastio();

  ll n;
  cin >> n;
  vector<pll> v;
  ll temp = n;
  if (n == 1) {
    return cout << 0 << endl, 0;
  }
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      ll cnt = 0;
      while (n % i == 0) {
        cnt++;
        n /= i;
      }
      v.pb({i, cnt});
    }
  }

  if (n > 1) {
    v.pb({n, 1});
  }

  ll ans = 0;

  for (auto u : v) {
    ll k = 1;
    while (temp > 1) {
      if (u.second < k)
        break;

      temp /= POW(u.first, k);

      u.second -= k;
      k++;
    }
    ans += k - 1;
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
#define ll long long
#define si(n) scanf("%d", &n);
#define PI acos(-1.0)
#define sl(n) scanf("%lld", &n);
#define sf(n) scanf("%lf", &n);
#define ss(n) scanf("%s", n);
#define for0(i, n) for (ll i = 0; i < n; i++)
#define for1(i, n) for (ll i = 1; i <= n; i++)
#define forc(i, j, n) for (ll i = j; i < n; i++)
#define pb push_back
#define sor(n) sort(n.begin(), n.end())
#define sorcmp(n) sort(n.begin(), n.end(), cmp)
#define rev(n) reverse(n.begin(), n.end())
#define pi(x) printf("%d", x);
#define pl(x) printf("%lld", x);
#define pf(x) printf("%.10lf", x);
#define ps(x) printf("%s", x);
#define en() printf("\\n");
#define random_shuffle(r...) random_shuffle(r, [](int _) { return rand() % _; })
#define SQR(a) (a) * (a)
#define sp(x) fixed << setprecision(x)
#define mod 1000000007
#define endl '\\n'
#define pii pair<int, int>
#define pll pair<ll, ll>
#define bug cout << "Here" << endl
void fastio() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
template <typename T> T POW(T B, T P) {
  if (P == 0)
    return 1;
  if (P & 1)
    return B * POW(B, P - 1);
  else
    return SQR(POW(B, P / 2));
}
ll powmod(ll a, ll b) {
  ll ans = 1;
  while (b) {
    if (b & 1)
      ans = ans * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return ans;
}

bitset<100000005> vis;
vector<ll> prime;

void sieve() {
  vis[1] = 1;
  vis[0] = 1;
  vis[2] = 0;
  for (ll i = 4; i <= 100000000; i += 2)
    vis[i] = 1;
  for (ll i = 3; i * i <= 100000000; i += 2) {
    if (!vis[i]) {

      for (ll j = i * i; j <= 100000000; j += i) {
        vis[j] = 1;
      }
    }
  }
  prime.pb(2);
  for (ll i = 3; i <= 100000000; i += 2) {
    if (!vis[i])
      prime.pb(i);
  }
}

bool check(ll n) {
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      return false;
    }
  }

  return true;
}

int main() {
  // fastio();
  sieve();

  ll n;
  cin >> n;
  if (n == 1) {
    return cout << 0 << endl, 0;
  }

  if (check(n)) {
    cout << 1 << endl;
    return 0;
  }
  ll sz = prime.size();

  ll cnt = 0;
  map<ll, ll> mp, vis;
s1:
  for (ll i = 0; i < sz; i++) {
    ll k = 1;
    ll u = prime[i];

    if (u > n)
      break;

    while (n > 1) {

      ll temp = POW(u, k);
      k++;
      if (mp[temp])
        continue;
      mp[temp]++;

      if (n % temp == 0) {
        // cout<<temp<<" "<<n<<endl;
        n /= temp;
        cnt++;
      } else
        break;
    }

    if (n == 1)
      break;
  }

  // cout<<n<<endl;
  vis[n]++;
  if (vis[n] == 2)
    return cout << cnt << endl, 0;

  if (n > 1)
    goto s1;

  cout << cnt << endl;

  return 0;
}