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

#include <bits/stdc++.h>
#define REP(i, m, n) for (int(i) = (m); (i) < (n); ++i)
#define rep(i, n) REP(i, 0, n)
#define all(x) (x).begin(), (x).end()
using namespace std;
using Graph = vector<vector<int>>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
const int MOD = 1e9 + 7;
const int INF = 1 << 30;
const ll LINF = 1LL << 60;

bool isPrime(ll n) {
  if (n < 2)
    return false;
  else if (n == 2)
    return true;
  else if (n % 2 == 0)
    return false;

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

  return true;
}

vector<pair<ll, ll>> prime_factorize(ll n) {
  vector<pair<ll, ll>> res;
  for (ll p = 2; p * p <= n; ++p) {
    if (n % p != 0)
      continue;
    ll num = 0;
    while (n % p == 0) {
      ++num;
      n /= p;
    }
    res.push_back(make_pair(p, num));
  }
  if (n != 1)
    res.push_back(make_pair(n, 1));
  return res;
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  ll n;
  cin >> n;

  if (n == 1) {
    cout << 0 << endl;
    return 0;
  }

  if (isPrime(n)) {
    cout << 1 << endl;
    return 0;
  }

  ll res = 0;
  vector<pair<ll, ll>> p = prime_factorize(n);

  vector<ll> v;
  for (auto np : p) {
    ll x = np.first;
    ll y = np.second;
    ll i = 1;
    while (y > 0) {
      v.push_back(pow(x, i));
      y -= i;
      i++;
      if (y < i)
        break;
    }
  }

  sort(v.begin(), v.end());

  rep(i, v.size()) {
    if (n < v[i])
      break;
    n /= v[i];
    res++;
  }

  cout << res << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, m, n) for (int(i) = (m); (i) < (n); ++i)
#define rep(i, n) REP(i, 0, n)
#define all(x) (x).begin(), (x).end()
using namespace std;
using Graph = vector<vector<int>>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
const int MOD = 1e9 + 7;
const int INF = 1 << 30;
const ll LINF = 1LL << 60;

bool isPrime(ll n) {
  if (n < 2)
    return false;
  else if (n == 2)
    return true;
  else if (n % 2 == 0)
    return false;

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

  return true;
}

vector<pair<ll, ll>> prime_factorize(ll n) {
  vector<pair<ll, ll>> res;
  for (ll p = 2; p * p <= n; ++p) {
    if (n % p != 0)
      continue;
    ll num = 0;
    while (n % p == 0) {
      ++num;
      n /= p;
    }
    res.push_back(make_pair(p, num));
  }
  if (n != 1)
    res.push_back(make_pair(n, 1));
  return res;
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  ll n;
  cin >> n;

  if (n == 1) {
    cout << 0 << endl;
    return 0;
  }

  if (isPrime(n)) {
    cout << 1 << endl;
    return 0;
  }

  ll res = 0;
  for (ll i = 2; i <= n; i++) {
    if (n % i != 0)
      continue;
    vector<pair<ll, ll>> p = prime_factorize(i);
    ll t = p.size();
    if (t != 1)
      continue;
    res++;
    n /= i;
    if (n == 1)
      break;
    // cout << i << " " << s << endl;
  }

  cout << res << endl;

  return 0;
}