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

#include <bits/stdc++.h>
#define int long long
#define L_T_S_K                                                                \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
using namespace std;

const int N = 1e6 + 7;

vector<int> spf(N);

bool isprime[N];

void seive() {
  iota(spf.begin(), spf.end(), 0);
  for (int i = 2; i < N; i += 2) {
    spf[i] = 2;
    isprime[i] = 0;
  }
  isprime[2] = 1;
  for (int i = 3; i * i < N; i += 2) {
    if (spf[i] == i) {
      isprime[i] = 1;
      for (int j = i * i; j < N; j += i) {
        isprime[j] = 0;
        if (spf[j] == j)
          spf[j] = i;
      }
    }
  }
}

int check(int n) {
  for (int i = 2; i <= sqrt(n); i++) {
    if (n % i == 0) {
      return 0;
    }
  }
  return 1;
}

int32_t main() {
  L_T_S_K;
  int n;
  cin >> n;

  if (n == 1) {
    cout << 0 << "\\n";
    return 0;
  }

  int ans = 0;
  int f = 0;
  for (int i = 2; i <= sqrt(n); i++) {
    if (n % i == 0) {
      f = 1;
      break;
    }
  }

  if (!f) {
    cout << 1 << "\\n";
    return 0;
  }

  for (int i = 2; i < 1000007; i++) {
    if (n == 1)
      break;
    if (n % i == 0) {
      int c = 0;
      while (n % i == 0) {
        n /= i;
        c++;
      }
      int u = 1;
      while (c - u >= 0) {
        c -= u;
        ans++;
        ++u;
      }
      if (n == 1)
        break;
    }
  }

  if (n > 1)
    ans++;
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long
#define L_T_S_K                                                                \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
using namespace std;

const int N = 1e6 + 7;

vector<int> spf(N);

bool isprime[N];

void seive() {
  iota(spf.begin(), spf.end(), 0);
  for (int i = 2; i < N; i += 2) {
    spf[i] = 2;
    isprime[i] = 0;
  }
  isprime[2] = 1;
  for (int i = 3; i * i < N; i += 2) {
    if (spf[i] == i) {
      isprime[i] = 1;
      for (int j = i * i; j < N; j += i) {
        isprime[j] = 0;
        if (spf[j] == j)
          spf[j] = i;
      }
    }
  }
}

int check(int n) {
  for (int i = 2; i <= sqrt(n); i++) {
    if (n % i == 0) {
      return 0;
    }
  }
  return 1;
}

int32_t main() {
  L_T_S_K;
  int n;
  cin >> n;

  if (n == 1) {
    cout << 0 << "\\n";
    return 0;
  }

  int ans = 0;
  int f = 0;
  for (int i = 2; i <= sqrt(n); i++) {
    if (n % i == 0) {
      f = 1;
      break;
    }
  }

  if (!f) {
    cout << 1 << "\\n";
    return 0;
  }
  seive();

  for (int i = 2;; i++) {
    if (check(n)) {
      ans++;
      break;
    }
    if (n == 1)
      break;
    if (spf[i] == i and n % i == 0) {
      int c = 0;
      while (n % i == 0) {
        n /= i;
        c++;
      }
      int u = 1;
      while (c - u >= 0) {
        c -= u;
        ans++;
        ++u;
      }
      if (n == 1)
        break;
    }
  }

  cout << ans << endl;

  return 0;
}