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

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>

#define int long long
#define rep(i, begin, end)                                                     \\
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \\
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define F first
#define S second
#define sz(x) ((int)x.size())
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define pb push_back
#define pf push_front
#define eb emplace_back
#define all(v) (v).begin(), (v).end()
#define mod 100000
#define mi multiset<int>
#define mii multiset<pii>
#define what_is(x) cerr << #x << " is " << x << "\\n";
#define sortA(v) sort(v.begin(), v.end())
#define sortD(v) sort(v.rbegin(), v.rend())
#define PI 3.14159265358979323846
#define vout(a)                                                                \\
  for (auto x : a)                                                             \\
    cout << x << " ";
#define INF 10000000000000000

// #define ordered_set tree<ll, null_type, less<ll>, rb_tree_tag,
// tree_order_statistics_node_update>

// using namespace __gnu_pbds;
using namespace std;

int lcm(int a, int b) { return (a * (b / __gcd(a, b))); }

int modp(int a, int m, int n) {
  // n=n%(m-1);
  // a=a%m;
  int res = 1;
  while (n > 0) {
    if (n & 1)
      res = (res * a); //%m;
    a = (a * a);       //%m;
    n >>= 1;
  }
  return res;
}

map<int, int> prime;

void primeFactors(int n) {
  // Print the number of 2s that divide n
  while (n % 2 == 0) {
    prime[2]++;
    n = n / 2;
  }

  // n must be odd at this point. So we can skip
  // one element (Note i = i +2)
  for (int i = 3; i <= sqrt(n); i = i + 2) {
    // While i divides n, print i and divide n
    while (n % i == 0) {
      prime[i]++;
      n = n / i;
    }
  }

  // This condition is to handle the case when n
  // is a prime number greater than 2
  if (n > 2)
    prime[n]++;
}
void solve() {
  int t, n, m, i, k, l, r, u, v, w, j, s, pos, sum = 0, flag = 0, count = 0;
  string S, T, U;
  // int h1,m1,h2,m2;
  // long double a,b;
  cin >> n;
  primeFactors(n);
  for (auto x : prime) {
    j = 1;
    // cout<<x.F<<" "<<x.S<<"\\n";
    t = x.S;
    while (t >= 0) {
      t -= j;
      j++;
      if (t >= 0)
        count++;
    }
  }
  cout << count;
  return;
  // vi fact;
  // // seive(n+1);
  // for(int i=1;i*i<=n;i++)
  // {
  //     if(n%i==0)
  //     {
  //         fact.pb(i);
  //         if(i!=n/i) fact.pb(n/i);
  //     }
  // }
  // sort(all(fact));
  // m=sz(fact);
  // j=1;
  // // // vout(fact) cout<<"\\n";
  // // map<int,int> vis;
  // rep(i,1,m)
  // {
  //     if(fact[i]%2||fact[i]==2)
  //     {

  //         j=1;
  //         t=fact[i];
  //         // cout<<t<<" ";
  //         while(n%t==0)
  //         {
  //             n/=t;

  //             j++;
  //             t*=fact[i];
  //             count++;
  //         }

  //         // cout<<fact[i]<<" ";
  //     }
  // }
  // cout<<count;
}

signed main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
    if (t)
      cout << "\\n";
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>

#define int long long
#define rep(i, begin, end)                                                     \\
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \\
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define F first
#define S second
#define sz(x) ((int)x.size())
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define pb push_back
#define pf push_front
#define eb emplace_back
#define all(v) (v).begin(), (v).end()
#define mod 100000
#define mi multiset<int>
#define mii multiset<pii>
#define what_is(x) cerr << #x << " is " << x << "\\n";
#define sortA(v) sort(v.begin(), v.end())
#define sortD(v) sort(v.rbegin(), v.rend())
#define PI 3.14159265358979323846
#define vout(a)                                                                \\
  for (auto x : a)                                                             \\
    cout << x << " ";
#define INF 10000000000000000

// #define ordered_set tree<ll, null_type, less<ll>, rb_tree_tag,
// tree_order_statistics_node_update>

// using namespace __gnu_pbds;
using namespace std;

int lcm(int a, int b) { return (a * (b / __gcd(a, b))); }

int modp(int a, int m, int n) {
  // n=n%(m-1);
  // a=a%m;
  int res = 1;
  while (n > 0) {
    if (n & 1)
      res = (res * a); //%m;
    a = (a * a);       //%m;
    n >>= 1;
  }
  return res;
}

const int MAX_SIZE = 2000001;

vector<int> isprime(MAX_SIZE, true);
vector<int> prime;
vector<int> SPF(MAX_SIZE);

void seive(int N) {
  isprime[0] = isprime[1] = false;
  for (int i = 2; i < N; i++) {
    if (isprime[i]) {
      prime.push_back(i);
      SPF[i] = i;
    }
    for (int j = 0;
         j < (int)prime.size() && i * prime[j] < N && prime[j] <= SPF[i]; j++) {
      isprime[i * prime[j]] = false;
      SPF[i * prime[j]] = prime[j];
    }
  }
}

void solve() {
  int t, n, m, i, k, l, r, u, v, w, j, s, pos, sum = 0, flag = 0, count = 0;
  string S, T, U;
  // int h1,m1,h2,m2;
  // long double a,b;
  cin >> n;
  i = 2;
  map<int, int> cnt;
  while (n) {
    if (i % 2 == 0 && i != 2) {
      i++;
      continue;
    }
    while (n % i == 0 && n) {
      n /= i;

      cnt[i]++;
      if (n == 1 || n == 0)
        break;
    }
    if (n == 1 || n == 0)
      break;
    i++;
  }
  for (auto x : cnt) {
    j = 1;
    t = x.S;
    while (t >= 0) {
      t -= j;
      j++;
      if (t >= 0)
        count++;
    }
  }
  cout << count;
  return;
  // vi fact;
  // // seive(n+1);
  // for(int i=1;i*i<=n;i++)
  // {
  //     if(n%i==0)
  //     {
  //         fact.pb(i);
  //         if(i!=n/i) fact.pb(n/i);
  //     }
  // }
  // sort(all(fact));
  // m=sz(fact);
  // j=1;
  // // vout(fact) cout<<"\\n";
  // map<int,int> vis;
  // rep(i,1,m)
  // {
  //     if(fact[i]%2||fact[i]==2)
  //     {

  //         j=1;
  //         t=modp(fact[i],0,j);
  //         // cout<<t<<" ";
  //         while(n%t==0)
  //         {
  //             n/=t;

  //             j++;
  //             t=modp(fact[i],0,j);
  //             count++;
  //         }

  //         // cout<<fact[i]<<" ";
  //     }
  // }
  // cout<<count;
}

signed main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
    if (t)
      cout << "\\n";
  }
  return 0;
}