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

#include <bits/stdc++.h>
using namespace std;

#define dump(...) cout << "# " << #__VA_ARGS__ << '=' << (__VA_ARGS__) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define peri(i, a, b) for (int i = int(b); i-- > int(a);)
#define rep(i, n) repi(i, 0, n)
#define per(i, n) peri(i, 0, n)
#define all(c) begin(c), end(c)
#define mp make_pair
#define mt make_tuple

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << '(' << p.first << ',' << p.second << ')';
}

template <typename Tuple> void print_tuple(ostream &, const Tuple &) {}
template <typename Car, typename... Cdr, typename Tuple>
void print_tuple(ostream &os, const Tuple &t) {
  print_tuple<Cdr...>(os, t);
  os << (sizeof...(Cdr) ? "," : "") << get<sizeof...(Cdr)>(t);
}
template <typename... Args>
ostream &operator<<(ostream &os, const tuple<Args...> &t) {
  print_tuple<Args...>(os << '(', t);
  return os << ')';
}

template <typename Ch, typename Tr, typename C, typename = decltype(begin(C()))>
basic_ostream<Ch, Tr> &operator<<(basic_ostream<Ch, Tr> &os, const C &c) {
  os << '[';
  for (auto i = begin(c); i != end(c); ++i)
    os << (i == begin(c) ? "" : " ") << *i;
  return os << ']';
}

constexpr int INF = 1e9;
constexpr int MOD = 1e9 + 7;
constexpr double EPS = 1e-9;

ll solve_naive(ll n) {
  int res = 0;
  repi(i, 1, n + 1) repi(j, i, n + 1) if (i / __gcd(i, j) * j == n) res++;
  return res;
}

ll solve(ll n) {
  vector<tuple<ll, int>> fs;
  {
    map<ll, int> tmp;
    for (ll i = 2; i * i <= n; i++)
      while (n % i == 0) {
        tmp[i]++;
        n /= i;
      }
    if (n != 1)
      tmp[n]++;
    for (auto p : tmp)
      fs.push_back(p);
  }
  int m = fs.size();

  set<tuple<ll, ll>> s;
  rep(i, 1 << m) {
    ll x = 1, y = 1;
    rep(j, m) {
      ll &z = i >> j & 1 ? x : y;
      rep(k, get<1>(fs[j])) z *= get<0>(fs[j]);
    }
    s.insert(mt(min(x, y), max(x, y)));
  }

  rep(i, m) {
    ll pri, cnt;
    tie(pri, cnt) = fs[i];
    set<tuple<ll, ll>> t = s;
    for (auto p : s) {
      ll x, y;
      tie(x, y) = p;
      ll &z = x % pri ? x : y;
      rep(k, cnt) {
        z *= pri;
        t.insert(mt(min(x, y), max(x, y)));
      }
    }
    swap(s, t);
  }
  return s.size();
}

int main() {
  for (ll n; cin >> n && n;)
    cout << solve(n) << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define dump(...) cout << "# " << #__VA_ARGS__ << '=' << (__VA_ARGS__) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define peri(i, a, b) for (int i = int(b); i-- > int(a);)
#define rep(i, n) repi(i, 0, n)
#define per(i, n) peri(i, 0, n)
#define all(c) begin(c), end(c)
#define mp make_pair
#define mt make_tuple

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << '(' << p.first << ',' << p.second << ')';
}

template <typename Tuple> void print_tuple(ostream &, const Tuple &) {}
template <typename Car, typename... Cdr, typename Tuple>
void print_tuple(ostream &os, const Tuple &t) {
  print_tuple<Cdr...>(os, t);
  os << (sizeof...(Cdr) ? "," : "") << get<sizeof...(Cdr)>(t);
}
template <typename... Args>
ostream &operator<<(ostream &os, const tuple<Args...> &t) {
  print_tuple<Args...>(os << '(', t);
  return os << ')';
}

template <typename Ch, typename Tr, typename C, typename = decltype(begin(C()))>
basic_ostream<Ch, Tr> &operator<<(basic_ostream<Ch, Tr> &os, const C &c) {
  os << '[';
  for (auto i = begin(c); i != end(c); ++i)
    os << (i == begin(c) ? "" : " ") << *i;
  return os << ']';
}

constexpr int INF = 1e9;
constexpr int MOD = 1e9 + 7;
constexpr double EPS = 1e-9;

int main() {
  for (ll n; cin >> n && n;) {
    map<int, int> fs;
    for (int i = 2; i * i <= n; i++)
      while (n % i == 0) {
        fs[i]++;
        n /= i;
      }
    if (n != 1)
      fs[n]++;

    ll res = 1;
    for (auto p : fs)
      res *= 2 * (p.second + 1);
    cout << res / 2 << endl;
  }
}