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

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cstdint>   // int64_t, int*_t
#include <cstdio>    // printf
#include <deque>     // deque
#include <iostream>  // cout, endl, cin
#include <map>       // map
#include <math.h>    // sqrt
#include <queue>     // queue, priority_queue
#include <set>       // set
#include <stack>     // stack
#include <string>    // string, to_string, stoi
#include <tuple>     // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
using namespace std;

long long divid(long long N, long long n) {
  long long ans = 0;
  while (true) {
    if (N % n != 0) {
      break;
    }
    ans++;
    N /= n;
  }
  return ans;
}

long long kaijo(long long n) {
  long long ans = 0;
  long long i = 1;
  while (true) {
    if (n < i) {
      break;
    }
    ans++;
    n -= i;
    i++;
  }
  return ans;
}

long long dividn(long long N, long long i, long long n) {
  for (long long j = 0; j < n; j++) {
    N /= i;
  }
  return N;
}

int main() {
  long long N, n;
  cin >> N;
  long long ans = 0;
  long long div, kai;
  long long i = 2;
  n = N;
  while (true) {
    if (sqrt(N) < i && sqrt(N) < n) {
      ans++;
      break;
    }
    if (sqrt(N) < i) {
      break;
    }
    div = divid(n, i);
    if (div == 0) {
      i++;
      continue;
    }
    kai = kaijo(div);
    ans += kai;
    n = dividn(n, i, div);
    i++;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cstdint>   // int64_t, int*_t
#include <cstdio>    // printf
#include <deque>     // deque
#include <iostream>  // cout, endl, cin
#include <map>       // map
#include <math.h>    // sqrt
#include <queue>     // queue, priority_queue
#include <set>       // set
#include <stack>     // stack
#include <string>    // string, to_string, stoi
#include <tuple>     // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
using namespace std;

long long divid(long long N, long long n) {
  long long ans = 0;
  while (true) {
    if (N % n != 0) {
      break;
    }
    ans++;
    N /= n;
  }
  return ans;
}

long long kaijo(long long n) {
  long long ans = 0;
  long long i = 1;
  while (true) {
    if (n < i) {
      break;
    }
    ans++;
    n -= i;
    i++;
  }
  return ans;
}

long long dividn(long long N, long long i, long long n) {
  for (long long j = 0; j < n; j++) {
    N /= i;
  }
  return N;
}

int main() {
  long long N;
  cin >> N;
  long long ans = 0;
  long long div, kai;
  long long i = 2;
  while (true) {
    if (N < i) {
      break;
    }
    if (sqrt(N) < i && ans == 0) {
      ans++;
      break;
    }
    div = divid(N, i);
    if (div == 0) {
      i++;
      continue;
    }
    kai = kaijo(div);
    ans += kai;
    N = dividn(N, i, div);
    i++;
  }
  cout << ans << endl;
}