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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <utility>
#include <vector>
#define s(n) scanf("%d", &n)
#define sc(n) scanf("%c", &n)
#define sl(n) scanf("%I64d", &n)
#define sf(n) scanf("%lf", &n)
#define ss(n) scanf("%s", n)
#define INF (int)1e9
#define EPS 1e-9
#define bitcount __builtin_popcount
#define gcd __gcd
#define forall(i, a, b) for (int i = a; i < b; i++)
#define foreach(v, c)                                                          \\
  for (typeof((c).begin()) v = (c).begin(); v != (c).end(); ++v)
#define all(a) a.begin(), a.end()
#define in(a, b) ((b).find(a) != (b).end())
#define pb push_back
#define fill(a, v) memset(a, v, sizeof a)
#define sz(a) ((int)(a.size()))
#define mp make_pair
#define MOD 1000000007
typedef long long ll;
/*For you see , each day i love you
 more.Today more than yesterday
  less than tomorrow.*/
using namespace std;

vector<int> has(1000001, 0);
vector<int> primes;

void sieve() {
  has[0] = has[1] = 1;
  for (int i = 2; i * i <= 1000000; i++) {
    if (!has[i])
      for (int j = i * i; j <= 1000000; j += i)
        has[j] = 1;
  }

  for (int i = 0; i <= 1000000; i++) {
    if (!has[i])
      primes.push_back(i);
  }
}

int solve(int count) {
  int start = 1, end = 40;
  while (start <= end) {
    int mid = (start + end) / 2;
    int val = mid * (mid + 1) / 2;

    if (val > count)
      end = mid - 1;
    else
      start = mid + 1;
  }

  return start - 1;
  return 0;
}

int main() {
  sieve();
  ll n;
  cin >> n;
  int ans = 0;

  for (int i = 0; i < primes.size() && primes[i] <= n; i++) {
    int count = 0;
    while (n % primes[i] == 0) {
      n /= primes[i];
      count++;
    }
    ans += solve(count);
  }

  if (n > 1) {
    ans++;
  }

  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <utility>
#include <vector>
#define s(n) scanf("%d", &n)
#define sc(n) scanf("%c", &n)
#define sl(n) scanf("%I64d", &n)
#define sf(n) scanf("%lf", &n)
#define ss(n) scanf("%s", n)
#define INF (int)1e9
#define EPS 1e-9
#define bitcount __builtin_popcount
#define gcd __gcd
#define forall(i, a, b) for (int i = a; i < b; i++)
#define foreach(v, c)                                                          \\
  for (typeof((c).begin()) v = (c).begin(); v != (c).end(); ++v)
#define all(a) a.begin(), a.end()
#define in(a, b) ((b).find(a) != (b).end())
#define pb push_back
#define fill(a, v) memset(a, v, sizeof a)
#define sz(a) ((int)(a.size()))
#define mp make_pair
#define MOD 1000000007
typedef long long ll;
/*For you see , each day i love you
 more.Today more than yesterday
  less than tomorrow.*/
using namespace std;

int solve(int count) {
  int start = 1, end = 40;
  while (start <= end) {
    int mid = (start + end) / 2;
    int val = mid * (mid + 1) / 2;

    if (val > count)
      end = mid - 1;
    else
      start = mid + 1;
  }

  return start - 1;
  return 0;
}

int main() {
  ll n;
  cin >> n;
  int ans = 0;

  for (int i = 2; i * i <= n; i++) {
    int count = 0;
    while (n % i == 0) {
      n /= i;
      count++;
    }
    ans += solve(count);
  }

  if (n > 1) {
    ans++;
  }

  cout << ans << endl;
  return 0;
}