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

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvl;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<pair<int, int>> vp;
typedef pair<int, int> pr;
typedef tuple<int, int, int> ti;

#define FOR(i, a, b) for (int i = (a), _b = (b); i < _b; i++)
#define FORD(i, b, a) for (int i = (b), _a = (a); i > _a; i--)
#define pb push_back
#define print(v) cout << v << '\\n'
#define pr_vec(v)                                                              \\
  for (int i = 0; i != v.size(); i++)                                          \\
  cout << v[i] << ' '
#define vin(v)                                                                 \\
  for (auto &i : v)                                                            \\
  cin >> i

ld pi = 3.141592653589793238;
ll M = 1e9 + 7;

int gcd(int a, int b) {
  if (a == 0)
    return b;

  return gcd(b % a, a);
}

bool is_prime(ll n) {
  if (n == 2)
    return true;
  if (n < 2)
    return false;
  if (n % 2 == 0)
    return false;

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

  return true;
}

bool is_palindrome(string s) { return equal(s.rbegin(), s.rend(), s.begin()); }

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll t, n, m, k, q, x, y;
  string s;
  bool flag;

  cin >> n;

  if (is_prime(n)) {
    print(1);
    return 0;
  }

  m = 2;
  k = 0;

  unordered_map<ll, ll> factors;

  while (n % 2 == 0) {
    k++;
    n /= 2;
  }

  if (k > 0)
    factors[2] = k;
  k = 0;

  for (int i = 3; i <= sqrt(n); i += 2) {
    k = 0;
    while (n % i == 0) {
      k++;
      n /= i;
    }
    factors[i] = k;
  }
  if (n > 2) {
    factors[n] = 1;
  }

  // for (auto i: factors) {
  //     cout << i.first << " -- " << i.second << '\\n';
  // }

  m = 0;

  for (auto i : factors) {
    k = i.second;

    ll j = 1;
    while ((j * (j + 1)) / 2 <= k) {
      j++;
    }
    m += j - 1;
  }

  print(m);

  // while (m <= n) {
  //     if (n % m == 0) {
  //         if (is_prime(m)) {
  //             ll div = m;
  //             while (m < n && n % div == 0) {
  //                 n /= div;
  //                 div *= m;
  //                 k++;
  //             }
  //         }
  //     } else if (is_prime(n)) {
  //         k++;
  //         break;
  //     }
  //     m++;
  // }

  // print(k);

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvl;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<pair<int, int>> vp;
typedef pair<int, int> pr;
typedef tuple<int, int, int> ti;

#define FOR(i, a, b) for (int i = (a), _b = (b); i < _b; i++)
#define FORD(i, b, a) for (int i = (b), _a = (a); i > _a; i--)
#define pb push_back
#define print(v) cout << v << '\\n'
#define pr_vec(v)                                                              \\
  for (int i = 0; i != v.size(); i++)                                          \\
  cout << v[i] << ' '
#define vin(v)                                                                 \\
  for (auto &i : v)                                                            \\
  cin >> i

ld pi = 3.141592653589793238;
ll M = 1e9 + 7;

int gcd(int a, int b) {
  if (a == 0)
    return b;

  return gcd(b % a, a);
}

bool is_prime(ll n) {
  if (n == 2)
    return true;
  if (n < 2)
    return false;
  if (n % 2 == 0)
    return false;

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

  return true;
}

bool is_palindrome(string s) { return equal(s.rbegin(), s.rend(), s.begin()); }

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll t, n, m, k, q, x, y;
  string s;
  bool flag;

  cin >> n;

  if (is_prime(n)) {
    print(1);
    return 0;
  }

  m = 2;
  k = 0;

  while (m <= n) {
    if (n % m == 0) {
      if (is_prime(m)) {
        ll div = m;
        while (n % div == 0) {
          n /= div;
          div *= m;
          k++;
        }
      }
    } else if (is_prime(n)) {
      k++;
      break;
    }
    m++;
  }

  print(k);

  return 0;
}