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

#include <bits/stdc++.h>
using namespace std;
/*{{{*/ // template
#define REP(i, n) for (int i = 0; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 29
#define LINF LLONG_MAX / 3
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(v) (v).begin(), (v).end()
#define all(v) ALL(v)
#define debug(x) cerr << #x << ":" << x << endl
#define debug2(x, y) cerr << #x << "," << #y ":" << x << "," << y << endl
#define CININIT cin.tie(0), ios::sync_with_stdio(false)
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}

constexpr ll mod = 1e9 + 7;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
/*}}}*/

int p, q, a, n;

pii add(pii i1, int c) {
  int a = i1.first, b = i1.second;
  int d = a * c;
  int e = b * c + a;
  //    int t = gcd(d,e);
  //    return make_pair(d/t,e/t);
  return pii(d, e);
}

// sum = (bunbo,bunsi)
int dfs(pii sum, ll product, int start, int num) {
  if (sum.second * q == sum.first * p) {
    return 1;
  }
  if (num == n || sum.second * q > sum.first * p)
    return 0;

  int ret = 0;
  for (int i = start; i < a / product + 1; i++) {
    if (i * product > a)
      break;
    pii ns = add(sum, i);
    ret += dfs(add(sum, i), product * i, i, num + 1);
  }
  return ret;
}

int main() {
  while (cin >> p >> q >> a >> n) {
    if (p == 0 && q == 0 && a == 0 && n == 0)
      break;
    int g = gcd(p, q);
    p /= g;
    q /= g;
    cout << dfs(make_pair(1, 0), 1, 1, 0) << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
/*{{{*/ // template
#define REP(i, n) for (int i = 0; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 29
#define LINF LLONG_MAX / 3
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(v) (v).begin(), (v).end()
#define all(v) ALL(v)
#define debug(x) cerr << #x << ":" << x << endl
#define debug2(x, y) cerr << #x << "," << #y ":" << x << "," << y << endl
#define CININIT cin.tie(0), ios::sync_with_stdio(false)
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}

constexpr ll mod = 1e9 + 7;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
/*}}}*/

int p, q, a, n;

pii add(pii i1, int c) {
  int a = i1.first, b = i1.second;
  int d = a * c;
  int e = b * c + a;
  int t = gcd(d, e);
  return make_pair(d / t, e / t);
}

// sum = (bunbo,bunsi)
int dfs(pii sum, ll product, int start, int num) {
  if (sum.second * q == sum.first * p) {
    return 1;
  }
  if (product > a || num == n || sum.second * q > sum.first * p)
    return 0;

  int ret = 0;
  for (int i = start; i < a / product + 1; i++) {
    pii ns = add(sum, i);
    if (i * product > a)
      break;
    ret += dfs(add(sum, i), product * i, i, num + 1);
  }
  return ret;
}

int main() {
  while (cin >> p >> q >> a >> n) {
    if (p == 0 && q == 0 && a == 0 && n == 0)
      break;
    int g = gcd(p, q);
    p /= g;
    q /= g;
    cout << dfs(make_pair(1, 0), 1, 1, 0) << endl;
  }
}