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


#include <algorithm>
#include <iostream>
#include <numeric>
#include <utility>
#include <vector>

template <class T, class U> inline bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

template <class T, class U> inline bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

struct range {
  struct itr {
    int64_t i;
    constexpr itr(int64_t i_) : i(i_) {}
    constexpr void operator++() { ++i; }
    constexpr int64_t operator*() const { return i; }
    constexpr bool operator!=(itr x) const { return i != x.i; }
  };
  const itr l, r;
  constexpr range(int64_t l_, int64_t r_) : l(l_), r(std::max(l_, r_)) {}
  constexpr itr begin() const { return l; }
  constexpr itr end() const { return r; }
};

struct revrange {
  struct itr {
    int64_t i;
    constexpr itr(int64_t i_) : i(i_) {}
    constexpr void operator++() { --i; }
    constexpr int64_t operator*() const { return i; }
    constexpr bool operator!=(itr x) const { return i != x.i; }
  };
  const itr l, r;
  constexpr revrange(int64_t l_, int64_t r_)
      : l(l_ - 1), r(std::max(l_, r_) - 1) {}
  constexpr itr begin() const { return r; }
  constexpr itr end() const { return l; }
};

using i32 = int32_t;
using i64 = int64_t;
using u32 = uint32_t;
using u64 = uint64_t;

constexpr i32 inf32 = (i32(1) << 30) - 1;
constexpr i64 inf64 = (i64(1) << 62) - 1;

int main() {
  u64 N;
  std::cin >> N;
  u32 ans = 0;
  for (u64 x = 2; x * x <= N; ++x) {
    if (N % x == 0) {
      u32 cnt = 0;
      while (N % x == 0) {
        N /= x;
        ++cnt;
      }
      u32 cur = 1;
      while (true) {
        if (cnt < cur) {
          break;
        }
        cnt -= cur;
        ++ans;
        ++cur;
      }
    }
  }
  if (N > 1) {
    ++ans;
  }
  std::cout << ans << '\\n';
  return 0;
}


Error: TLE

Fixed submission:


#include <algorithm>
#include <iostream>
#include <numeric>
#include <utility>
#include <vector>

template <class T, class U> inline bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

template <class T, class U> inline bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

struct range {
  struct itr {
    int64_t i;
    constexpr itr(int64_t i_) : i(i_) {}
    constexpr void operator++() { ++i; }
    constexpr int64_t operator*() const { return i; }
    constexpr bool operator!=(itr x) const { return i != x.i; }
  };
  const itr l, r;
  constexpr range(int64_t l_, int64_t r_) : l(l_), r(std::max(l_, r_)) {}
  constexpr itr begin() const { return l; }
  constexpr itr end() const { return r; }
};

struct revrange {
  struct itr {
    int64_t i;
    constexpr itr(int64_t i_) : i(i_) {}
    constexpr void operator++() { --i; }
    constexpr int64_t operator*() const { return i; }
    constexpr bool operator!=(itr x) const { return i != x.i; }
  };
  const itr l, r;
  constexpr revrange(int64_t l_, int64_t r_)
      : l(l_ - 1), r(std::max(l_, r_) - 1) {}
  constexpr itr begin() const { return r; }
  constexpr itr end() const { return l; }
};

using i32 = int32_t;
using i64 = int64_t;
using u32 = uint32_t;
using u64 = uint64_t;

constexpr i32 inf32 = (i32(1) << 30) - 1;
constexpr i64 inf64 = (i64(1) << 62) - 1;

int main() {
  u64 N;
  std::cin >> N;
  u32 ans = 0;
  for (u32 x = 2; x * x <= N; ++x) {
    if (N % x == 0) {
      u32 cnt = 0;
      while (N % x == 0) {
        N /= x;
        ++cnt;
      }
      u32 cur = 1;
      while (true) {
        if (cnt < cur) {
          break;
        }
        cnt -= cur;
        ++ans;
        ++cur;
      }
    }
  }
  if (N > 1) {
    ++ans;
  }
  std::cout << ans << '\\n';
  return 0;
}