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

#include <bits/stdc++.h>
#define pb push_back
#define mk make_pair
#define ll long long
#define vi vector<ll>
#define ss second
#define ff first
#define pii pair<int, int>
#define mii map<int, int>
#define mod 1000000007
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
#define ps(x, y) fixed << setprecision(y) << x;
#define fo(i, j, k, in) for (ll i = j; i < k; i += in)
#define re(i, j) fo(i, 0, j, 1)
#define pi 3.1415926535897932384626433832795
#define all(cont) cont.begin(), cont.end()
#define countbit(x) __builtin_popcount(x)
#define mod 1000000007
#define lo lower_bound
#define de(n)                                                                  \\
  ll n;                                                                        \\
  cin >> n;
#define def(a, n)                                                              \\
  ll n;                                                                        \\
  cin >> n;                                                                    \\
  ll a[n];                                                                     \\
  re(i, n) { cin >> a[i]; }
#define z 1000000000000000000
using namespace std;
ll ab(ll a, ll b) {
  if (a > b)
    return a - b;
  return b - a;
}
ll factor(ll n) {
  ll ans = 0;
  for (ll j = 1; j * j <= n; j++) {
    if (n % j == 0) {
      if (j * j == n)
        ans++;
      else {
        ans += 2;
      }
    }
  }
  return ans;
}
ll power(ll x, ll y, ll p) {
  int res = 1;
  x = x % p;
  if (x == 0)
    return 0;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;

    y = y >> 1; // y = y/2
    x = (x * x) % p;
  }
  return res;
}
ll pow(ll x, ll y) {
  int res = 1;

  while (y > 0) {
    if (y & 1)
      res = res * x;

    y = y >> 1;
    x = x * x;
  }
  return res;
}
void cp() {
  ios ::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);

#endif
}
vector<ll> get_prime_factors_less_than(ll n) {
  vector<ll> factor;
  vector<ll> prime(n, 1);
  prime[0] = 0, prime[1] = 0;
  for (ll i = 2; i <= n; i++) {
    if (prime[i] == 1) {
      for (ll j = i * i; j <= n; j = j + i)
        prime[j] = 0;
      factor.push_back(i);
    }
  }
  return factor;
}
ll digit(ll n) {
  ll q = 0;
  while (n > 0) {
    q++;
    n /= 10;
  }
  return q;
}
ll sq(ll n) { return n * n; }
ll NR(ll n, ll r) {
  long long p = 1, k = 1;
  if (n - r < r)
    r = n - r;

  if (r != 0) {
    while (r) {
      p *= n;
      k *= r;

      // gcd of p, k
      long long m = __gcd(p, k);
      p /= m;
      k /= m;

      n--;
      r--;
    }
  }

  else
    p = 1;

  // if our approach is correct p = ans and k =1
  return p;
}
bool isPrime(ll n) {
  // Corner cases
  if (n <= 1)
    return false;
  if (n <= 3)
    return true;

  // This is checked so that we can skip
  // middle five numbers in below loop
  if (n % 2 == 0 || n % 3 == 0)
    return false;

  for (ll i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0)
      return false;

  return true;
}
int main() {
  cp();
  de(n);
  if (n == 1) {
    cout << "0\\n";
    exit(0);
  }
  ll ans = 0;
  ll k = 1;
  ll g = 2;
  if (isPrime(n) == true) {
    cout << "1\\n";
    exit(0);
  }
  while (n % pow(g, k) == 0) {
    n /= pow(g, k);
    ans++;
    k++;
  }
  while (n % 2 == 0) {
    n /= 2;
  }
  // cout<<ans<<"kk\\n";
  if (n == 3 || n == 5 || n == 7) {
    ans++;
  } else {
    for (ll g = 3; g * g <= n; g += 2) {
      k = 1;
      bool change = 0;
      while (n % pow(g, k) == 0) {
        n /= pow(g, k);
        ans++;
        k++;
        change = 1;
      }
      while (n % g == 0) {
        change = 1;
        n /= g;
      }
      if (change == 1) {
        if (isPrime(n) == true) {
          ans++;
          break;
        }
      }
      if (n < g)
        break;
    }
  }
  cout << ans;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define pb push_back
#define mk make_pair
#define ll long long
#define vi vector<ll>
#define ss second
#define ff first
#define pii pair<int, int>
#define mii map<int, int>
#define mod 1000000007
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
#define ps(x, y) fixed << setprecision(y) << x;
#define fo(i, j, k, in) for (ll i = j; i < k; i += in)
#define re(i, j) fo(i, 0, j, 1)
#define pi 3.1415926535897932384626433832795
#define all(cont) cont.begin(), cont.end()
#define countbit(x) __builtin_popcount(x)
#define mod 1000000007
#define lo lower_bound
#define de(n)                                                                  \\
  ll n;                                                                        \\
  cin >> n;
#define def(a, n)                                                              \\
  ll n;                                                                        \\
  cin >> n;                                                                    \\
  ll a[n];                                                                     \\
  re(i, n) { cin >> a[i]; }
#define z 1000000000000000000
using namespace std;
ll ab(ll a, ll b) {
  if (a > b)
    return a - b;
  return b - a;
}
ll factor(ll n) {
  ll ans = 0;
  for (ll j = 1; j * j <= n; j++) {
    if (n % j == 0) {
      if (j * j == n)
        ans++;
      else {
        ans += 2;
      }
    }
  }
  return ans;
}
ll power(ll x, ll y, ll p) {
  int res = 1;
  x = x % p;
  if (x == 0)
    return 0;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;

    y = y >> 1; // y = y/2
    x = (x * x) % p;
  }
  return res;
}
ll pow(ll x, ll y) {
  int res = 1;

  while (y > 0) {
    if (y & 1)
      res = res * x;

    y = y >> 1;
    x = x * x;
  }
  return res;
}
void cp() {
  ios ::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);

#endif
}
vector<ll> get_prime_factors_less_than(ll n) {
  vector<ll> factor;
  vector<ll> prime(n, 1);
  prime[0] = 0, prime[1] = 0;
  for (ll i = 2; i <= n; i++) {
    if (prime[i] == 1) {
      for (ll j = i * i; j <= n; j = j + i)
        prime[j] = 0;
      factor.push_back(i);
    }
  }
  return factor;
}
ll digit(ll n) {
  ll q = 0;
  while (n > 0) {
    q++;
    n /= 10;
  }
  return q;
}
ll sq(ll n) { return n * n; }
ll NR(ll n, ll r) {
  long long p = 1, k = 1;
  if (n - r < r)
    r = n - r;

  if (r != 0) {
    while (r) {
      p *= n;
      k *= r;

      // gcd of p, k
      long long m = __gcd(p, k);
      p /= m;
      k /= m;

      n--;
      r--;
    }
  }

  else
    p = 1;

  // if our approach is correct p = ans and k =1
  return p;
}
bool isPrime(ll n) {
  // Corner cases
  if (n <= 1)
    return false;
  if (n <= 3)
    return true;

  // This is checked so that we can skip
  // middle five numbers in below loop
  if (n % 2 == 0 || n % 3 == 0)
    return false;

  for (ll i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0)
      return false;

  return true;
}
int main() {
  cp();
  de(n);
  if (n == 1) {
    cout << "0\\n";
    exit(0);
  }
  ll ans = 0;
  ll k = 1;
  ll g = 2;
  if (isPrime(n) == true) {
    cout << "1\\n";
    exit(0);
  }
  while (n % pow(g, k) == 0) {
    n /= pow(g, k);
    ans++;
    k++;
  }
  while (n % 2 == 0) {
    n /= 2;
  }
  for (ll g = 3; g <= n; g += 2) {
    k = 1;
    while (n % pow(g, k) == 0) {
      n /= pow(g, k);
      ans++;
      k++;
    }
    while (n % g == 0) {
      n /= g;
    }
    if (isPrime(n) == true) {
      ans++;
      break;
    }
    if (n < g)
      break;
  }
  cout << ans;
  return 0;
}