alexjercan/bugnet huggingface dataset:
Problem p00712 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int solve(int p, int q, int a, int n, int cur, int mult) {
  if (n >= 0 && p == 0)
    return 1;
  if (n <= 0 || p <= 0)
    return 0;

  int res = 0;
  for (int i = cur; mult * i <= a; i++) {
    res += solve(p * i - q, q * i, a, n - 1, i, mult * i);
  }
  return res;
}

int main(void) {
  int p, q, a, n;

  while (cin >> p >> q >> a >> n && p) {
    cout << solve(p, q, a, n, 1, 1) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int solve(int p, int q, int a, int n, int cur, int mult) {
  if (n == 0 && p == 0)
    return 1;
  if (n <= 0 || p <= 0)
    return 0;

  int res = 0;
  for (int i = cur; mult * i <= a; i++) {
    res += solve(p * i - q, q * i, a, n - 1, i, mult * i);
  }
  return res;
}

int main(void) {
  int p, q, a, n;

  while (cin >> p >> q >> a >> n && p) {
    int ans = 0;
    for (int i = 1; i <= n; i++)
      ans += solve(p, q, a, i, 1, 1);
    cout << ans << endl;
  }

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

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
int MAX;

// int memo[810][810][9];

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

pii frac_minus(int p, int q, int mp, int mq) { // p/q - mp/mq
  int nume = p * mq - q * mp;
  int deno = q * mq;
  int g = gcd(nume, deno);
  return pii(nume / g, deno / g);
}

int solve(int p, int q, int prod, int n, int pre) {

  if (prod > MAX or p < 0 or q < 0)
    return 0;
  if (n < 0)
    return 0;

  int ans = 0;
  if (p == 0)
    return 1;
  //			ans++;
  /*
  if(p == 1 and (ll)q*(ll)prod <= MAX and pre <= q and q>0){
//			cout << "(" << p <<"/"<<q<<","<<prod<<","<<n<<") ";
                ans++;
  }
  */

  int high;
  if (p != 0)
    high = min(MAX, q * n / p);
  else
    high = 12000;
  for (int j = pre; j <= high; j++) {
    pii tmp = frac_minus(p, q, 1, j);
    int np = tmp.first;
    int nq = tmp.second;
    if (prod * j > MAX)
      break;
    ans += solve(np, nq, prod * j, n - 1, j);
  }

  return ans;
}

int main() {

  while (1) {

    int p, q, n;
    cin >> p >> q >> MAX >> n;
    if (!p)
      break;
    int g = gcd(p, q);
    cout << solve(p / g, q / g, 1, n, 1) << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
int MAX;

// int memo[810][810][9];

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

pii frac_minus(int p, int q, int mp, int mq) { // p/q - mp/mq
  int nume = p * mq - q * mp;
  int deno = q * mq;
  int g = gcd(nume, deno);
  return pii(nume / g, deno / g);
}

int solve(int p, int q, int prod, int n, int pre) {

  if (prod > MAX or p < 0 or q < 0)
    return 0;
  if (n < 0)
    return 0;

  int ans = 0;
  if (p == 0)
    ans++;
  /*
  if(p == 1 and (ll)q*(ll)prod <= MAX and pre <= q and q>0){
//			cout << "(" << p <<"/"<<q<<","<<prod<<","<<n<<") ";
                ans++;
  }
  */

  for (int j = pre; j <= 12000; j++) {
    pii tmp = frac_minus(p, q, 1, j);
    int np = tmp.first;
    int nq = tmp.second;
    if (prod * j > MAX)
      break;
    ans += solve(np, nq, prod * j, n - 1, j);
  }

  return ans;
}

int main() {

  while (1) {

    int p, q, n;
    cin >> p >> q >> MAX >> n;
    if (!p)
      break;
    int g = gcd(p, q);
    cout << solve(p / g, q / g, 1, n, 1) << endl;
  }
}
Problem p00712 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define fi first
#define se second
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb(s) push_back(s)
#define mp(a, b) make_pair(a, b)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define maxch(x, y) x = max(x, y)
#define minch(x, y) x = min(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt(x) bitset<32>(x).count()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> PPI;
typedef pair<ll, ll> PL;
typedef pair<P, ll> PPL;

#define INF INT_MAX / 3

#define MAX_N 1000

int p, q, a, n;

int dfs(int np, int nq, int low, int nn) {
  if (nn > n)
    return 0;
  if (np * q == nq * p)
    return 1;
  if (np * q > nq * p)
    return 0;
  int res = 0;
  for (int i = low; i * nq <= a; i++) {
    res += dfs(i * np + nq, i * nq, i, nn + 1);
  }
  return res;
}

int main() {
  cin.sync_with_stdio(false);
  while (1) {
    cin >> p >> q >> a >> n;
    if (!p)
      break;
    cout << dfs(0, 1, 1, 0) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

#define fi first
#define se second
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb(s) push_back(s)
#define mp(a, b) make_pair(a, b)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define maxch(x, y) x = max(x, y)
#define minch(x, y) x = min(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt(x) bitset<32>(x).count()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> PPI;
typedef pair<ll, ll> PL;
typedef pair<P, ll> PPL;

#define INF INT_MAX / 3

#define MAX_N 1000

int p, q, a, n;

//// gcd
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

int dfs(int np, int nq, int na, int lc, int d) {
  if (d == n)
    return (np == 1 && na * nq <= a && nq >= lc ? 1 : 0);
  if (max(lc, nq / np + 1) > a)
    return 0;
  int res = 0;
  for (int c = max(lc, nq / np + 1); c * na <= a; c++) {
    int nnp, nnq;
    nnp = c * np - nq;
    nnq = c * nq;
    int gg = gcd(nnp, nnq);
    res += dfs(nnp / gg, nnq / gg, na * c, c, d + 1);
  }
  if (np == 1 && na * nq <= a && nq >= lc) {
    // dbg(nq);
    res++;
  }
  return res;
}

int main() {
  cin.sync_with_stdio(false);
  while (1) {
    cin >> p >> q >> a >> n;
    if (!p)
      break;
    int gg = gcd(p, q);
    cout << dfs(p / gg, q / gg, 1, 1, 1) << endl;
  }
  return 0;
}
Problem p00712 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <utility>

using namespace std;

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

int lcm(int a, int b) {
  int t = a;
  while (t % b != 0)
    t += a;
  return t;
}

typedef pair<int, int> R;

void yakubun(R &a) {
  int d = gcd(a.first, a.second);
  a.first /= d;
  a.second /= d;
}

bool cmp(R a, R b) {
  double va = (double)a.first / (double)a.second;
  double vb = (double)b.first / (double)b.second;
  return va <= vb;
}

R sub(R a, R b) {
  int m = lcm(a.second, b.second);
  int t = a.first * (m / a.second) - b.first * (m / b.second);
  R r = make_pair(t, m);
  yakubun(r);
  return r;
}

double val(R a) { return (double)a.first / (double)a.second; }

int calc = 0;
int A;
int f(R mx, R cur, int a, int n) {
  int ret = 0;
  if (cur.first == 0)
    return 1;
  if (n == 0)
    return ret;
  for (int k = 1; k <= A / a && cmp(cur, make_pair(n, k)); k++) {
    calc++;
    R r = make_pair(1, k);
    if (cmp(r, mx) && cmp(r, cur)) {
      ret += f(r, sub(cur, r), a * k, n - 1);
    }
  }
  return ret;
}

int main() {
  while (true) {
    int p, q, n;
    cin >> p >> q >> A >> n;
    if (p == 0 && q == 0 && A == 0 && n == 0)
      return 0;
    calc = 0;
    cout << f(make_pair(p, q), make_pair(p, q), 1, n) << endl;
    // cerr << "calc=" << calc << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <utility>

using namespace std;

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

int lcm(int a, int b) {
  int t = a;
  while (t % b != 0)
    t += a;
  return t;
}

typedef pair<int, int> R;

void yakubun(R &a) {
  int d = gcd(a.first, a.second);
  a.first /= d;
  a.second /= d;
}

bool cmp(R a, R b) {
  double va = (double)a.first / (double)a.second;
  double vb = (double)b.first / (double)b.second;
  return va <= vb;
}

R sub(R a, R b) {
  int m = lcm(a.second, b.second);
  int t = a.first * (m / a.second) - b.first * (m / b.second);
  R r = make_pair(t, m);
  yakubun(r);
  return r;
}
int calc = 0;
int A;
int f(R mx, R cur, int a, int n) {
  int ret = 0;
  if (cur.first == 0)
    return 1;
  if (n == 0)
    return ret;
  for (int k = 1; k <= A / a; k++) {
    calc++;
    R r = make_pair(1, k);
    if (cmp(r, mx) && cmp(r, cur)) {
      ret += f(r, sub(cur, r), a * k, n - 1);
    }
  }
  return ret;
}

int main() {
  while (true) {
    int p, q, n;
    cin >> p >> q >> A >> n;
    if (p == 0 && q == 0 && A == 0 && n == 0)
      return 0;
    calc = 0;
    cout << f(make_pair(p, q), make_pair(p, q), 1, n) << endl;
    // cerr << "calc=" << calc << endl;
  }
}
Problem p00712 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <stdio.h>
#include <string.h>

using namespace std;
typedef long long ll;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define DEC(i, s) for (int i = (s); i >= 0; i--)

#define SIZE(v) (int)((v).size())
#define MEMSET(v, h) memset((v), h, sizeof(v))
#define FIND(m, w) ((m).find(w) != (m).end())

ll p, q, a, n;

ll calc(ll depth, ll prev, ll s, ll sum) {
  if (s * p == q * sum) {
    return 1;
  }
  if (depth == n) {
    return 0;
  }
  if (s * p < q * sum) {
    return 0;
  }
  ll ret = 0;
  FOREQ(i, prev, a) {
    ll ns = i * s;
    if (ns > a) {
      break;
    }
    ll nsum = sum * ns / s + ns / i;
    if (ns * p > q * (nsum + (n - depth - 1) * ns / i)) {
      break;
    }
    ret += calc(depth + 1, i, ns, nsum);
  }
  return ret;
}

int main() {
  while (scanf("%lld %lld %lld %lld", &p, &q, &a, &n), p | q | a | n) {
    printf("%lld\\n", calc(0, 1, 1, 0));
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>

using namespace std;
typedef long long ll;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define DEC(i, s) for (int i = (s); i >= 0; i--)

#define SIZE(v) (int)((v).size())
#define MEMSET(v, h) memset((v), h, sizeof(v))
#define FIND(m, w) ((m).find(w) != (m).end())

ll p, q, a, n;

ll calc(ll depth, ll prev, ll s, ll sum) {
  if (s * p == q * sum) {
    return 1;
  }
  if (depth == n) {
    return 0;
  }
  if (s * p < q * sum) {
    return 0;
  }
  ll ret = 0;
  FOREQ(i, prev, a) {
    ll ns = i * s;
    if (ns > a) {
      break;
    }
    ll nsum = sum * ns / s + ns / i;
    ret += calc(depth + 1, i, ns, nsum);
  }
  return ret;
}

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

#include <iostream>

using namespace std;

int serch(int p, int q, int a, int low, int n) {
  int i, count, high;
  count = 0;
  if (p == 0)
    return 1;
  if (n == 0)
    return 0;
  if (low < q / p)
    low = q / p;
  if (a > q * n / p)
    high = q * n / p;
  else
    high = a;
  for (i = low; i <= high; i++) {
    count += serch(p * i - q, q * i, a / i, i, n - 1);
  }
  return count;
}

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

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int serch(int p, int q, int a, int low, int n) {
  int i, count;
  count = 0;
  if (p == 0)
    return 1;
  if (n == 0)
    return 0;
  if (low < p / q + 1)
    low = p / q + 1;
  for (i = low; i <= a; i++) {
    count += serch(p * i - q, q * i, a / i, i, n - 1);
  }
  return count;
}

int main() {
  int p, q, a, n;

  while (1) {
    scanf("%d%d%d%d", &p, &q, &a, &n);
    if (!(p | q | a | n))
      break;
    cout << serch(p, q, a, 1, n) << endl;
  }
  return 0;
}
Problem p00712 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(a) (a).begin(), (a).end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
double EPS = 1e-10;
int p, q, a, n;
int rec(int x, int y, int cnt, int num) {
  if (cnt > n)
    return 0;
  if (x * p == y * q)
    return 1;
  if (x * p < y * q)
    return 0;
  int res = 0;
  for (int i = num; i * x <= a; i++) {
    res += rec(x * i, y * i + x, cnt + 1, i);
  }
  return res;
}
int main() {
  while (cin >> p >> q >> a >> n && (p || q || a || n)) {
    cout << rec(1, 0, 0, 1) << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(a) (a).begin(), (a).end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
double EPS = 1e-10;
int A, N;
int cnt;
int gcd(int a, int b) {
  if (a < b)
    swap(a, b);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }
P bet(int q, int p, int x, int y) {
  int mom = lcm(p, y);
  int chl = q * (mom / p) - x * (mom / y);
  if (chl < 0) {
    return MP(-1, -1);
  }
  int k = gcd(mom, chl);
  return MP(chl / k, mom / k);
}
void rec(int p, int q, int num, int mx, int mlt) {
  if (num >= N || mlt > A)
    return;
  if (p == 1 && q >= mx && mlt * q <= A) {
    cnt++;
  }
  for (int i = mx; mlt * i <= A; i++) {
    P pp = bet(p, q, 1, i);
    if (pp.first == -1)
      continue;
    rec(pp.first, pp.second, num + 1, i, mlt * i);
  }
  return;
}
int main() {
  int p, q;
  while (cin >> p >> q >> A >> N && (p || q || A || N)) {
    cnt = 0;
    int mm = gcd(p, q);
    p /= mm;
    q /= mm;
    rec(p, q, 0, 1, 1);
    cout << cnt << endl;
  }
}
Problem p00712 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
typedef int ll;
typedef unsigned long long ull;
using namespace std;
#define pb push_back
int dy[] = {0, 0, 1, -1, 1, 1, -1, -1};
int dx[] = {1, -1, 0, 0, 1, -1, -1, 1};

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define mp make_pair
#define fi first
#define sc second
ll p, q, a, n;

typedef ll GCD_TYPE;
GCD_TYPE gcd(GCD_TYPE a, GCD_TYPE b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll dfs(ll p, ll q, ll nowa, ll nown, ll j) {
  if (p <= 0) {
    return p == 0;
  }
  if (nown >= n) {
    return 0;
  }
  if (q * (n - nown) < p * j) {
    return 0;
  }
  ll g = gcd(p, q);
  p /= g;
  q /= g;

  ll ret = 0;
  for (ll i = j; i * nowa <= a; i++) {
    ll l = q * i / gcd(q, i);

    ret += dfs((p * l / q) - (l / i), l, nowa * i, nown + 1, i);
  }
  return ret;
}

int main() {

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

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
#define pb push_back
int dy[] = {0, 0, 1, -1, 1, 1, -1, -1};
int dx[] = {1, -1, 0, 0, 1, -1, -1, 1};

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define mp make_pair
#define fi first
#define sc second
ll p, q, a, n;

typedef ll GCD_TYPE;
GCD_TYPE gcd(GCD_TYPE a, GCD_TYPE b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll dfs(ll p, ll q, ll nowa, ll nown, ll j) {
  if (p <= 0) {
    return p == 0;
  }
  if (nown >= n) {
    return 0;
  }
  if (q * (n - nown) < p * j) {
    return 0;
  }
  ll g = gcd(p, q);
  p /= g;
  q /= g;

  ll ret = 0;
  for (ll i = j; i * nowa <= a; i++) {
    ll l = q * i / gcd(q, i);

    ret += dfs((p * l / q) - (l / i), l, nowa * i, nown + 1, i);
  }
  return ret;
}

int main() {

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

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

#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

// #define int long long
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

int gcd(int x, int y) { return y ? gcd(y, x % y) : x; }
int lcm(int x, int y) { return x * y / gcd(x, y); }

int p, q, a, n;
using T = tuple<int, int, int, int, int>;
int f(int x, int y, int z, int c, int a_) {
  if (c == n - 1 && x != 1)
    return 0;
  if (c == n)
    return 0;
  if (x * z > (n - c) * y)
    return 0;
  int ret = 0;
  rep(i, z, a + 1) {
    if (i * a_ > a)
      break;
    if (x * i > y) {
      int nx = x * i - y, ny = y * i;
      int g = gcd(nx, ny);
      nx /= g;
      ny /= g;
      ret += f(nx, ny, i, c + 1, i * a_);
    } else if (x * i == y) {
      ret += 1;
    }
  }
  return ret;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  for (; cin >> p >> q >> a >> n && p;) {
    cout << f(p, q, 1, 0, 1) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

// #define int long long
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

int gcd(int x, int y) { return y ? gcd(y, x % y) : x; }
int lcm(int x, int y) { return x * y / gcd(x, y); }

int p, q, a, n;
using T = tuple<int, int, int, int, int>;
int f(int x, int y, int z, int c, int a_) {
  if (c == n)
    return 0;
  if (x * z > (n - c) * y)
    return 0;
  int ret = 0;
  rep(i, z, a + 1) {
    if (i * a_ > a)
      break;
    if (x * i > y) {
      int nx = x * i - y, ny = y * i;
      int g = gcd(nx, ny);
      nx /= g;
      ny /= g;
      ret += f(nx, ny, i, c + 1, i * a_);
    } else if (x * i == y) {
      ret += 1;
    }
  }
  return ret;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  for (; cin >> p >> q >> a >> n && p;) {
    cout << f(p, q, 1, 0, 1) << endl;
  }
  return 0;
}
Problem p00712 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>

using namespace std;

int calc(int p, int q, int mind, int ca, int a, int cn, int n) {
  int s = 0;
  if (cn <= n) {
    mind = max(mind, q / p);
    int maxd = floor((double)(n - cn + 1) * q / p);
    for (int i = mind; i <= maxd && pow(i, floor((double)i * p / q)) * ca <= a;
         i++) {
      if (p * i - q == 0) {
        s++;
      } else if (p * i - q > 0) {
        int _p = p * i - q;
        int _q = q * i;
        s += calc(_p, _q, i, ca * i, a, cn + 1, n);
      }
    }
  }
  return s;
}

int main() {
  int p, q, a, n;
  cin >> p >> q >> a >> n;
  while (p) {
    for (int i = 2; i <= min(p, q); i++) {
      while (p % i + q % i == 0) {
        p /= i;
        q /= i;
      }
    }
    cout << calc(p, q, 1, 1, a, 1, n) << endl;
    cin >> p >> q >> a >> n;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>

using namespace std;

int calc(int p, int q, int mind, int ca, int a, int cn, int n) {
  int s = 0;
  if (cn <= n) {
    mind = max(mind, q / p);
    int maxd = floor((double)(n - cn + 1) * q / p);
    for (int i = mind; i <= maxd && pow(i, floor((double)i * p / q)) * ca <= a;
         i++) {
      if (p * i - q == 0) {
        s++;
      } else if (p * i - q > 0) {
        int _p = p * i - q;
        int _q = q * i;
        for (int j = 2; j <= min(_p, _q); j++) {
          while (_p % j + _q % j == 0) {
            _p /= j;
            _q /= j;
          }
        }
        s += calc(_p, _q, i, ca * i, a, cn + 1, n);
      }
    }
  }
  return s;
}

int main() {
  int p, q, a, n;
  cin >> p >> q >> a >> n;
  while (p) {
    for (int i = 2; i <= min(p, q); i++) {
      while (p % i + q % i == 0) {
        p /= i;
        q /= i;
      }
    }
    cout << calc(p, q, 1, 1, a, 1, n) << endl;
    cin >> p >> q >> a >> n;
  }
}
Problem p00712 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <cstdio>

using namespace std;

const double EPS = 1e-9;

int a, n;
double p, q;

int dfs(int i, int cnt, double sum, int prod) {
  if (abs(sum - p / q) < EPS)
    return 1;
  if (sum + (n - cnt) * 1.0 / i + EPS < p / q || p / q < sum - EPS ||
      cnt + 1 > n || prod * i > a)
    return 0;
  return dfs(i, cnt + 1, sum + 1.0 / i, prod * i) + dfs(i + 1, cnt, sum, prod);
}

int main() {
  for (; scanf("%lf%lf%d%d", &p, &q, &a, &n), p;)
    printf("%d\\n", dfs(1, 0, 0, 1));
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstdio>

using namespace std;

const double EPS = 1e-9;

int p, q, a, n;

int dfs(int i, int cnt, double sum, int prod) {
  if (abs(sum - (double)p / q) < EPS)
    return 1;
  if (sum > (double)p / q + EPS || cnt + 1 > n || prod * i > a)
    return 0;
  return dfs(i, cnt + 1, sum + 1.0 / i, prod * i) + dfs(i + 1, cnt, sum, prod);
}

int main() {
  for (; scanf("%d%d%d%d", &p, &q, &a, &n), p;)
    printf("%d\\n", dfs(1, 0, 0, 1));
  return 0;
}