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

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <stack>
#include <vector>

using namespace std;

#define MAX 1000003

vector<int> vprimes;
bool *isPrime = new bool[MAX];

// Ååöñ
template <typename Integer> Integer GCD(Integer a, Integer b) {
  if (!b)
    return a;
  else
    return GCD(b, a % b);
}

void MakePrime() {
  for (int i = 0; i < MAX; ++i)
    isPrime[i] = true;
  isPrime[0] = false;
  isPrime[1] = false;
  for (long long int i = 2; i < MAX; ++i)
    if (isPrime[i])
      for (long long int j = 2; i * j < MAX; ++j)
        isPrime[i * j] = false;
  for (int i = 0; i < MAX; ++i)
    if (isPrime[i])
      vprimes.push_back(i);
}

template <typename Integer>
void Factorization(Integer n, vector<pair<Integer, Integer>> &vp) {
  if (n == 1) {
    vp.push_back(make_pair(1, 1));
    return;
  }
  for (int i = 0; i < vprimes.size(); ++i) {
    while (n % vprimes[i] == 0) {
      n /= vprimes[i];
      if (vp.size() > 0 && vp.back().first == vprimes[i])
        vp.back().second++;
      else
        vp.push_back(make_pair(vprimes[i], 1));
    }
    if (n == 1)
      return;
  }
  vp.push_back(make_pair(n, 1));
}

// ®ªÌñ,vvÉ½¢µÄÐÆÂÌªvª èAvÌ®Íå«¢ÉÀÔ
void partitions(int n, int max_n, vector<int> &v, vector<vector<int>> &vv) {
  if (n == 0) {
    vv.push_back(v);
    return;
  }
  for (int i = min(n, max_n); i > 0; i--) {
    v.push_back(i);
    partitions(n - i, i, v, vv);
    v.pop_back();
  }
}

template <typename Integer> Integer divisorFunction0(Integer n) {
  Integer ret = 1;
  if (n == 1)
    return 1;
  stack<pair<Integer, Integer>> st;
  vector<pair<Integer, Integer>> facts;
  Factorization(n, facts);
  for (int i = 0; i < facts.size(); ++i) {
    ret *= facts[i].second + 1;
  }
  return ret;
}

// æ1íñÖ@ñÌaðßé
// ½ªO(sqrt(n))+alpha­ç¢Å®­
template <typename Integer>
Integer divisorFunction1(Integer n, vector<Integer> &vdivs) {
  Integer ret = 0;
  if (n == 1)
    return 1;
  stack<pair<Integer, Integer>> st;
  vector<pair<Integer, Integer>> facts;
  Factorization(n, facts);
  st.push(make_pair(0, 1));
  while (!st.empty()) {
    pair<Integer, Integer> nst = st.top();
    st.pop();
    if (nst.first >= facts.size()) {
      vdivs.push_back(nst.second);
      continue;
    }
    Integer r = 1;
    for (int i = 0; i < facts[nst.first].second + 1; ++i) {
      pair<Integer, Integer> nx(nst.first + 1, nst.second * r);
      st.push(nx);
      r *= facts[nst.first].first;
    }
  }
  return ret;
}

int main() {
  MakePrime();
  while (true) {
    long long int res = 0;
    long long int L;
    vector<long long int> vdiv;
    cin >> L;
    if (L == 0)
      break;
    divisorFunction1(L, vdiv);
    res += vdiv.size();
    // cout << vdiv.size() << endl;
    for (int i = 1; i < vdiv.size(); ++i) {
      for (int j = i + 1; j < vdiv.size() - 1; ++j) {
        if (GCD(vdiv[i], vdiv[j]) == 1) {
          ++res;
          // break;
        }
      }
    }
    if (L == 1)
      res = 1;
    cout << res << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <stack>
#include <vector>

using namespace std;

#define MAX 10000003
#define KMAX 1000000000000003LL

vector<int> vprimes;
bool *isPrime = new bool[MAX];

void MakePrime() {
  for (int i = 0; i < MAX; ++i)
    isPrime[i] = true;
  isPrime[0] = false;
  isPrime[1] = false;
  for (long long int i = 2; i < MAX; ++i)
    if (isPrime[i])
      for (long long int j = 2; i * j < MAX; ++j)
        isPrime[i * j] = false;
  for (int i = 0; i < MAX; ++i)
    if (isPrime[i])
      vprimes.push_back(i);
}

template <typename Integer>
void Factorization(Integer n, vector<pair<Integer, Integer>> &vp) {
  if (n == 1) {
    vp.push_back(make_pair(1, 1));
    return;
  }
  for (int i = 0; i < vprimes.size(); ++i) {
    while (n % vprimes[i] == 0) {
      n /= vprimes[i];
      if (vp.size() > 0 && vp.back().first == vprimes[i])
        vp.back().second++;
      else
        vp.push_back(make_pair(vprimes[i], 1));
    }
    if (n == 1)
      return;
  }
  vp.push_back(make_pair(n, 1));
}

// ®ªÌñ,vvÉ½¢µÄÐÆÂÌªvª èAvÌ®Íå«¢ÉÀÔ
void partitions(int n, int max_n, vector<int> &v, vector<vector<int>> &vv) {
  if (n == 0) {
    vv.push_back(v);
    return;
  }
  for (int i = min(n, max_n); i > 0; i--) {
    v.push_back(i);
    partitions(n - i, i, v, vv);
    v.pop_back();
  }
}

template <typename Integer> Integer divisorFunction0(Integer n) {
  Integer ret = 1;
  if (n == 1)
    return 1;
  stack<pair<Integer, Integer>> st;
  vector<pair<Integer, Integer>> facts;
  Factorization(n, facts);
  for (int i = 0; i < facts.size(); ++i) {
    ret *= facts[i].second + 1;
  }
  return ret;
}

int main() {
  MakePrime();
  while (true) {
    long long int res = 0;
    long long int L;
    cin >> L;
    if (L == 0)
      break;
    res += divisorFunction0(L);
    vector<pair<long long int, long long int>> facts;
    Factorization(L, facts);
    if (facts.size() > 1) {
      res += facts.size();
    }
    cout << res << endl;
  }
  return 0;
}