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

#include <bits/stdc++.h>
#define ll long long
#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define f(i, a, b) for (ll i = a; i < b; i++)
#define fr(i, a, b) for (ll i = a; i >= b; i--)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define LB lower_bound
#define UB upper_bound
#define endl "\\n"
#define PI 3.141592653589793
using namespace std;

bool sortbysec(const pair<ll, ll> &a, const pair<ll, ll> &b) {
  return (a.second < b.second);
}

ll maxSubArraySum(ll a[], ll size) {
  ll max_so_far = INT_MIN, max_ending_here = 0;

  for (ll i = 0; i < size; i++) {
    max_ending_here = max_ending_here + a[i];
    if (max_so_far < max_ending_here)
      max_so_far = max_ending_here;

    if (max_ending_here < 0)
      max_ending_here = 0;
  }
  return max_so_far;
}

ll findSubarraySum(ll arr[], ll n, ll sum = 0) {
  unordered_map<ll, ll> prevSum;
  ll res = 0;
  ll currsum = 0;
  for (ll i = 0; i < n; i++) {
    currsum += arr[i];
    if (currsum == sum)
      res++;
    if (prevSum.find(currsum - sum) != prevSum.end())
      res += (prevSum[currsum - sum]);
    prevSum[currsum]++;
  }
  return res;
}
ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}

ll powmp(ll x, ll y, ll p) {
  ll res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}

int main() {
  FAST ll n;
  cin >> n;
  map<ll, ll> cn;
  while (n % (2LL) == 0) {
    cn[2]++;
    n /= 2;
  }
  for (ll i = 3; i * i <= n; i += 2) {
    while (n % i == 0) {
      cn[i]++;
      n /= i;
    }
  }
  if (n > 1)
    cn[n] = 1;
  ll ans = 0;
  for (auto k : cn) {
    ll s = k.S;
    ll curr = 1;
    while (s - curr >= 0) {
      ans++;
      s -= curr;
      curr++;
    }
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define f(i, a, b) for (ll i = a; i < b; i++)
#define fr(i, a, b) for (ll i = a; i >= b; i--)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define LB lower_bound
#define UB upper_bound
#define endl "\\n"
#define PI 3.141592653589793
using namespace std;

bool sortbysec(const pair<ll, ll> &a, const pair<ll, ll> &b) {
  return (a.second < b.second);
}

ll maxSubArraySum(ll a[], ll size) {
  ll max_so_far = INT_MIN, max_ending_here = 0;

  for (ll i = 0; i < size; i++) {
    max_ending_here = max_ending_here + a[i];
    if (max_so_far < max_ending_here)
      max_so_far = max_ending_here;

    if (max_ending_here < 0)
      max_ending_here = 0;
  }
  return max_so_far;
}

ll findSubarraySum(ll arr[], ll n, ll sum = 0) {
  unordered_map<ll, ll> prevSum;
  ll res = 0;
  ll currsum = 0;
  for (ll i = 0; i < n; i++) {
    currsum += arr[i];
    if (currsum == sum)
      res++;
    if (prevSum.find(currsum - sum) != prevSum.end())
      res += (prevSum[currsum - sum]);
    prevSum[currsum]++;
  }
  return res;
}
ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}

ll powmp(ll x, ll y, ll p) {
  ll res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}

int main() {
  FAST ll n;
  cin >> n;
  map<ll, ll> cn;
  while (n % 2 == 0) {
    cn[2]++;
    n /= 2;
  }
  for (int i = 3; i * i <= n; i += 2) {
    while (n % i == 0) {
      cn[i]++;
      n /= i;
    }
  }
  if (n > 1)
    cn[n] = 1;
  ll ans = 0;
  for (auto k : cn) {
    ll s = k.S;
    ll curr = 1;
    while (s - curr >= 0) {
      ans++;
      s -= curr;
      curr++;
    }
  }
  cout << ans << endl;
}