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

/*
 * a.cpp
 *
 *  Created on: 2019/06/08
 *      Author: MEIP-users
 */

#include <algorithm>
#include <bits/stdc++.h>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

#define MOD (long long int)(1e9 + 7)
#define ll long long int
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, n) for (int i = 1; i <= (int)(n); i++)
#define REP(i, n) for (int i = n - 1; i >= 0; i--)
#define REPS(i, n) for (int i = n; i > 0; i--)
#define INF (int)(1e9 + 10)
#define LINF (long long int)(112345678901234567)
#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))
#define chmin(a, b) a = (((a) > (b)) ? (b) : (a))

int main() {
  ll n;
  cin >> n;
  if (n == 1) {
    cout << 0 << endl;
    return 0;
  }
  ll nn = n;
  ll i = 2;
  vector<pair<ll, int>> ps = {{1, 1}};
  while (i <= sqrt(n)) {
    if (n % i == 0) {
      if (ps[ps.size() - 1].first == i) {
        ps[ps.size() - 1].second++;
      } else {
        pair<ll, int> p = {i, 1};
        ps.push_back(p);
      }
      n = n / i;
    } else {
      i++;
    }
    if (ps.size() == 1 && sqrt(nn) < i) {
      break;
    }
  }

  ps.erase(ps.begin());
  if (ps[ps.size() - 1].first == n) {
    ps[ps.size() - 1].second++;
  } else {
    ps.push_back({n, 1});
  }
  int ans = 0;
  rep(i, ps.size()) {
    int cnt = 0;
    int a = ps[i].second;
    // cout << ps[i].first << " " << ps[i].second<<endl;
    while (a > 0) {

      if (a > 0) {
        cnt++;
        a -= 1;
      }
      a -= cnt;
    }
    ans += cnt;
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

/*
 * a.cpp
 *
 *  Created on: 2019/06/08
 *      Author: MEIP-users
 */

#include <algorithm>
#include <bits/stdc++.h>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

#define MOD (long long int)(1e9 + 7)
#define ll long long int
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, n) for (int i = 1; i <= (int)(n); i++)
#define REP(i, n) for (int i = n - 1; i >= 0; i--)
#define REPS(i, n) for (int i = n; i > 0; i--)
#define INF (int)(1e9 + 10)
#define LINF (long long int)(112345678901234567)
#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))
#define chmin(a, b) a = (((a) > (b)) ? (b) : (a))

int main() {
  ll n;
  cin >> n;
  if (n == 1) {
    cout << 0 << endl;
    return 0;
  }
  ll nn = n;
  ll i = 2;
  vector<pair<ll, int>> ps = {{1, 1}};
  while (i <= n) {
    if (n % i == 0) {
      if (ps[ps.size() - 1].first == i) {
        ps[ps.size() - 1].second++;
      } else {
        pair<ll, int> p = {i, 1};
        ps.push_back(p);
      }
      n = n / i;
    } else {
      i++;
    }
    if (ps.size() == 1 && sqrt(nn) < i) {
      break;
    }
  }
  if (ps.size() == 1) {
    ps = {{n, 1}};
  } else {
    ps.erase(ps.begin());
  }
  int ans = 0;
  rep(i, ps.size()) {
    int cnt = 0;
    int a = ps[i].second;
    // cout << ps[i].first << " " << ps[i].second<<endl;
    while (a > 0) {

      if (a > 0) {
        cnt++;
        a -= 1;
      }
      a -= cnt;
    }
    ans += cnt;
  }
  cout << ans << endl;
  return 0;
}