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

#include <bits/stdc++.h>
using namespace std;

constexpr int64_t INF = 1'010'000'000'000'000'017LL;
constexpr int64_t MOD = 1'000'000'007LL;
constexpr double EPS = 1e-12;
constexpr double PI = 3.14159265358979323846;

#define FOR(i, start, end) for (uint64_t i = start; i < end; i++)
#define REP(i, n) FOR(i, 0, n)

// 最大公約数gcd
// 最小公倍数lcm=m*n/gcd
uint64_t gcd(uint64_t m, uint64_t n) {
  uint64_t temp;
  while (m % n != 0) {
    temp = n;
    n = m % n;
    m = temp;
  }
  return n;
}

uint64_t lcm(uint64_t m, uint64_t n) { return (m * n) / gcd(m, n); }

void comb(vector<vector<uint64_t>> &v) {
  for (uint64_t i = 0; i < v.size(); i++) {
    v[i][0] = 1;
    v[i][i] = 1;
  }
  for (uint64_t k = 1; k < v.size(); k++) {
    for (uint64_t j = 1; j < k; j++) {
      v[k][j] = (v[k - 1][j - 1] + v[k - 1][j]);
    }
  }
}

bool is_product_overflow(uint64_t a, uint64_t b) {

  uint64_t prod = a * b;

  return (prod / b != a);
}

void findFactors(uint64_t number, list<uint64_t> &factors) {
  uint64_t val = sqrt(number);
  for (uint64_t i = 2; i <= val; i++) { // 他の素数で割っていく
    while (number % i == 0) {
      number /= i;
      factors.push_back(i);
    }
  }
  if (number != 1) {
    factors.push_back(number);
  }
}

signed main() {
  uint64_t n;
  cin >> n;
  list<uint64_t> factors;
  findFactors(n, factors);

  uint64_t totalCount = 0;
  while (factors.size() > 0) {
    bool isFirst = true;
    uint64_t val = 0;
    uint64_t factCount = 0;
    for (auto factor : factors) {
      if (isFirst) {
        val = factor;
        isFirst = false;
        factCount++;
      } else if (val == factor) {
        factCount++;
      }
    }
    for (int j = 0; j < factCount; j++) {
      factors.pop_front();
    }
    uint64_t currCount = 1;
    while (factCount >= currCount) {
      for (int j = 0; j < currCount; j++) {
        n /= val;
        factCount--;
      }
      totalCount++;
      currCount++;
    }
  }
  cout << totalCount << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

constexpr int64_t INF = 1'010'000'000'000'000'017LL;
constexpr int64_t MOD = 1'000'000'007LL;
constexpr double EPS = 1e-12;
constexpr double PI = 3.14159265358979323846;

#define FOR(i, start, end) for (uint64_t i = start; i < end; i++)
#define REP(i, n) FOR(i, 0, n)

// 最大公約数gcd
// 最小公倍数lcm=m*n/gcd
uint64_t gcd(uint64_t m, uint64_t n) {
  uint64_t temp;
  while (m % n != 0) {
    temp = n;
    n = m % n;
    m = temp;
  }
  return n;
}

uint64_t lcm(uint64_t m, uint64_t n) { return (m * n) / gcd(m, n); }

void comb(vector<vector<uint64_t>> &v) {
  for (uint64_t i = 0; i < v.size(); i++) {
    v[i][0] = 1;
    v[i][i] = 1;
  }
  for (uint64_t k = 1; k < v.size(); k++) {
    for (uint64_t j = 1; j < k; j++) {
      v[k][j] = (v[k - 1][j - 1] + v[k - 1][j]);
    }
  }
}

bool is_product_overflow(uint64_t a, uint64_t b) {

  uint64_t prod = a * b;

  return (prod / b != a);
}

void findFactors(uint64_t number, list<uint64_t> &factors) {

  for (uint64_t i = 2; i <= number; i++) { // 他の素数で割っていく
    while (number % i == 0) {
      number /= i;
      factors.push_back(i);
    }
  }
}
signed main() {
  uint64_t n;
  cin >> n;
  list<uint64_t> factors;
  findFactors(n, factors);

  uint64_t totalCount = 0;
  while (factors.size() > 0) {
    bool isFirst = true;
    uint64_t val = 0;
    uint64_t factCount = 0;
    for (auto factor : factors) {
      if (isFirst) {
        val = factor;
        isFirst = false;
        factCount++;
      } else if (val == factor) {
        factCount++;
      }
    }
    for (int j = 0; j < factCount; j++) {
      factors.pop_front();
    }
    uint64_t currCount = 1;
    while (factCount >= currCount) {
      for (int j = 0; j < currCount; j++) {
        n /= val;
        factCount--;
      }
      totalCount++;
      currCount++;
    }
  }
  cout << totalCount << endl;
  return 0;
}