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

#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define con_input(v)                                                           \\
  for (auto it = v.begin(); it != v.end(); it++) {                             \\
    cin >> *it;                                                                \\
  }
#define con_debug(v)                                                           \\
  for (auto it = v.begin(); it != v.end(); it++) {                             \\
    cerr << *it << " ";                                                        \\
  }                                                                            \\
  cerr << endl;
using namespace std;
template <typename T> T digitsum(T n);
template <typename T> T gcd(T u, T v);
template <typename T> bool isPrime(T n);
template <typename T> vector<pair<T, T>> prime_factor(T n);

int main() {
  long long int n, ans = 0, a;
  cin >> n;
  vector<pair<long long int, long long int>> sosu = prime_factor(n);
  rep(i, sosu.size()) {
    a = 1;
    while (sosu[i].second >= a) {
      ans++;
      sosu[i].second -= a;
      a++;
    }
  }
  cout << ans << endl;
}

template <typename T> T digitsum(T n) {
  string s = to_string(n);
  T sum = 0;
  T d = 1;
  for (T e = 0; e < s.length(); e++) {
    sum += (n / d) % 10;
    d *= 10;
  }
  return sum;
}
template <typename T> T gcd(T u, T v) {
  while (v != 0) {
    T r = u % v;
    u = v;
    v = r;
  }
  return u;
}
template <typename T> bool isPrime(T n) {
  if (n <= 1)
    return false;
  if (n == 2)
    return true;
  if (n % 2 == 0)
    return false;
  for (T q = 3; q * q <= n; q += 2) {
    if (n % q == 0)
      return false;
  }
  return true;
}
template <typename T> vector<pair<T, T>> prime_factor(T n) {
  vector<pair<T, T>> ret;
  if (n % 2 == 0) {
    T tmp = 0;
    while (n % 2 == 0) {
      tmp++;
      n /= 2;
    }
    ret.push_back(make_pair(2, tmp));
  }
  for (T i = 3; i * i <= n; i += 2) {
    if (n % i == 0) {
      T tmp = 0;
      while (n % i == 0) {
        tmp++;
        n /= i;
      }
      ret.push_back(make_pair(i, tmp));
    }
  }
  if (n != 1)
    ret.push_back(make_pair(n, 1));
  return ret;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int i, j, k, count1 = 0, count2 = 0;
bool IsPrime(long long int num) {
  if (num < 2)
    return false;
  else if (num == 2)
    return true;
  else if (num % 2 == 0)
    return false; // 偶数はあらかじめ除く

  double sqrtNum = sqrt(num);
  for (long long int i = 3; i <= sqrtNum; i += 2) {
    if (num % i == 0) {
      // 素数ではない
      return false;
    }
  }

  // 素数である
  return true;
}

int main(void) {
  int ans = 0;
  long long int n, a;
  cin >> n;
  if (n == 1) {
    cout << 0 << endl;
    return 0;
  }
  if (IsPrime(n)) {
    cout << 1 << endl;
    return 0;
  }
  list<int> list;
  if (n % 2 == 0) {
    count1 = 0;
    while (n % 2 == 0) {
      n /= 2;
      count1++;
    }
    a = 1;
    while (a <= count1) {
      ans++;
      count1 -= a;
      a++;
    }
  }
  if (n > 100000000) {
    for (long long int i = 3; i <= n; i += 2) {
      if (n % i == 0) {
        count1 = 0;
        while (n % i == 0) {
          n /= i;
          count1++;
        }
        a = 1;
        while (a <= count1) {
          ans++;
          count1 -= a;
          a++;
        }
      }
    }
  } else {
    for (long long int i = 3; i <= n; i += 2) {
      if (n % i == 0) {
        count1 = 0;
        while (n % i == 0) {
          n /= i;
          count1++;
        }
        a = 1;
        while (a <= count1) {
          ans++;
          count1 -= a;
          a++;
        }
      }
    }
  }
  cout << ans << endl;
}