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

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

#define ll long long
#define fw(p) for (int w = 0; w < (p); w++)
#define fx(p) for (int x = 0; x < (p); x++)
#define fy(p) for (int y = 0; y < (p); y++)
#define fz(p) for (int z = 0; z < (p); z++)
#define fyg(p, g) for (int y = (g); y < (p); y++)
#define fzg(p, g) for (int z = (g); z < (p); z++)
#define ce(d) cout << d << endl;
#define vecp(p)                                                                \\
  int aa;                                                                      \\
  cin >> aa;                                                                   \\
  (p).push_back(aa);
#define vecpl(p)                                                               \\
  long long aa;                                                                \\
  cin >> aa;                                                                   \\
  (p).push_back(aa);
#define vecps(p)                                                               \\
  string aa;                                                                   \\
  cin >> aa;                                                                   \\
  (p).push_back(aa);
#define vecp2(p)                                                               \\
  cin >> aa;                                                                   \\
  (p).push_back(aa);
#define vecpl2(p)                                                              \\
  long long a b;                                                               \\
  cin >> ab;                                                                   \\
  (p).push_back(ab);
#define vecps2(p)                                                              \\
  string ab;                                                                   \\
  cin >> ab;                                                                   \\
  (p).push_back(ab);
#define sorts(c) sort((c).begin(), (c).end());
#define reverses(c) reverse((c).begin(), (c).end());
#define vec(b) vector<int>(b);
#define vecl(b) vector<long long>(b);
#define vecs(b) vector<string>(b);
#define pb(b, a) (b).push_back((a));
#define doublece(a, b) cout << (a) << ' ' << (b) << endl;
#define pairs(s) vector<pair<int, int>>(s);
#define pairsp(s)                                                              \\
  int aa, bb;                                                                  \\
  cin >> aa >> bb;                                                             \\
  (s).push_back(make_pair(aa, bb));
#define MOD 1000000007
#define cey ce("Yes")
#define cen ce("No")
#define ceY ce("YES")
#define ceN ce("NO")

int main() {
  ll N;
  cin >> N;

  map<int, int> K;
  set<int> G;
  ll j = 2;
  ll n = N;

  while (n != 1) {
    if (n % j == 0) {
      K[j]++;
      n /= j;
      G.insert(j);
    } else {
      j++;
    }

    if (j * j > N) {
      break;
    }
  }

  if (G.size() == 0) {
    if (N == 1) {
      ce(0)
    } else {
      ce(1)
    }
    return 0;
  }

  j = 2;
  ll k = n;
  while (k != 1) {
    if (k % j == 0) {
      K[j]++;
      k /= j;
      G.insert(j);
    } else {
      j++;
    }

    if (j * j > n) {
      break;
    }
  }
  if (k != 1) {
    K[k]++;
    G.insert(k);
  }

  ll ans = 0;
  for (auto g : G) {

    int i = 1;
    int k = K[g];
    while (i <= k) {
      ans++;
      k -= i;
      i++;
    }
  }
  ce(ans)

      return 0;
}


Error: TLE

Fixed submission:

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

#define ll long long
#define fw(p) for (int w = 0; w < (p); w++)
#define fx(p) for (int x = 0; x < (p); x++)
#define fy(p) for (int y = 0; y < (p); y++)
#define fz(p) for (int z = 0; z < (p); z++)
#define fyg(p, g) for (int y = (g); y < (p); y++)
#define fzg(p, g) for (int z = (g); z < (p); z++)
#define ce(d) cout << d << endl;
#define vecp(p)                                                                \\
  int aa;                                                                      \\
  cin >> aa;                                                                   \\
  (p).push_back(aa);
#define vecpl(p)                                                               \\
  long long aa;                                                                \\
  cin >> aa;                                                                   \\
  (p).push_back(aa);
#define vecps(p)                                                               \\
  string aa;                                                                   \\
  cin >> aa;                                                                   \\
  (p).push_back(aa);
#define vecp2(p)                                                               \\
  cin >> aa;                                                                   \\
  (p).push_back(aa);
#define vecpl2(p)                                                              \\
  long long a b;                                                               \\
  cin >> ab;                                                                   \\
  (p).push_back(ab);
#define vecps2(p)                                                              \\
  string ab;                                                                   \\
  cin >> ab;                                                                   \\
  (p).push_back(ab);
#define sorts(c) sort((c).begin(), (c).end());
#define reverses(c) reverse((c).begin(), (c).end());
#define vec(b) vector<int>(b);
#define vecl(b) vector<long long>(b);
#define vecs(b) vector<string>(b);
#define pb(b, a) (b).push_back((a));
#define doublece(a, b) cout << (a) << ' ' << (b) << endl;
#define pairs(s) vector<pair<int, int>>(s);
#define pairsp(s)                                                              \\
  int aa, bb;                                                                  \\
  cin >> aa >> bb;                                                             \\
  (s).push_back(make_pair(aa, bb));
#define MOD 1000000007
#define cey ce("Yes")
#define cen ce("No")
#define ceY ce("YES")
#define ceN ce("NO")

int main() {
  ll N;
  cin >> N;

  map<int, int> K;
  set<int> G;
  ll i = 2;
  ll n = N;
  bool flag = true;
  while (n != 1) {
    while (n % i == 0) {
      K[i]++;
      n /= i;
      G.insert(i);
      flag = false;
    }

    if (i * i > N && flag) {
      break;
    }
    i++;
  }
  if (K.size() == 0) {
    if (N == 1) {
      ce(0)
    } else {
      ce(1)
    }
    return 0;
  }
  ll ans = 0;
  for (auto g : G) {
    int i = 1;
    ll k = K[g];
    int b = (-1 + sqrt(1 + 8 * k)) / 2;
    ans += b;
  }
  ce(ans)

      return 0;
}