alexjercan/bugnet huggingface dataset:
Problem p00712 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int solve(int p, int q, int a, int n, int down);
int mx(int x, int y);
int xy(int x, int n);
int main() {
  int p, q, a, n;
  while (cin >> p >> q >> a >> n, p || q || a || n) {
    int ans = 0;
    for (int i = 1; i <= n; i++)
      ans += solve(p, q, a, i, q / p);
    cout << ans << endl;
  }
  return 0;
}
int solve(int p, int q, int a, int n, int down) {
  int ans = 0;
  if (p == 0)
    return 0;
  if (n == 1 && q % p == 0 && q / p <= a && down <= q / p) {
    return 1;
  }
  int up = n * q / p;
  for (int i = down; i <= up; i++) {
    if (i == 0)
      continue;
    if (a < xy(i, n))
      break;
    int after = q * i / mx(q, i);
    if (p * after / q - after / i > 0)
      ans += solve(p * after / q - after / i, after, a / i, n - 1, i);
  }
  return ans;
}
int mx(int x, int y) {
  if (y == 0)
    return x;
  return mx(y, x % y);
}
int xy(int x, int n) {
  if (n == 1)
    return x;
  return xy(x, n - 1) * x;
}

Error: -8

Fixed submission:

#include <iostream>
using namespace std;
int solve(int p, int q, int a, int n, int down);
int mx(int x, int y);
int xy(int x, int n);
int main() {
  int p, q, a, n;
  while (cin >> p >> q >> a >> n, p || q || a || n) {
    int ans = 0;
    for (int i = 1; i <= n; i++)
      ans += solve(p, q, a, i, q / p);
    cout << ans << endl;
  }
  return 0;
}
int solve(int p, int q, int a, int n, int down) {
  // cout <<p<<" "<<q<<" "<<a<<" "<<n<<" "<<down<<endl;
  int ans = 0;
  if (p == 0)
    return 0;
  if (n == 1 && q % p == 0 && q / p <= a && down <= q / p) {
    return 1;
  }
  int up = n * q / p;
  for (int i = down; i <= up; i++) {
    if (a < xy(i, n))
      break;
    int after = q * i / mx(q, i);
    if (p * after / q - after / i > 0)
      ans += solve(p * after / q - after / i, after, a / i, n - 1, i);
  }
  return ans;
}
int mx(int x, int y) {
  if (y == 0)
    return x;
  return mx(y, x % y);
}
int xy(int x, int n) {
  if (n == 1)
    return x;
  return xy(x, n - 1) * x;
}
Problem p00712 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>

using namespace std;

int p, q, a, n;

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

int counter(int p, int q, int m, int n, int minNumber) {
  if (minNumber > m)
    return 0;
  if (p * minNumber > q * n)
    return 0;
  int temp = gcd(q, minNumber);
  // if (temp >
  int ans = 0;
  int p1 = p * minNumber, q1 = q * minNumber;
  for (int i = 0; i <= n; i++) {
    if (n <= 0 || m <= 0)
      break;
    if (p1 == 0) {
      ans++;
      break;
    } else if (p1 < 0) {
      break;
    } else {
      int g = gcd(p1, q1);
      ans += counter(p1 / g, q1 / g, m, n - i, minNumber + 1);
      p1 -= q;
      m /= minNumber;
    }
  }
  // printf("%d,%d(%d)=>%d\\n", p, q, n,ans);
  return ans;
}

int main() {
  while (1) {
    scanf("%d%d%d%d", &p, &q, &a, &n);
    if (p == 0)
      break;
    int g = gcd(p, q);
    printf("%d\\n", counter(p / g, q / g, a, n, 1));
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>

using namespace std;

int p, q, a, n;

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

int counter(int p, int q, int m, int n, int minNumber) {
  if (minNumber > m)
    return 0;
  int ans = 0;
  int p1 = p * minNumber, q1 = q * minNumber;
  for (int i = 0; i <= n; i++) {
    if (n <= 0 || m <= 0)
      break;
    if (p1 == 0) {
      ans++;
      break;
    } else if (p1 < 0) {
      break;
    } else {
      int g = gcd(p1, q1);
      ans += counter(p1 / g, q1 / g, m, n - i, minNumber + 1);
      p1 -= q;
      m /= minNumber;
    }
  }
  // printf("%d,%d(%d)=>%d\\n", p, q, n,ans);
  return ans;
}

int main() {
  while (1) {
    scanf("%d%d%d%d", &p, &q, &a, &n);
    if (p == 0)
      break;
    printf("%d\\n", counter(p, q, a, n, 1));
  }
}
Problem p00712 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
typedef long long ll;
ll p, q, a, n, an;

ll gcd(ll a, ll b) {
  if (b)
    return gcd(b, a % b);
  return a;
}

void dfs(ll d, ll s, ll t, ll k, ll j) {
  if (d > n || s * q > p * k)
    return;
  if (s * q == p * k) {
    an++;
    return;
  }
  for (int i = j; t * i <= a; i++) {
    // ll cd=gcd(k,i);
    dfs(d + 1, s * i + k, t * i, k * i, i);
  }
}

int main() {
  while (cin >> p >> q >> a >> n && n) {
    an = 0;
    dfs(0, 0, 1, 1, 1);
    cout << an << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
typedef long long ll;
ll p, q, a, n, an;

ll gcd(ll a, ll b) {
  if (b)
    return gcd(b, a % b);
  return a;
}

void dfs(ll d, ll s, ll t, ll k, ll j) {
  if (d > n || s * q > p * k)
    return;
  if (s * q == p * k) {
    an++;
    return;
  }
  for (int i = j; t * i <= a; i++) {
    ll cd = gcd(k, i);
    dfs(d + 1, s * i / cd + k / cd, t * i, k * i / cd, i);
  }
}

int main() {
  while (cin >> p >> q >> a >> n && n) {
    an = 0;
    dfs(0, 0, 1, 1, 1);
    cout << an << endl;
  }
  return 0;
}
Problem p00712 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

#define ISEQ(c) (c).begin(), (c).end()
typedef long long ll;
typedef pair<int, int> P;

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

int calc(int p, int q, int a, int n, int d, int num, int prod) {
  if (p == 0)
    return 1;
  if (num == n)
    return 0;
  int res = 0;
  int g = gcd(p, q);
  p /= g;
  q /= g;
  for (int i = d; prod * i <= a; i++) {
    if (p * i - q < 0)
      continue;
    if (p * i - q * (n - num) > 0)
      break;
    res += calc(p * i - q, i * q, a, n, i, num + 1, prod * i);
  }
  return res;
}

int main() {
  while (true) {
    int p, q, a, n;
    scanf("%d%d%d%d", &p, &q, &a, &n);
    if (p == 0 and q == 0 and a == 0 and n == 0)
      break;

    printf("%d\\n", calc(p, q, a, n, 1, 0, 1));
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

#define ISEQ(c) (c).begin(), (c).end()
typedef long long ll;
typedef pair<int, int> P;

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

int calc(int p, int q, int a, int n, int d, int num, int prod) {
  if (p == 0)
    return 1;
  if (num == n)
    return 0;
  int res = 0;
  int g = gcd(p, q);
  p /= g;
  q /= g;
  for (int i = d; prod * i <= a; i++) {
    if (p * i - q < 0)
      continue;
    res += calc(p * i - q, i * q, a, n, i, num + 1, prod * i);
  }
  return res;
}

int main() {
  while (true) {
    int p, q, a, n;
    scanf("%d%d%d%d", &p, &q, &a, &n);
    if (p == 0 and q == 0 and a == 0 and n == 0)
      break;

    printf("%d\\n", calc(p, q, a, n, 1, 0, 1));
  }
}
Problem p00712 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

int p, q, a;

int solve(int n, int d, int prod, int sum) {
  int cnt = 0;
  for (int i = d; prod * i <= a; i++) {
    int left = p * (prod * i);
    int right = sum * i + prod;
    if (left < right * q) {
      continue;
    }
    if (left == right * q) {
      cnt++;
      continue;
    }
    if (n == 1) {
      continue;
    }
    cnt += solve(n - 1, i, prod * i, right);
  }

  return cnt;
}

int main() {
  int n;
  while (cin >> p >> q >> a >> n, (p || q || a || n)) {
    cout << solve(n, 1, 1, 0) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <math.h>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

string GetStr() {
  string str;
  getline(cin, str);
  return str;
}

vector<int> GetIntVector() {
  string str;
  getline(cin, str);
  istringstream line(str);

  istream_iterator<int> iit(line);
  istream_iterator<int> iit_eof;

  vector<int> ret;

  for (; iit != iit_eof; iit++) {
    ret.push_back(*iit);
  }

  return ret;
}

typedef struct {
  int p, q; // p/q
} fraction;

int counter;

int GCD(int a, int b) {
  int r;
  while ((r = a % b) != 0) {
    a = b;
    b = r;
  }

  return abs(b);
}

fraction sub(fraction l, fraction r) {
  fraction ret;
  ret.p = l.p * r.q - l.q * r.p;
  ret.q = r.q * l.q;

  int gcd = GCD(ret.p, ret.q);
  ret.p /= gcd;
  ret.q /= gcd;

  return ret;
}

void div(fraction f, int min_q, int max_q, int n) {
  if (n <= 0)
    return;

  if (f.p == 1) {
    if (f.q <= max_q && f.q >= min_q) {
      counter++;
      min_q++;
    } else {
      return;
    }
  }

  for (int i = min_q; i < max_q; i++) {
    fraction next;
    next.p = 1;
    next.q = i;

    fraction s = sub(f, next);
    if (s.p <= 0) // Ì
      continue;

    div(s, next.q, max_q / next.q, n - 1);
  }
}

void solve(int p, int q, int a, int n) {
  counter = 0;

  fraction fr;
  fr.p = p;
  fr.q = q;
  int gcd = GCD(fr.p, fr.q);
  fr.p /= gcd;
  fr.q /= gcd;

  div(fr, 2, a, n);

  cout << counter << endl;
}

void solve1131() {
  vector<int> v = GetIntVector();
  while (v[0] != 0 && v[1] != 0 && v[2] != 0 && v[3] != 0) {
    solve(v[0], v[1], v[2], v[3]);
    v = GetIntVector();
  }
}

int main() {
  solve1131();

  return 0;
}
Problem p00712 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;
int ans = 0;
int p, q, a, n;
void dfs(int num, int seki, int deno, int nume, int value) {
  if (nume > 0) {
    int g = __gcd(deno, nume);
    deno /= g;
    nume /= g;
  }
  if ((nume / (double)deno) + (double)(n - num) / (double)value <
      (double)p / (double)q - 1e-7)
    return;
  if (p * deno == q * nume) {
    ans++;
  }
  if (p * deno <= q * nume) {
    return;
  }
  if (num == n)
    return;
  for (int i = value; i * seki <= a; i++) {
    dfs(num + 1, seki * i, deno * i, nume * i + deno, i);
  }
}
int main() {

  while (cin >> p >> q >> a >> n && q && p) {
    ans = 0;
    dfs(0, 1, 1, 0, 1);
    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;
int ans = 0;
int p, q, a, n;
void dfs(int num, int seki, int deno, int nume, int value) {
  if (nume > 0) {
    int g = __gcd(deno, nume);
    deno /= g;
    nume /= g;
  }
  if (p * deno == q * nume) {
    ans++;
  }
  if (p * deno <= q * nume) {
    return;
  }
  if (num == n)
    return;
  for (int i = value; i * seki <= a; i++) {
    dfs(num + 1, seki * i, deno * i, nume * i + deno, i);
  }
}
int main() {

  while (cin >> p >> q >> a >> n && q && p) {
    ans = 0;
    dfs(0, 1, 1, 0, 1);
    cout << ans << endl;
  }

  return 0;
}
Problem p00712 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
using namespace std;
typedef long long ll;
int p, q, a, n;
int ans;
int t[10];

void rec(int x, int y, int z) {
  int sum = 0;
  for (int i = 0; i < x; i++) {
    sum += (y / t[i]);
  }
  if (sum * q == p * y) {
    ans++;
  } else if (sum * q > p * y) {
    return;
  }

  if (x == n)
    return;
  for (int i = z; i <= a; i++) {
    if (y * i > a)
      break;
    t[x] = i;
    rec(x + 1, y * i, i);
  }
}

int main() {
  while (1) {
    scanf("%d %d %d %d", &p, &q, &a, &n);
    if (p == 0 && q == 0 && a == 0 && n == 0)
      break;
    ans = 0;
    rec(0, 1, 1);
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

P change(P res) {
  ll x = __gcd(res.first, res.second);
  return P(res.first / x, res.second / x);
}
P calc(P a, P b) {
  a.first *= b.second;
  b.first *= a.second;
  P res = P(a.first - b.first, a.second * b.second);
  ll x = __gcd(res.first, res.second);
  return P(res.first / x, res.second / x);
}

ll p, q, a, n;
ll ans;

void rec(P p, ll x, ll y, ll z) {
  p = change(p);
  if (p.first == 0) {
    ans++;
    return;
  }
  if (x == n)
    return;
  for (ll i = max(p.second / p.first, z);; i++) {
    if (i * y > a)
      break;
    P np = calc(p, P(1, i));
    if (np.first < 0)
      continue;
    rec(np, x + 1, y * i, i);
  }
}

int main() {
  while (1) {
    cin >> p >> q >> a >> n;
    if (p == 0 && q == 0 && a == 0 && n == 0)
      break;
    ans = 0;
    rec(P(p, q), 0, 1, 1);
    cout << ans << endl;
  }
  return 0;
}
Problem p00712 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

int GCD(int m, int n) {
  if (n == 0)
    return m;
  return GCD(n, m % n);
}

int dfs(int p, int q, int a, int n, int temp) {
  if (a < temp || n == 0)
    return 0;
  int res = 0;
  int gcd = GCD(p, q);
  p /= gcd;
  q /= gcd;
  for (int i = max((q + p - 1) / p, temp); i <= a; i++) {
    if (p == 1 && q == i) {
      res++;
    } else
      res += dfs(p * i - q, q * i, a / i, n - 1, i);
  }
  return res;
}

int main() {
  int p, q, a, n;
  while (cin >> p >> q >> a >> n, p || q || a || n) {
    cout << dfs(p, q, a, n, 1) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int GCD(int m, int n) {
  if (n == 0)
    return m;
  return GCD(n, m % n);
}

int dfs(int p, int q, int a, int n, int temp) {
  if (a == 0 || n == 0)
    return 0;
  int res = 0;
  int gcd = GCD(p, q);
  p /= gcd;
  q /= gcd;
  for (int i = max((q + p - 1) / p, temp); i <= a; i++) {
    if (p == 1 && q == i) {
      res++;
    } else
      res += dfs(p * i - q, q * i, a / i, n - 1, i);
  }
  return res;
}

int main() {
  int p, q, a, n;
  while (cin >> p >> q >> a >> n, p || q || a || n) {
    cout << dfs(p, q, a, n, 1) << endl;
  }
  return 0;
}
Problem p00712 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
const double EPS = 1e-10;
const double PI = acos(-1.0);

int dfs(int p, int q, int a, int n, int bunbo, int bunsi, int cnt, int prev) {
  if (cnt == n) {
    return (q * bunsi == p * bunbo);
  }
  int ret = (q * bunsi == p * bunbo);
  FOR(i, prev, a + 1) {
    int new_bunbo = bunbo * i;
    int new_bunsi = bunsi * i + bunbo;
    if (new_bunbo > a)
      break;
    ret += dfs(p, q, a, n, new_bunbo, new_bunsi, cnt + 1, i);
  }
  return ret;
}
int main() {
  int p, q, a, n;
  while (cin >> p >> q >> a >> n, p | q | a | n) {
    cout << dfs(p, q, a, n, 1, 0, 0, 1) << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
const double EPS = 1e-10;
const double PI = acos(-1.0);

int dfs(int p, int q, int a, int n, int bunbo, int bunsi, int cnt, int prev) {
  if (cnt == n) {
    return (q * bunsi == p * bunbo);
  }
  int ret = (q * bunsi == p * bunbo);
  FOR(i, prev, a + 1) {
    int new_bunbo = bunbo * i;
    int new_bunsi = bunsi * i + bunbo;
    if (new_bunbo <= a) {
      ret += dfs(p, q, a, n, new_bunbo, new_bunsi, cnt + 1, i);
    }
  }
  return ret;
}
int main() {
  int p, q, a, n;
  while (cin >> p >> q >> a >> n, p | q | a | n) {
    cout << dfs(p, q, a, n, 1, 0, 0, 1) << endl;
  }
}
Problem p00712 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
struct F {
  int n, d;
  F(int n, int d) : n(n), d(d) {}
};
F operator-(const F &a, const F &b) {
  int rn = a.n * b.d - b.n * a.d, rd = a.d * b.d, g;
  return F(rn, rd);
}
int p, q, a, n;
int rec(int cp, int cq, int cd, int ca, int cn) {
  int r = 0;
  for (int nd = cd; nd * ca <= a; nd++)
    if (cp * nd >= cq && cn != n) {
      if (cp * nd == cq)
        r++;
      else {
        F nF = F(cp, cq) - F(1, nd);
        r += rec(nF.n, nF.d, nd, nd * ca, cn + 1);
      }
    }
  return r;
}
int main() {
  while (cin >> p >> q >> a >> n, p)
    cout << rec(p, q, 1, 1, 0) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int gcd(int x, int y) { return y == 0 ? x : gcd(y, x % y); }
struct F {
  int n, d;
  F(int n, int d) : n(n), d(d) {}
};
F operator-(const F &a, const F &b) {
  int rn = a.n * b.d - b.n * a.d, rd = a.d * b.d, g;
  g = gcd(rn, rd);
  return F(rn / g, rd / g);
}
int p, q, a, n;
int rec(int cp, int cq, int cd, int ca, int cn) {
  int r = 0;
  for (int nd = cd; nd * ca <= a; nd++)
    if (cp * nd >= cq && cn != n) {
      if (cp * nd == cq)
        r++;
      else {
        F nF = F(cp, cq) - F(1, nd);
        r += rec(nF.n, nF.d, nd, nd * ca, cn + 1);
      }
    }
  return r;
}
int main() {
  while (cin >> p >> q >> a >> n, p)
    cout << rec(p, q, 1, 1, 0) << endl;
  return 0;
}
Problem p00712 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long LL;
static const double EPS = 1e-9;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
struct flac {
  int p, q;
  flac(int p, int q) : p(p), q(q) {}
};

flac sumf(flac f, flac g) { return flac(f.p * g.q + f.q * g.p, f.q * g.q); }
bool operator==(flac f, flac g) { return (f.p * g.q == f.q * g.p); }
bool operator<(flac f, flac g) { return (f.p * g.q < f.q * g.p); }

int solve(flac sum, int &a, int n, flac &target, int bf) {
  if (sum == target)
    return 1;
  if (target < sum)
    return 0;
  if (n == 0)
    return 0;
  int para = sum.q;
  int ret = 0;
  FOR(i, bf, a) {
    if (i * para > a)
      break;
    ret += solve(sumf(sum, flac(1, i)), a, n - 1, target, i);
  }
  return ret;
}

int main(void) {
  int p, q, a, n;
  while (cin >> p >> q >> a >> n) {
    if (!p && !q && !a && !n)
      break;
    flac tar = flac(p, q);
    cout << solve(flac(0, 1), a, n, tar, 1) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long LL;
static const double EPS = 1e-9;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
struct flac {
  int p, q;
  flac(int p, int q) : p(p), q(q) {}
};

flac sumf(flac f, flac g) { return flac(f.p * g.q + f.q * g.p, f.q * g.q); }
bool operator==(flac f, flac g) { return (f.p * g.q == f.q * g.p); }
bool operator<(flac f, flac g) { return (f.p * g.q < f.q * g.p); }

int solve(flac sum, int &a, int n, flac &target, int bf) {
  if (sum == target)
    return 1;
  if (n == 0)
    return 0;
  int para = sum.q;
  int ret = 0;
  FOR(i, bf, a) {
    if (i * para > a)
      break;
    ret += solve(sumf(sum, flac(1, i)), a, n - 1, target, i);
  }
  return ret;
}

int main(void) {
  int p, q, a, n;
  while (cin >> p >> q >> a >> n) {
    if (!p && !q && !a && !n)
      break;
    flac tar = flac(p, q);
    cout << solve(flac(0, 1), a, n, tar, 1) << endl;
  }
  return 0;
}
Problem p00712 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) REP(i, 0, n)
#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define pb push_back
#define all(r) (r).begin(), (r).end()
#define rall(r) (r).rbegin(), (r).rend()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 1e9;
const ll LINF = 1e18;
const ll MOD = 1e9 + 7;
double EPS = 1e-8;

int P, Q, A, N;

int calc(int p, int q, int n, int a, int t) {
  if (p * Q == P * q)
    return 1;
  if (n == N)
    return 0;
  if (p * Q > P * q)
    return 0;
  int ret = 0;
  for (int i = t; i * a <= A; i++) {
    int np = p * i + q, nq = i * q;
    // int g = __gcd(np, nq);
    // np /= g;
    // nq /= g;
    ret += calc(np, nq, n + 1, i * a, i);
  }
  return ret;
}

int main() {
  // while(cin >> P >> Q >> A >> N) {
  while (scanf("%d %d %d %d", &P, &Q, &A, &N) != EOF) {
    if (P == 0 && Q == 0 && A == 0 && N == 0)
      break;
    // cout << calc(0, 1, 0, 1, 1) << endl;
    printf("%d\\n", calc(0, 1, 0, 1, 1));
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) REP(i, 0, n)
#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define pb push_back
#define all(r) (r).begin(), (r).end()
#define rall(r) (r).rbegin(), (r).rend()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 1e9;
const ll LINF = 1e18;
const ll MOD = 1e9 + 7;
double EPS = 1e-8;

int P, Q, A, N;

int calc(int p, int q, int n, int a, int t) {
  if (p * Q == P * q)
    return 1;
  if (n == N)
    return 0;
  if (p * Q > P * q)
    return 0;
  int ret = 0;
  for (int i = t; i * a <= A; i++) {
    int np = p * i + q, nq = i * q;
    int g = __gcd(np, nq);
    np /= g;
    nq /= g;
    ret += calc(np, nq, n + 1, i * a, i);
  }
  return ret;
}

int main() {
  // while(cin >> P >> Q >> A >> N) {
  while (scanf("%d %d %d %d", &P, &Q, &A, &N) != EOF) {
    if (P == 0 && Q == 0 && A == 0 && N == 0)
      break;
    // cout << calc(0, 1, 0, 1, 1) << endl;
    printf("%d\\n", calc(0, 1, 0, 1, 1));
  }
}