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;
// vectorのリサイズ
// A.resize(N);

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); }

// firstに素数, secondに何乗かが入る
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; // n の最下位bitが 1 ならば x^(2^i) をかける
    x *= x;
    n >>= 1; // n を1bit 左にずらす
  }
  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:

#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;
// vectorのリサイズ
// A.resize(N);

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); }

// O(NloglogN)
std::vector<int> Eratosthenes(const ll N) {
  std::vector<bool> is_prime(N + 1);
  for (int i = 0; i <= N; i++) {
    is_prime[i] = true;
  }
  std::vector<int> P;
  for (int i = 2; i <= N; i++) {
    if (is_prime[i]) {
      for (int j = 2 * i; j <= N; j += i) {
        is_prime[j] = false;
      }
      P.emplace_back(i);
    }
  }
  return P;
}

// O(sqrt(x))
bool is_prime(ll x) {
  if (x <= 1) {
    return false;
  }
  for (int i = 2; i * i < x; i++) {
    if (x % i == 0) {
      return false;
    }
  }
  return true;
}

// firstに素数, secondに何乗かが入る
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;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll n;
  cin >> n;
  // if (is_prime(n)) {
  //   cout << 1 << endl;
  //   return 0;
  // }
  vector<P> f = factorize(n);
  map<ll, ll> mp;
  bool flg = false;
  while (n != 1 && !mp.count(n)) {
    rep(i, f.size()) {
      if (flg) {
        flg = false;
        break;
      }
      for (int j = 1; j <= f[i].second; j++) {
        ll z = pow(f[i].first, j);
        if (n % z)
          continue;
        if (!mp.count(z)) {
          mp[z]++;
          // cout << "z: " << z << endl;
          n /= z;
          // cout << "n: " << n << endl;
          flg = true;
        }
        if (z > n)
          break;
      }
    }
  }
  ll ans;
  for (auto m : mp) {
    // cout << m.first << endl;
    ++ans;
  }
  cout << ans << endl;
  return 0;
}