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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef istringstream ISS;
typedef ostringstream OSS;
typedef vector<string> VS;
typedef int INT;
typedef vector<INT> VI;
typedef vector<VI> VVI;
typedef pair<INT, INT> II;
typedef vector<II> VII;

template <class T> ostream &operator<<(ostream &os, vector<T> v) {
  for (typename vector<T>::iterator it_i = v.begin(); it_i != v.end(); ++it_i) {
    os << *it_i << ", ";
  }
  return os;
}

typedef long long LL;
const int SIZE = 1000001;
const int ESIZE = 1001;
LL L;
bool isPrime[SIZE];
LL P[SIZE];
int PC;
int E[ESIZE];
int EC;
int res;

void makePrimes() {
  PC = 0;
  memset(P, false, sizeof(P));
  for (int i = 3; i < SIZE; i += 2) {
    isPrime[i] = true;
  }
  P[PC++] = 2;
  for (int i = 3; i < SIZE; i += 2) {
    if (!isPrime[i])
      continue;
    P[PC++] = i;
    for (int j = i + i; j < SIZE; j += i) {
      isPrime[j] = false;
    }
  }
}

void init() { memset(E, 0, sizeof(E)); }

void calcExp(LL L) {
  EC = 0;
  int i = 0;
  for (; i < PC && L > 1; ++i) {
    LL p = P[i];
    if (L % p == 0) {
      while (L > 1 && L % p == 0) {
        E[EC]++;
        L /= p;
      }
      EC++;
    }
  }
  if (i >= PC) {
    E[EC++] = 1;
  }
}

void dfs(int k, int r) {
  if (k >= EC) {
    res += r;
    return;
  }
  for (int i = 0; i < E[k]; ++i)
    dfs(k + 1, r);
  dfs(k + 1, r * (E[k] + 1));
}

int solve() {
  calcExp(L);
  res = 0;
  dfs(0, 1);
  return (res + 1) >> 1;
}

int main() {
  makePrimes();
  while (cin >> L && L) {
    init();
    cout << solve() << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef istringstream ISS;
typedef ostringstream OSS;
typedef vector<string> VS;
typedef int INT;
typedef vector<INT> VI;
typedef vector<VI> VVI;
typedef pair<INT, INT> II;
typedef vector<II> VII;

template <class T> ostream &operator<<(ostream &os, vector<T> v) {
  for (typename vector<T>::iterator it_i = v.begin(); it_i != v.end(); ++it_i) {
    os << *it_i << ", ";
  }
  return os;
}

typedef long LL;
const int SIZE = 1000001;
LL L;
bool isPrime[SIZE];
LL P[SIZE];
int PC;
int E[SIZE];
int EC;
int res;

void makePrimes() {
  PC = 0;
  memset(P, false, sizeof(P));
  for (int i = 3; i < SIZE; i += 2) {
    isPrime[i] = true;
  }
  P[PC++] = 2;
  for (int i = 3; i < SIZE; i += 2) {
    if (!isPrime[i])
      continue;
    P[PC++] = i;
    for (int j = i + i; j < SIZE; j += i) {
      isPrime[j] = false;
    }
  }
}

void init() { memset(E, 0, sizeof(E)); }

void calcExp(LL L) {
  EC = 0;
  int i = 0;
  for (; i < PC && L > 1; ++i) {
    LL p = P[i];
    if (L % p == 0) {
      while (L > 1 && L % p == 0) {
        E[EC]++;
        L /= p;
      }
      EC++;
    }
  }
  if (i == PC) {
    E[EC++] = 1;
  }
}

void dfs(int k, int r) {
  if (k >= EC) {
    res += r;
    return;
  }
  for (int i = 0; i < E[k]; ++i)
    dfs(k + 1, r);
  dfs(k + 1, r * (E[k] + 1));
}

int solve() {
  calcExp(L);
  res = 0;
  dfs(0, 1);
  return (res + 1) / 2;
}

int main() {
  makePrimes();
  while (cin >> L && L) {
    init();
    cout << solve() << endl;
  }
  return 0;
}