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

#include <bits/stdc++.h>

#define ms(a, b) memset(a, b, sizeof(a))
#define rep(a, b, c) for (int a = (int)(b); a < (int)(c); a++)
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define m_p(a, b) make_pair(a, b)
#define lson l, mid, o << 1
#define rson mid + 1, r, o << 1 | 1
#define ls o << 1
#define rs o << 1 | 1
#define inf 0x3f3f3f3f

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef pair<int, int> pii;

inline bool isdigit(char &ch) { return ch >= '0' && ch <= '9'; }
template <class T> void read(T &x) {
  x = 0;
  ll f = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); ch = getchar())
    x = (x << 1) + (x << 3) + ch - '0';
  x *= f;
}
template <class T> inline void write(T x) {
  if (x == 0) {
    putchar('0');
    return;
  }
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  int _stk[65], _top = 0;
  for (; x; x /= 10)
    _stk[++_top] = x % 10 + 48;
  for (; _top; _top--)
    putchar(_stk[_top]);
}

const int MAXN = 1e6 + 10;

int arr[70], cnt;
ll a;
bool vis[MAXN];
int pr[MAXN];
int calc(int x) {
  int t = 1;
  int ss = 0;
  while (x >= t) {
    ss++;
    x -= t;
    t++;
  }
  return ss;
}
int main() {
  read(a);
  ll sum = 0;
  for (int i = 2; i <= MAXN; ++i) {
    if (!vis[i]) {
      pr[++pr[0]] = i;
      for (int j = i + i; j <= MAXN; j += i)
        vis[j] = 1;
    }
  }
  for (int i = 1; i <= pr[0]; ++i) {
    if (a % pr[i] == 0) {
      cnt++;
      while (a % pr[i] == 0) {
        a /= pr[i];
        arr[cnt]++;
      }
    }
  }
  if (a > 1) {
    cnt++;
    arr[cnt]++;
  }
  rep(i, 1, cnt + 1) {
    //	cout << arr[i] << " "<<calc(arr[i]) << endl;
    sum += calc(arr[i]);
  }
  cout << sum;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define ms(a, b) memset(a, b, sizeof(a))
#define rep(a, b, c) for (int a = (int)(b); a < (int)(c); a++)
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define m_p(a, b) make_pair(a, b)
#define lson l, mid, o << 1
#define rson mid + 1, r, o << 1 | 1
#define ls o << 1
#define rs o << 1 | 1
#define inf 0x3f3f3f3f

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef pair<int, int> pii;

inline bool isdigit(char &ch) { return ch >= '0' && ch <= '9'; }
template <class T> void read(T &x) {
  x = 0;
  ll f = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); ch = getchar())
    x = (x << 1) + (x << 3) + ch - '0';
  x *= f;
}
template <class T> inline void write(T x) {
  if (x == 0) {
    putchar('0');
    return;
  }
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  int _stk[65], _top = 0;
  for (; x; x /= 10)
    _stk[++_top] = x % 10 + 48;
  for (; _top; _top--)
    putchar(_stk[_top]);
}

int arr[70], cnt;
ll a;
int calc(int x) {
  int t = 1;
  int ss = 0;
  while (x >= t) {
    ss++;
    x -= t;
    t++;
  }
  return ss;
}
int main() {
  read(a);
  ll sum = 0;
  for (int i = 2; i * i <= a; ++i) {
    if (a % i == 0) {
      cnt++;
      while (a % i == 0) {
        arr[cnt]++;
        a /= i;
      }
    }
  }
  if (a > 1) {
    cnt++;
    arr[cnt]++;
  }
  rep(i, 1, cnt + 1) {
    //	cout << arr[i] << " "<<calc(arr[i]) << endl;
    sum += calc(arr[i]);
  }
  cout << sum;
  return 0;
}