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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<vector<ll>> Matrix;
const char wall = '#';
vector<vector<char>> fields;
vector<vector<bool>> used;
vector<vector<ll>> dist;
const int dx[4] = {0, -1, 0, 1};
const int dy[4] = {1, 0, -1, 0};
vector<ll> A;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
vector<pair<ll, ll>> factorize(ll n) {
  vector<pair<ll, ll>> res;
  for (ll i = 2; i * i <= n; ++i) {
    if (n % i)
      continue;
    res.emplace_back(i, 0);
    while (n % i == 0) {
      n /= i;
      res.back().second++;
    }
  }
  if (n != 1)
    res.emplace_back(n, 1);
  return res;
}
long long fastpow(long long x, long long n) {
  long long ret = 1;
  while (n > 0) {
    if (n & 1)
      ret *= x;
    x *= x;
    n >>= 1;
  }
  return ret;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  cin >> n;
  vector<P> fact = factorize(n);
  map<ll, ll> mp;
  vector<ll> candidates;
  set<ll> s;
  for (auto f : fact) {
    rep1(i, f.second) { s.insert(fastpow(f.first, i)); }
  }
  ll ans = 0;
  for (auto z : s) {
    if (n % z)
      continue;
    n /= z;
    ans++;
    if (n < z)
      break;
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#pragma GCC optimize(3)
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
/*#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
#include<ext/pb_ds/priority_queue.hpp>*/
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
// #include<ext/rope>
#define ll long long
#define ull unsigned long long
#define INFI 2147483647
#define INFL 9223372036854775807
#define INFU 18446744073709551615
using namespace std;
// using namespace __gnu_pbds;
// using namespace __gnu_cxx;
const double PI = acos(-1.0);
const double eps = 1e-6;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
template <typename T> void debug(T x) {
  cout << "value=" << x << endl;
  return;
}

bool prime(ll N) {
  if (N < 2)
    return false;
  if (N == 2)
    return true;

  ll i;
  for (i = 2; i < sqrt(N); i++) {
    if (N % i == 0) // i能被j整除，N不是质数
    {
      return false;
    }
  }

  return true;
}
set<ll> s;
ll fpow(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = (res * a);
    a = (a * a);
    b >>= 1;
  }
  return res;
}
ll factor_num(ll N) {
  ll i = 2;
  ll num = 0;
  ll n = N;
  while (i < N) {
    if (prime(N)) // N为质数，循环退出
    {
      num++;
      break;
    }

    if (N % i != 0 || !prime(i)) // i不是N的因数 || i不是质数
    {
      i++;
      continue;
    }

    if (prime(i)) // i是N的一个质因数
    {
      if (s.find(i) != s.end()) {
        N = N / i;
        break;
      }
      ll cnt = 0;
      while (N % i == 0) {
        cnt++;
        // printf("n=%lld\\n",N);
        N = N / i;
        if (N % i != 0)
          break;
      }
      // printf("cnt=%lld\\n",cnt);
      // printf("n=%lld\\n",N);
      if (cnt == 1 || cnt == 2)
        num += 1;
      else {
        ll res;
        ll l = 1, r = cnt;
        while (l <= r) {
          ll mid = (l + r) >> 1;
          if ((1 + mid) * mid / 2 > cnt)
            r = mid - 1;
          else
            l = mid + 1;
        }
        num += r;
        // printf("res=%lld\\n",res);
      }
      // N=N*fpow(i,cnt-1);
      s.insert(i);
      // cout<<"i="<<i<<"cnt="<<cnt<<endl
      i = 2;
    }
  }

  return num;
}
signed main() {
  // ios_base::sync_with_stdio(false);
  // cin.tie(0);
  // cout.tie(0);
  ll n;
  cin >> n;
  if (n == 1)
    cout << 0 << endl;
  else if (prime(n))
    cout << 1 << endl;
  else
    cout << factor_num(n);
  return 0;
}