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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>

#include <bits/stdc++.h>
#include <cmath>
#include <limits>

using namespace std;
typedef long long LL;

LL isPrime(LL p) {
  if (p <= 1)
    return 0;
  if (p == 2 || p == 3 || p == 5 || p == 7)
    return 1;
  if (p == 4 || p == 6 || p == 8 || p == 9)
    return 0;

  LL i;
  int ret = 1;
  for (i = 2; i * i <= p; i++) {
    if (p % i == 0 && i != p) {
      ret = 0;
      break;
    }
  }
  return ret;
}

map<long long, int> mp;

int ecount(int e) {
  if (e == 1)
    return 1;
  else if (e == 2)
    return 1;
  else {
    int ret = 1;
    while (true) {
      if (ret * (ret + 1) > (2 * e))
        break;
      ret++;
    }
    ret--;
    return ret;
  }
}

int main(int argc, char *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  long long N;
  cin >> N;

  if (N == 1) {
    cout << "0" << endl;
    return 0;
  }
  if (isPrime(N)) {
    cout << "1" << endl;
    return 0;
  }

  long long i = 2;
  int sqN = (int)sqrt(N) + 1;
  long long n = N;
  for (i = 2; n != 1 && i < (N / 2) + 1; i++) {
    if ((n % i) == 0) {
      if (isPrime(i)) {
        int e = 0;
        long long u = n;
        while ((u % i) == 0) {
          e++;
          u = u / i;
          n = u;
        }
        mp[i] = e;
        if (isPrime(n)) {
          mp[n] = 1;
          break;
        }
      }
    }
  }

  if (mp.size() == 0) {
    mp[N] = 1;
    cout << "1" << endl;
    return 0;
  }

  long long ans = 0;
  for (auto itr = mp.begin(); itr != mp.end(); itr++) {
    ans += ecount(itr->second);
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>

#include <bits/stdc++.h>
#include <cmath>
#include <limits>

using namespace std;
typedef long long LL;

LL isPrime(LL p) {
  if (p <= 1)
    return 0;
  if (p == 2 || p == 3 || p == 5 || p == 7)
    return 1;
  if (p == 4 || p == 6 || p == 8 || p == 9)
    return 0;

  LL i;
  int ret = 1;
  for (i = 2; i * i <= p; i++) {
    if (p % i == 0 && i != p) {
      ret = 0;
      break;
    }
  }
  return ret;
}

map<long long, int> mp;

int ecount(int e) {
  if (e == 1)
    return 1;
  else if (e == 2)
    return 1;
  else {
    int ret = 1;
    while (true) {
      if (ret * (ret + 1) > (2 * e))
        break;
      ret++;
    }
    ret--;
    return ret;
  }
}

int main(int argc, char *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  long long N;
  cin >> N;

  if (N == 1) {
    cout << "0" << endl;
    return 0;
  }
  if (isPrime(N)) {
    cout << "1" << endl;
    return 0;
  }

  long long i = 2;
  int sqN = (int)sqrt(N) + 1;
  long long n = N;
  for (i = 2; n != 1 && i < (N / 2) + 1; i++) {
    if (isPrime(i)) {
      if ((n % i) == 0) {
        int e = 0;
        long long u = n;
        while ((u % i) == 0) {
          e++;
          u = u / i;
          n = u;
        }
        mp[i] = e;
      }
    }
  }

  if (mp.size() == 0) {
    mp[N] = 1;
    cout << "1" << endl;
    return 0;
  }

  long long ans = 0;
  for (auto itr = mp.begin(); itr != mp.end(); itr++) {
    ans += ecount(itr->second);
  }
  cout << ans << endl;
  return 0;
}