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

#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = s; i < (int)(n); i++)
#define Clear(a) a = decltype(a)();
typedef long long ll;
typedef vector<vector<ll>> vvl;
typedef vector<ll> vl;
typedef vector<vector<char>> vvc;
typedef vector<char> vc;
typedef pair<ll, ll> pll;
typedef priority_queue<string> priquestring;
typedef priority_queue<int> priqueint;
typedef priority_queue<ll> priquell;
typedef priority_queue<char> priquechar;
typedef priority_queue<string, vector<string>, greater<string>> rpriquestring;
typedef priority_queue<int, vector<int>, greater<int>> rpriqueint;
typedef priority_queue<ll, vector<ll>, greater<ll>> rpriquell;
typedef priority_queue<char, vector<char>, greater<char>> rpriquechar;
ll max(ll x, ll y) {
  if (x > y)
    return x;
  else
    return y;
}
ll min(ll x, ll y) { return x + y - max(x, y); }

const ll big = 1000000007;
int exp(int x, int y) {
  if (y == 0)
    return 1; // 0^0=1
  if (x == 1 || x == 0)
    return x;
  if (y % 2 == 1)
    return (exp(x, y - 1) * x) % big;
  int t = exp(x, y / 2);
  return (t * t) % big;
}
void chmin(ll a, ll b) {
  if (a > b)
    a = b;
  return;
}

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

void next(vector<int> &use) {
  bool ans = true;
  int cnt = 0;
  while (ans) {
    use[cnt]++;
    if (use[cnt] <= 2)
      ans = false;
    else
      use[cnt] = 0;
    cnt++;
  }
}

int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  cout << fixed << setprecision(10);
  /*--------------------------------*/

  vector<bool> pri(1000001, true);
  rep2(i, 2, 1000001) {
    if (!pri[i])
      continue;
    for (int j = 2 * i; j <= 1000001; j += i)
      pri[j] = false;
  }
  queue<int> pr;
  rep2(i, 2, 1000001) {
    if (pri[i])
      pr.push(i);
  }

  ll n;
  cin >> n;
  if (n == 1) {
    cout << 0 << endl;
    return 0;
  }
  ll ans = 0;
  int p;
  while (!pr.empty()) {
    p = pr.front();
    pr.pop();
    if (p * p > n)
      break;
    ll j = p;
    while (n % j == 0) {
      n /= j;
      j *= p;
      ans++;
    }
    while (n % p == 0)
      n /= p;
  }
  if (n != 1)
    ans++;
  cout << ans << '\\n';
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = s; i < (int)(n); i++)
#define Clear(a) a = decltype(a)();
typedef long long ll;
typedef vector<vector<ll>> vvl;
typedef vector<ll> vl;
typedef vector<vector<char>> vvc;
typedef vector<char> vc;
typedef pair<ll, ll> pll;
typedef priority_queue<string> priquestring;
typedef priority_queue<int> priqueint;
typedef priority_queue<ll> priquell;
typedef priority_queue<char> priquechar;
typedef priority_queue<string, vector<string>, greater<string>> rpriquestring;
typedef priority_queue<int, vector<int>, greater<int>> rpriqueint;
typedef priority_queue<ll, vector<ll>, greater<ll>> rpriquell;
typedef priority_queue<char, vector<char>, greater<char>> rpriquechar;
ll max(ll x, ll y) {
  if (x > y)
    return x;
  else
    return y;
}
ll min(ll x, ll y) { return x + y - max(x, y); }

const ll big = 1000000007;
int exp(int x, int y) {
  if (y == 0)
    return 1; // 0^0=1
  if (x == 1 || x == 0)
    return x;
  if (y % 2 == 1)
    return (exp(x, y - 1) * x) % big;
  int t = exp(x, y / 2);
  return (t * t) % big;
}
void chmin(ll a, ll b) {
  if (a > b)
    a = b;
  return;
}

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

void next(vector<int> &use) {
  bool ans = true;
  int cnt = 0;
  while (ans) {
    use[cnt]++;
    if (use[cnt] <= 2)
      ans = false;
    else
      use[cnt] = 0;
    cnt++;
  }
}

int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  cout << fixed << setprecision(10);
  /*--------------------------------*/

  ll n;
  cin >> n;
  if (n == 1) {
    cout << 0 << endl;
    return 0;
  }
  ll ans = 0;
  for (int i = 2; i * i <= n; i++) {
    if (n % i != 0)
      continue;
    int j = i;
    while (n % j == 0) {
      n /= j;
      j *= i;
      ans++;
    }
    while (n % i == 0)
      n /= i;
    if (n == 1)
      break;
    if (i != 2)
      i++;
  }
  if (n != 1)
    ans++;
  if (ans == 0)
    ans++;
  cout << ans << endl;
}