Problem p00646 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>

#ifdef _MSC_VER
#include <agents.h>
#endif

#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define REV(v) v.rbegin(), v.rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define X first
#define Y second
#define MP make_pair
#define umap unordered_map

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> P;
typedef unsigned int uint;

vector<pair<ll, int>> v;

ll _pow(ll x, ll n) {
  ll res = 1;
  while (n) {
    if (n & 1)
      res *= x;
    x *= x;
    n /= 2;
  }
  return res;
}

int ans;
void dfs(int n, ll a, ll b) {
  if (n == v.size()) {
    if (a <= b) {
      // cout << a << ' ' << b << endl;
      ++ans;
    }
    return;
  }

  int x = v[n].first;
  int m = v[n].second;
  for (int i = 0; i < m; ++i) {
    dfs(n + 1, a * _pow(x, i), b * _pow(x, m));
    dfs(n + 1, a * _pow(x, m), b * _pow(x, i));
  }
  dfs(n + 1, a * _pow(x, m), b * _pow(x, m));
}

int main() {
  ll L;
  while (cin >> L, L) {
    ans = 0;
    v.clear();
    for (int i = 2; i * i <= L; ++i) {
      int cnt = 0;
      while (L % i == 0) {
        ++cnt;
        L /= i;
      }
      if (cnt)
        v.push_back(MP(i, cnt));
    }
    if (L != 1)
      v.push_back(MP(L, 1));

    dfs(0, 1, 1);
    cout << ans << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>

#ifdef _MSC_VER
#include <agents.h>
#endif

#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define REV(v) v.rbegin(), v.rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define X first
#define Y second
#define MP make_pair
#define umap unordered_map

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> P;
typedef unsigned int uint;

vector<pair<ll, int>> v;

ll _pow(ll x, ll n) {
  ll res = 1;
  while (n) {
    if (n & 1)
      res *= x;
    x *= x;
    n /= 2;
  }
  return res;
}

int ans;
void dfs(int n, ll a, ll b) {
  if (n == v.size()) {
    if (a <= b) {
      // cout << a << ' ' << b << endl;
      ++ans;
    }
    return;
  }

  int x = v[n].first;
  int m = v[n].second;
  for (int i = 0; i < m; ++i) {
    dfs(n + 1, a * _pow(x, i), b * _pow(x, m));
    dfs(n + 1, a * _pow(x, m), b * _pow(x, i));
  }
  dfs(n + 1, a * _pow(x, m), b * _pow(x, m));
}

int main() {
  ll L;
  while (cin >> L, L) {
    ans = 0;
    v.clear();
    for (ll i = 2; i * i <= L; ++i) {
      int cnt = 0;
      while (L % i == 0) {
        ++cnt;
        L /= i;
      }
      if (cnt)
        v.push_back(MP(i, cnt));
    }
    if (L != 1)
      v.push_back(MP(L, 1));

    dfs(0, 1, 1);
    cout << ans << endl;
  }

  return 0;
}