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

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount

#define rep(i, n) for (int i = 0; i < n; ++i)

template <class T> void max_swap(T &a, const T &b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T &b) { a = min(a, b); }

typedef long long ll;
typedef pair<int, int> pint;

const double PI = acos(-1.0);
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

template <class T> map<T, int> prime_factors(T n) {
  map<T, int> res;
  for (T i = 2; i * i <= n; ++i) {
    if (n % i == 0) {
      int c = 0;
      for (; n % i == 0; n /= i)
        ++c;
      res[i] = c;
    }
  }
  if (n > 1)
    res[n] = 1;
  return res;
}
template <class T> vector<T> divisors(T n) {
  vector<T> res;
  for (T i = 1; i * i <= n; ++i) {
    if (n % i == 0) {
      res.push_back(i);
      if (i != n / i)
        res.push_back(n / i);
    }
  }
  return res;
}
vector<int> fact_num, take;
ll dfs(int depth) {
  if (depth == take.size()) {
    ll res = 1;
    for (int i = 0; i < take.size(); ++i)
      if (take[i] == fact_num[i])
        res *= fact_num[i] + 1;
    return res;
  } else {
    ll res = 0;
    for (int i = 0; i <= fact_num[depth]; ++i) {
      take[depth] = i;
      res += dfs(depth + 1);
    }
    return res;
  }
}
int main() {
  ll L;
  while (scanf("%lld", &L), L) {
    map<ll, int> f = prime_factors(L);
    fact_num = vector<int>();
    foreach (it, f)
      fact_num.push_back(it->second);
    take.resize(fact_num.size());

    printf("%lld\\n", (dfs(0) + 1) / 2);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount

#define rep(i, n) for (int i = 0; i < n; ++i)

template <class T> void max_swap(T &a, const T &b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T &b) { a = min(a, b); }

typedef long long ll;
typedef pair<int, int> pint;

const double PI = acos(-1.0);
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

template <class T> map<T, int> prime_factors(T n) {
  map<T, int> res;
  for (T i = 2; i * i <= n; ++i) {
    if (n % i == 0) {
      int c = 0;
      for (; n % i == 0; n /= i)
        ++c;
      res[i] = c;
    }
  }
  if (n > 1)
    res[n] = 1;
  return res;
}
template <class T> vector<T> divisors(T n) {
  vector<T> res;
  for (T i = 1; i * i <= n; ++i) {
    if (n % i == 0) {
      res.push_back(i);
      if (i != n / i)
        res.push_back(n / i);
    }
  }
  return res;
}
int main() {
  ll L;
  while (scanf("%lld", &L), L) {
    // map<ll, int> f = prime_factors(L);
    // vector<int> fact_num, power;
    // foreach (it, f)
    //{
    //	fact_num.push_back(it->second);
    //	power.push_back(1);
    //	for (int i = 0; i < it->second; ++i)
    //		power.back() *= it->first;
    // }

    // vector<ll> div = divisors(L);

    vector<int> fact_num;
    vector<ll> power;
    ll n = L;
    for (ll i = 2; i * i <= n; ++i) {
      if (n % i == 0) {
        int c = 0;
        ll t = 1;
        for (; n % i == 0; n /= i)
          ++c, t *= i;
        fact_num.push_back(c);
        power.push_back(t);
      }
    }
    if (n > 1) {
      fact_num.push_back(1);
      power.push_back(n);
    }

    vector<ll> div;
    for (ll i = 1; i * i <= L; ++i) {
      if (L % i == 0) {
        div.push_back(i);
        if (i != L / i)
          div.push_back(L / i);
      }
    }

    ll res = 1;
    foreach (it, div) {
      ll d = *it;
      ll pairs = 1;
      for (int i = 0; i < fact_num.size(); ++i)
        if (d % power[i] == 0)
          pairs *= fact_num[i] + 1;

      res += pairs;
    }
    res /= 2;
    printf("%lld\\n", res);
  }
}