Problem p00712 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define mind(a, b) (a > b ? b : a)
#define maxd(a, b) (a > b ? a : b)
#define absd(x) (x < 0 ? -(x) : x)
#define pow2(x) ((x) * (x))
#define rep(i, n) for (int i = 0; i < n; ++i)
#define repr(i, n) for (int i = n - 1; i >= 0; --i)
#define repl(i, s, n) for (int i = s; i <= n; ++i)
#define replr(i, s, n) for (int i = n; i >= s; --i)
#define repf(i, s, n, j) for (int i = s; i <= n; i += j)
#define repe(e, obj) for (auto e : obj)

#define SP << " " <<
#define COL << " : " <<
#define COM << ", " <<
#define ARR << " -> " <<
#define PNT(STR) cout << STR << endl
#define POS(X, Y) "(" << X << ", " << Y << ")"
#define DEB(A) " (" << #A << ") " << A
#define DEBREP(i, n, val)                                                      \
  for (int i = 0; i < n; ++i)                                                  \
    cout << val << " ";                                                        \
  cout << endl
#define ALL(V) (V).begin(), (V).end()
#define INF 1000000007
#define INFLL 10000000000000000007LL
#define EPS 1e-9

typedef unsigned int uint;
typedef unsigned long ulong;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
// typedef pair<ll, ll> P;
typedef pair<P, int> PI;
typedef pair<int, P> IP;
typedef pair<P, P> PP;
typedef priority_queue<P, vector<P>, greater<P>> pvqueue;

ll gcd(ll m, ll n) {
  if (m < n)
    return gcd(n, m);
  ll r = m % n;
  return r ? gcd(n, r) : n;
}

int p, q, a, n;
// p/q = 1/a{0} + ... + 1/a{c-1} + x/y
// where P_{i=0..c-1}a{i} = s
int dfs(ll x, ll y, ll s, ll c, ll now) {
  int res = (x == 1 && now <= y && s * y <= a && c + 1 <= n);
  if (s * now > a || c >= n)
    return res;
  // use
  int nx = x * now - y, ny = y * now;
  if (nx != 0) {
    int g = gcd(nx, ny);
    nx /= g;
    ny /= g;
    int nxt = (ny + nx - 1) / nx;
    nxt = maxd(nxt, now);
    res += dfs(nx, ny, s * now, c + 1, nxt);
  }
  // next
  res += dfs(x, y, s, c, now + 1);
  return res;
}

int main() {
  while (cin >> p >> q >> a >> n && p + q + a + n) {
    int g = gcd(p, q);
    cout << dfs(p / g, q / g, 1, 0, maxd(1, (p + q - 1) / p)) << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define mind(a, b) (a > b ? b : a)
#define maxd(a, b) (a > b ? a : b)
#define absd(x) (x < 0 ? -(x) : x)
#define pow2(x) ((x) * (x))
#define rep(i, n) for (int i = 0; i < n; ++i)
#define repr(i, n) for (int i = n - 1; i >= 0; --i)
#define repl(i, s, n) for (int i = s; i <= n; ++i)
#define replr(i, s, n) for (int i = n; i >= s; --i)
#define repf(i, s, n, j) for (int i = s; i <= n; i += j)
#define repe(e, obj) for (auto e : obj)

#define SP << " " <<
#define COL << " : " <<
#define COM << ", " <<
#define ARR << " -> " <<
#define PNT(STR) cout << STR << endl
#define POS(X, Y) "(" << X << ", " << Y << ")"
#define DEB(A) " (" << #A << ") " << A
#define DEBREP(i, n, val)                                                      \
  for (int i = 0; i < n; ++i)                                                  \
    cout << val << " ";                                                        \
  cout << endl
#define ALL(V) (V).begin(), (V).end()
#define INF 1000000007
#define INFLL 10000000000000000007LL
#define EPS 1e-9

typedef unsigned int uint;
typedef unsigned long ulong;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
// typedef pair<ll, ll> P;
typedef pair<P, int> PI;
typedef pair<int, P> IP;
typedef pair<P, P> PP;
typedef priority_queue<P, vector<P>, greater<P>> pvqueue;

ll gcd(ll m, ll n) {
  if (m < n)
    return gcd(n, m);
  ll r = m % n;
  return r ? gcd(n, r) : n;
}

int p, q, a, n;
// p/q = 1/a{0} + ... + 1/a{c-1} + x/y
// where P_{i=0..c-1}a{i} = s
int dfs(ll x, ll y, ll s, ll c, ll now) {
  int res = (x == 1 && now <= y && s * y <= a && c + 1 <= n);
  if (s * now > a || c >= n || x * now > y * (n - c))
    return res;
  // use
  int nx = x * now - y, ny = y * now;
  if (nx != 0) {
    int g = gcd(nx, ny);
    nx /= g;
    ny /= g;
    int nxt = (ny + nx - 1) / nx;
    nxt = maxd(nxt, now);
    res += dfs(nx, ny, s * now, c + 1, nxt);
  }
  // next
  res += dfs(x, y, s, c, now + 1);
  return res;
}

int main() {
  while (cin >> p >> q >> a >> n && p + q + a + n) {
    int g = gcd(p, q);
    cout << dfs(p / g, q / g, 1, 0, maxd(1, (p + q - 1) / p)) << endl;
  }
  return 0;
}