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

#include <bits/stdc++.h> //  IT'S NEVER TOO LATE...
using namespace std;
#include <math.h>
#define ll long long
#define fore(i, l, r) for (int i = int(l); i < int(r); ++i)
#define forn(i, n) fore(i, 0, n)
#define fori(i, l, r) fore(i, l, (r) + 1)
#define sz(v) int((v).size())
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define mp make_pair
#define ff first
#define ss second

const double PI = atan(1) * 4;

int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

vector<int> ar[1000001];
vector<pair<int, int>> path;
int vis[1000001], dis[1000001];

/*void dfs(int node)
{
    vis[node]=1;

    for(int child : ar[node])
    {
        if(vis[child]==0)
        {
            dis[child]=dis[node]+1;
            dfs(child);
        }
    }
}*/

/*    void bfs(int i)
    {
        queue<int> q;
        q.push(i);
        vis[i]=1;
        dis[i]=0;

        while(!q.empty())
        {
            int curr=q.front();
            q.pop();

            for(int child : ar[curr])
                if(vis[child]==0)
                {
                    q.push(child);
                    dis[child]=dis[curr]+1;
                    vis[child]=1;
                }
        }
    }*/

bool isprime(int n) {
  for (int i = 2; i * i <= n; i++)
    if (n % i == 0)
      return false;
  return true;
}

bool comp(pair<int, int> a, pair<int, int> b) { return b.ff < a.ff; }

int modpow(int b, int p, int m) {
  if (p == 0)
    return 1 % m;
  if (p % 2)
    return (b * modpow(b, p - 1, m)) % m;
  int u = modpow(b, p / 2, m);
  return (u * u) % m;
}

int c[55], dp[300];

int fn(int n, int m) {
  // cout<<n<<" ";
  if (n == 0)
    return 1;

  if (dp[n])
    return dp[n];
  int ans = 0;

  forn(i, m) {
    // cout<<c[i]<<"\\n";
    if (c[i] <= n)
      ans += fn(n - c[i], m);
  }
  dp[n] = ans;
  return ans;
}

int main() {

  int t = 1;
  // cin>>t;

  while (t--) {
    ll n, k;
    cin >> n;
    k = n;

    map<ll, ll> m;

    for (ll i = 2; i * i <= k; i++) {
      // cout<<i<<"\\n";
      while (n % i == 0) {
        m[i]++;

        n /= i;
      }
      if (n == 1)
        break;
    }
    if (n > 1)
      m[k]++;

    ll ans = 0;
    for (auto i : m) {
      // cout<<i.ff <<" "<< i.ss <<"\\n";
      for (ll j = 1;; j++) {
        // cout<<j<<"\\n";
        if ((j * (j + 1)) / 2 <= i.ss)
          ans++;
        else
          break;
      }
    }

    cout << ans;
  }
  return 0;
}

//              NOW IT'S TIME TO WAKE UP...

Error: TLE

Fixed submission:

#include <bits/stdc++.h> //  IT'S NEVER TOO LATE...
using namespace std;
#include <math.h>
#define ll long long
#define fore(i, l, r) for (int i = int(l); i < int(r); ++i)
#define forn(i, n) fore(i, 0, n)
#define fori(i, l, r) fore(i, l, (r) + 1)
#define sz(v) int((v).size())
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define mp make_pair
#define ff first
#define ss second

const double PI = atan(1) * 4;

int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

vector<int> ar[1000001];
vector<pair<int, int>> path;
int vis[1000001], dis[1000001];

/*void dfs(int node)
{
    vis[node]=1;

    for(int child : ar[node])
    {
        if(vis[child]==0)
        {
            dis[child]=dis[node]+1;
            dfs(child);
        }
    }
}*/

/*    void bfs(int i)
    {
        queue<int> q;
        q.push(i);
        vis[i]=1;
        dis[i]=0;

        while(!q.empty())
        {
            int curr=q.front();
            q.pop();

            for(int child : ar[curr])
                if(vis[child]==0)
                {
                    q.push(child);
                    dis[child]=dis[curr]+1;
                    vis[child]=1;
                }
        }
    }*/

bool isprime(int n) {
  for (int i = 2; i * i <= n; i++)
    if (n % i == 0)
      return false;
  return true;
}

bool comp(pair<int, int> a, pair<int, int> b) { return b.ff < a.ff; }

int modpow(int b, int p, int m) {
  if (p == 0)
    return 1 % m;
  if (p % 2)
    return (b * modpow(b, p - 1, m)) % m;
  int u = modpow(b, p / 2, m);
  return (u * u) % m;
}

int c[55], dp[300];

int fn(int n, int m) {
  // cout<<n<<" ";
  if (n == 0)
    return 1;

  if (dp[n])
    return dp[n];
  int ans = 0;

  forn(i, m) {
    // cout<<c[i]<<"\\n";
    if (c[i] <= n)
      ans += fn(n - c[i], m);
  }
  dp[n] = ans;
  return ans;
}

int main() {

  int t = 1;
  // cin>>t;

  while (t--) {
    int n, k;
    cin >> n;
    k = n;

    map<int, int> m;

    for (int i = 2; i * i <= k; i++) {
      // cout<<i<<"\\n";
      while (n % i == 0) {
        m[i]++;

        n /= i;
      }
      if (n == 1)
        break;
    }
    if (n > 1)
      m[k]++;

    int ans = 0;
    for (auto i : m) {
      // cout<<i.ff <<" "<< i.ss <<"\\n";
      for (int j = 1;; j++) {
        // cout<<j<<"\\n";
        if ((j * (j + 1)) / 2 <= i.ss)
          ans++;
        else
          break;
      }
    }

    cout << ans;
  }
  return 0;
}

//              NOW IT'S TIME TO WAKE UP...