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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctype.h>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
#define pb push_back
#define mp make_pair
using namespace std;
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define REP(i, x, n) for (int i = x; i < n; i++)
#define NREP(i, n) FOR(i, 1, n)
using ll = long long;
using pii = pair<int, int>;
using PII = pair<ll, ll>;
using piii = pair<pii, pii>;
using Pi = pair<int, pii>;
using Graph = vector<vector<int>>;
const int dx[4] = {0, -1, 1, 0};
const int dy[4] = {-1, 0, 0, 1};
bool check(int x, int y, int n, int m) {
  if (1 <= x && x <= n && 1 <= y && y <= m)
    return true;
  else
    return false;
}
const ll INF = 1e+7;
int gcd(int x, int y) {
  if (x < y)
    swap(x, y);
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
void mul(ll a, ll b) { a = a * b % INF; }
using Graph = vector<vector<int>>;

long long modinv(long long a, long long m) {
  long long b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}
const double PI = 3.14159265358979323846;
const int MAX = 510000;
ll fac[MAX], finv[MAX], inv[MAX];
// テーブルをつくる前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1; // mod pにおける1,2,,,nの逆元
  for (int i = 2; i < MAX; ++i) {
    fac[i] = fac[i - 1] * i % INF;
    inv[i] = INF - inv[INF % i] * (INF / i) % INF;
    finv[i] = finv[i - 1] * inv[i] % INF;
  }
}

ll COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % INF) % INF;
}
double Euclidean_distance(double x1, double y1, double x2, double y2) {
  return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}

int prime[1001000];     // i番目の素数
bool is_prime[1001010]; // is_prime[i]がtrueならiは素数
                        // n以下の素数の数を返す
int sieve(int n) {
  int p = 0;
  for (int i = 0; i <= n; ++i) {
    is_prime[i] = true;
  }
  is_prime[0] = false;
  is_prime[1] = false;
  for (int i = 2; i <= n; ++i) {
    if (is_prime[i]) {
      prime[p] = i;
      p++;
      for (int j = 2 * i; j <= n; j += i) {
        is_prime[j] = false;
      }
    }
  }
  return p;
}

// 素因数分解
map<int, int> prime_factor(int n) {
  map<int, int> res;
  for (int i = 2; i * i <= n; ++i) {
    while (n % i == 0) {
      ++res[i];
      n /= i;
    }
  }
  if (n != 1)
    res[n] = 1;
  return res;
}

ll powmod(ll a, ll k, ll mod) {
  ll ap = a, ans = 1;
  while (k) {
    if (k & 1) {
      ans *= ap;
      ans %= mod;
    }
    ap = ap * ap;
    ap %= mod;
    k >>= 1;
  }
  return ans;
}
ll invi(ll a, ll mod) { return powmod(a, mod - 2, mod); }
// 逆元Aを足したときのmodで割った余りは
//+=invi(A)
///////////////////////////////////////

// dp[v][s]:=頂点vにいて所持金sという状態になるのに要する最小の所要時間
int findpos(ll k, ll hieghts[], ll n) {
  int l = 1, r = n;
  while (l < r) {
    int mid = (l + r) / 2;
    if (hieghts[mid] < k) {
      l = mid + 1;
    } else if (hieghts[mid] > k) {
      r = mid;
    } else {
      return mid;
    }
  }
  return l;
}
int main() {
  ll n;
  cin >> n;
  vector<pair<ll, ll>> v;
  int c = 0;
  while (n % 2 == 0) {
    n = n / 2;
    c++;
  }
  if (c > 0) {
    v.pb({2, c});
  }
  for (int i = 3; i <= n && i <= 1000000; i += 2) {
    c = 0;
    while (n % i == 0) {
      n = n / i;
      c++;
    }
    if (c > 0) {
      v.pb({i, c});
    }
  }
  if (n > 3) {
    v.pb({n, 1});
  }
  ll no = 0;
  for (auto p : v) {
    ll x = 1, ad = 1;
    // cout<<p.second<<endl;
    while (x <= p.second) {
      no++;
      ad++;
      x += ad;
    }
    // cout<<no<<endl;
  }
  cout << no;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctype.h>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
#define pb push_back
#define mp make_pair
using namespace std;
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define REP(i, x, n) for (int i = x; i < n; i++)
#define NREP(i, n) FOR(i, 1, n)
using ll = long long;
using pii = pair<int, int>;
using PII = pair<ll, ll>;
using piii = pair<pii, pii>;
using Pi = pair<int, pii>;
using Graph = vector<vector<int>>;
const int dx[4] = {0, -1, 1, 0};
const int dy[4] = {-1, 0, 0, 1};
bool check(int x, int y, int n, int m) {
  if (1 <= x && x <= n && 1 <= y && y <= m)
    return true;
  else
    return false;
}
const ll INF = 1e+7;
int gcd(int x, int y) {
  if (x < y)
    swap(x, y);
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
void mul(ll a, ll b) { a = a * b % INF; }
using Graph = vector<vector<int>>;

long long modinv(long long a, long long m) {
  long long b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}
const double PI = 3.14159265358979323846;
const int MAX = 510000;
ll fac[MAX], finv[MAX], inv[MAX];
// テーブルをつくる前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1; // mod pにおける1,2,,,nの逆元
  for (int i = 2; i < MAX; ++i) {
    fac[i] = fac[i - 1] * i % INF;
    inv[i] = INF - inv[INF % i] * (INF / i) % INF;
    finv[i] = finv[i - 1] * inv[i] % INF;
  }
}

ll COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % INF) % INF;
}
double Euclidean_distance(double x1, double y1, double x2, double y2) {
  return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}

int prime[1001000];     // i番目の素数
bool is_prime[1001010]; // is_prime[i]がtrueならiは素数
                        // n以下の素数の数を返す
int sieve(int n) {
  int p = 0;
  for (int i = 0; i <= n; ++i) {
    is_prime[i] = true;
  }
  is_prime[0] = false;
  is_prime[1] = false;
  for (int i = 2; i <= n; ++i) {
    if (is_prime[i]) {
      prime[p] = i;
      p++;
      for (int j = 2 * i; j <= n; j += i) {
        is_prime[j] = false;
      }
    }
  }
  return p;
}

// 素因数分解
map<int, int> prime_factor(int n) {
  map<int, int> res;
  for (int i = 2; i * i <= n; ++i) {
    while (n % i == 0) {
      ++res[i];
      n /= i;
    }
  }
  if (n != 1)
    res[n] = 1;
  return res;
}

ll powmod(ll a, ll k, ll mod) {
  ll ap = a, ans = 1;
  while (k) {
    if (k & 1) {
      ans *= ap;
      ans %= mod;
    }
    ap = ap * ap;
    ap %= mod;
    k >>= 1;
  }
  return ans;
}
ll invi(ll a, ll mod) { return powmod(a, mod - 2, mod); }
// 逆元Aを足したときのmodで割った余りは
//+=invi(A)
///////////////////////////////////////

// dp[v][s]:=頂点vにいて所持金sという状態になるのに要する最小の所要時間
int findpos(ll k, ll hieghts[], ll n) {
  int l = 1, r = n;
  while (l < r) {
    int mid = (l + r) / 2;
    if (hieghts[mid] < k) {
      l = mid + 1;
    } else if (hieghts[mid] > k) {
      r = mid;
    } else {
      return mid;
    }
  }
  return l;
}
int main() {
  ll n;
  cin >> n;
  vector<pair<ll, ll>> v;
  int c = 0;
  while (n % 2 == 0) {
    n = n / 2;
    c++;
  }
  if (c > 0) {
    v.pb({2, c});
  }
  for (int i = 3; i <= n; i += 2) {
    c = 0;
    while (n % i == 0) {
      n = n / i;
      c++;
    }
    if (c > 0) {
      v.pb({i, c});
    }
  }
  if (n > 3) {
    v.pb({n, 1});
  }
  ll no = 0;
  for (auto p : v) {
    ll x = 1, ad = 1;
    // cout<<p.second<<endl;
    while (x <= p.second) {
      no++;
      ad++;
      x += ad;
    }
    // cout<<no<<endl;
  }
  cout << no;
}