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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
using namespace std;
const int INF = 1e9;
const double eps = 1e-9;
typedef long long ll;

double p, q, m, a;
ll sum;
void dfs(ll anow, int num, double now, int before) {
  // cout << anow << "  " << num << "  " << now << "  " << before << endl;
  // cout << fabs(p/q-now) << endl;
  if (num <= m && fabs(p / q - now) < eps && anow <= a) {
    sum++;
    return;
  }
  if (a < anow || num > m || now > p / q)
    return;

  num++;

  for (int i = before; i < a; i++) {
    if (anow * i > a) {
      break;
    }
    dfs(anow * i, num, now + (double)1 / i, i);
  }
}

int main() {
  vector<int> g;
  while (cin >> p >> q >> a >> m, p + q + a + m) {
    sum = 0;
    dfs(1, 0, 0, 1);
    g.push_back(sum);
    // cout << sum << endl;
  }
  REP(i, g.size()) { cout << g[i] << endl; }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
using namespace std;
const int INF = 1e9;
const double eps = 1e-9;
typedef long long ll;

double p, q, m, a;
ll sum;
void dfs(ll anow, int num, double now, int before) {
  // cout << anow << "  " << num << "  " << now << "  " << before << endl;
  // cout << fabs(p/q-now) << endl;
  if (num <= m && fabs(p / q - now) < eps && anow <= a) {
    sum++;
    return;
  }
  if (a < anow || num > m || now > p / q)
    return;

  num++;

  for (int i = before; i < 1000; i++) {
    dfs(anow * i, num, now + (double)1 / i, i);
  }
}

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

#include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

typedef long double ld;

const ld EPS = 1e-9;

ld p, q;
int a, n;
ld t;

int dfs(int m, ld s, int w, int c) {

  if (abs(s - t) < EPS) {
    return 1;
  }
  if (m == n)
    return 0;

  int ret = 0;
  for (int i = min(a / w, c); i >= 1; i--) {
    if (w * i <= a) {
      ld j = s + (ld(1.0) / ld(i));
      if (j <= t + EPS) {
        ret += dfs(m + 1, j, w * i, i);
      }
    }
  }
  return ret;
}

int main() {

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

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

typedef long double ld;

const ld EPS = 1e-9;

ld p, q;
int a, n;
ld t;

int dfs(int m, ld s, int w, int c) {

  if (abs(s - t) < EPS) {
    return 1;
  }
  if (m == n)
    return 0;

  int ret = 0;
  for (int i = c; i >= 1; i--) {
    if (w * i <= a) {
      ld j = s + (ld(1.0) / ld(i));
      if (j <= t + EPS) {
        ret += dfs(m + 1, j, w * i, i);
      }
    }
  }
  return ret;
}

int main() {

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

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

#include <iostream>

using namespace std;

#define EPS (1e-8)
#define EQ(a, b) ((a)-EPS < (b) && (b) < (a) + EPS)

int p, q, a;
double lhs; // left hand side

int solve(int n, int d, int prod, double sum) {
  int cnt = 0;
  for (int i = d; prod * i <= a; i++) {
    double rhs = sum + 1.0 / i; // right hand side
    if (lhs < rhs - EPS) {
      continue;
    }
    if (EQ(lhs, rhs)) {
      cnt++;
      continue;
    }
    if (n == 1) {
      continue;
    }
    cnt += solve(n - 1, i, prod * i, rhs);
  }

  return cnt;
}

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

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int P, Q, MS, C;

bool f(int shi, int bo, int start, int dep, int check) {

  if (check > MS) {
    return false;
  }
  if (shi * Q == bo * P) {
    C++;
    // return true;
  }
  if (shi * Q > bo * P) {
    return false;
  }
  if (dep == 0) {
    return false;
  }
  for (int i = start; i <= 2000; i++) {

    f(i * shi + bo, bo * i, i, dep - 1, check * i);
  }
  return true;
}

int main() {

  int n;
  while (cin >> P >> Q >> MS >> n) {

    if (P == 0 && Q == 0 && MS == 0 && n == 0)
      break;

    C = 0;
    f(0, 1, 1, n, 1);

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

#include <algorithm>
#include <cstdio>
using namespace std;

int p, q, a, n;

int solve(int num, int den, int cnt, int pro, int e) {
  if (cnt > n || p * den < num * q)
    return 0;
  else if (p * den == num * q)
    return 1;

  int res = 0;
  for (int i = e; i <= a; ++i) {
    if (pro * i > a)
      break;

    int x = num * i + den;
    int y = den * i;
    /*
    int gcd = __gcd(x, y);
    x /= gcd;
    y /= gcd;
    */

    res += solve(x, y, cnt + 1, pro * i, i);
  }
  return res;
}

int main() {
  while (~scanf("%d %d %d %d", &p, &q, &a, &n)) {
    if ((p | q | a | n) == 0)
      break;
    printf("%d\\n", solve(0, 1, 0, 1, 1));
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

int p, q, a, n;

int solve(int num, int den, int cnt, int pro, int e) {
  if (cnt > n || p * den < num * q)
    return 0;
  else if (p * den == num * q)
    return 1;

  int res = 0;
  for (int i = e; i <= a; ++i) {
    if (pro * i > a)
      break;

    int x = num * i + den;
    int y = den * i;
    int gcd = __gcd(x, y);
    x /= gcd;
    y /= gcd;

    res += solve(x, y, cnt + 1, pro * i, i);
  }
  return res;
}

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

#include <algorithm>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const ll MOD = 998244353;
const ll INF = (ll)1000000007 * 1000000007;
const double EPS = 1e-9;
typedef pair<int, int> P;
typedef unsigned int ui;
#define stop                                                                   \\
  char nyaa;                                                                   \\
  cin >> nyaa;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-11;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
int p, q, a, n;
int nx, ny;
int dfs(int f, int c, int s, int x, int y) {
  int res = 0;
  if (x * q == y * p)
    return 1;
  if (x * q > y * p || c == n)
    return 0;
  for (int i = f; i <= a / s; i++) {
    nx = i * x + y;
    ny = i * y;
    res += dfs(i, c + 1, s * i, nx, ny);
  }
  return res;
}
int main() {
  while (cin >> p >> q >> a >> n, p) {
    int out = 0;
    for (int i = 1; i <= a; i++) {
      out += dfs(i, 1, i, 1, i);
    }
    cout << out << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const ll MOD = 998244353;
const ll INF = (ll)1000000007 * 1000000007;
const double EPS = 1e-9;
typedef pair<int, int> P;
typedef unsigned int ui;
#define stop                                                                   \\
  char nyaa;                                                                   \\
  cin >> nyaa;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-11;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
int p, q, a, n;
int nx, ny;
int dfs(int f, int c, int s, int x, int y) {
  int res = 0;
  if (x * q == y * p)
    return 1;
  if (c == n)
    return 0;
  for (int i = f; i <= a / s; i++) {
    nx = i * x + y;
    ny = i * y;
    res += dfs(i, c + 1, s * i, nx, ny);
  }
  return res;
}
int main() {
  while (cin >> p >> q >> a >> n, p) {
    int out = 0;
    for (int i = 1; i <= (int)sqrt(a); i++) {
      out += dfs(i, 1, i, 1, i);
    }
    cout << out << endl;
  }
  return 0;
}
Problem p00712 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>

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

int p, q, a, n, cnt;

void dfs(int v, int l, int r, int now) {
  double dsum = (double)l / r;
  dsum += (double)(n - v + 1) / now;
  if ((double)p / q > dsum)
    return;
  if (v != 0) {
    int d = gcd(r, l);
    if (l / d == p && r / d == q) {
      cnt++;
      return;
    }
  }
  if (v < n) {
    for (int i = a / r; i >= now; i--) {
      if ((double)p / q < (double)(l * i + r) / (r * i))
        return;
      dfs(v + 1, l * i + r, r * i, i);
    }
  }
}

int main(void) {
  while (1) {
    scanf("%d %d %d %d", &p, &q, &a, &n);
    if (p + q + a + n == 0)
      break;
    int d = gcd(p, q);
    p /= d;
    q /= d;
    cnt = 0;
    dfs(0, 0, 1, 1);
    printf("%d\\n", cnt);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>

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

int p, q, a, n, cnt;

void dfs(int v, int l, int r, int now) {
  if (v != 0) {
    int d = gcd(r, l);
    if (l / d == p && r / d == q) {
      cnt++;
      return;
    }
  }
  if (v < n) {
    for (int i = a / r; i >= now; i--) {
      if ((double)p / q < (double)(l * i + r) / (r * i))
        return;
      dfs(v + 1, l * i + r, r * i, i);
    }
  }
}

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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
int a, n, tp, tq;

void rec(int p, int q, int an, int &cnt, int k, int total) {
  if (total > a)
    return;
  if (an > n)
    return;
  if (p * tq > tp * q)
    return;
  if (((n - an) * q + k * p) * tq < tp * k * q)
    return;
  if (p == tp && q == tq) {
    cnt++;
    return;
  }

  for (int i = k; q * i <= a; i++) {
    int A = p * i + q, B = q * i;
    if (total * i > a)
      continue;
    int gcd1 = __gcd(A, B);
    A /= gcd1;
    B /= gcd1;
    rec(A, B, an + 1, cnt, i, total * i);
  }
}

int main() {

  while (1) {
    scanf("%d %d %d %d", &tp, &tq, &a, &n);
    if (!(tp | tq | a | n))
      break;
    int gcd1 = __gcd(tp, tq);
    tp /= gcd1;
    tq /= gcd1;
    int theNumber = 0;
    for (int i = 1; i <= a; i++) {
      if (i > a)
        continue;
      rec(1, i, 1, theNumber, i, i);
    }
    printf("%d\\n", theNumber);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
int a, n, tp, tq;

void rec(int p, int q, int an, int &cnt, int k, int total) {
  if (total <= 0)
    return;
  if (total > a)
    return;
  if (an > n)
    return;
  if (p * tq > tp * q)
    return;
  if (p == tp && q == tq) {
    cnt++;
    return;
  }

  for (int i = k; q * i <= a; i++) {
    int A = p * i + q, B = q * i;
    if (total * i > a)
      continue;
    int gcd1 = __gcd(A, B);
    A /= gcd1;
    B /= gcd1;
    rec(A, B, an + 1, cnt, i, total * i);
  }
}

int main() {

  while (1) {
    scanf("%d %d %d %d", &tp, &tq, &a, &n);
    if (!(tp | tq | a | n))
      break;
    int gcd1 = __gcd(tp, tq);
    tp /= gcd1;
    tq /= gcd1;
    int theNumber = 0;
    for (int i = 1; i <= a; i++) {
      if (i > a)
        continue;
      rec(1, i, 1, theNumber, i, i);
    }
    printf("%d\\n", theNumber);
  }
  return 0;
}
Problem p00712 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

int p, q, a, n, answer;

void search(int pp, int qq, int start, int count) {
  if (count == n)
    return;
  for (int i = start; i <= a; ++i) {
    int ppp = pp * i + qq;
    int qqq = qq * i;
    if (qqq > a)
      return;
    if (ppp / qqq > p / q)
      continue;
    if (!(qqq % q) && !(ppp % p) && qqq / q == ppp / p)
      ++answer;
    else
      search(ppp, qqq, i, count + 1);
  }
  return;
}

int gcd(int a, int b) {
  int c;
  while (b) {
    c = a % b;
    a = b;
    b = c;
  }
  return a;
}

int main(void) {
  while (cin >> p >> q >> a >> n, p | q | a | n) {
    int gg = gcd(p, q);
    p /= gg;
    q /= gg;
    answer = 0;
    search(0, 1, 1, 0);
    cout << answer << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int p, q, a, n, answer;

void search(int pp, int qq, int start, int count) {
  if (count == n)
    return;
  for (int i = start; i <= a; ++i) {
    int ppp = pp * i + qq;
    int qqq = qq * i;
    if (qqq > a)
      return;
    if (!(qqq % q) && !(ppp % p) && qqq / q == ppp / p)
      ++answer;
    else
      search(ppp, qqq, i, count + 1);
  }
  return;
}

int gcd(int a, int b) {
  int c;
  while (b) {
    c = a % b;
    a = b;
    b = c;
  }
  return a;
}

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

#include <algorithm>
#include <cmath>
#include <complex>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

using namespace std;

typedef pair<int, int> Pi;

int p, q, a, n;

int rec(int bunbo, int bunsi, int cnt,
        int idx) { // rec(分母, 分子, 個数, 今のidx)
  if (cnt > n)
    return 0;
  if (bunbo * p == bunsi * q)
    return 1;
  if (bunbo * p < bunsi * q)
    return 0;
  int ret = 0;
  for (int i = idx; i * bunbo <= a; i++) {
    ret += rec(bunbo * i, bunsi * i + bunbo, cnt + 1, i);
  }
  return ret;
}

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

using namespace std;

typedef pair<int, int> Pi;

int p, q, a, n;
map<pair<Pi, Pi>, int> dp;

int rec(int bunbo, int bunsi, int cnt,
        int idx) { // rec(分母, 分子, 個数, 今のidx)
  if (cnt > n)
    return 0;
  if (dp.find(make_pair(Pi(bunbo, bunsi), Pi(cnt, idx))) != dp.end())
    return dp[make_pair(Pi(bunbo, bunsi), Pi(cnt, idx))];
  if (bunbo * p == bunsi * q)
    return 1;
  if (bunbo * p < bunsi * q)
    return 0;
  int ret = 0;
  for (int i = idx; i * bunbo <= a; i++) {
    ret += rec(bunbo * i, bunsi * i + bunbo, cnt + 1, i);
  }
  return dp[make_pair(Pi(bunbo, bunsi), Pi(cnt, idx))] = ret;
}

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

#include <bits/stdc++.h>
#include <cxxabi.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
#define rep(i, n) range(i, 0, n)
#define range(i, a, n) for (int i = a; i < n; i++)
#define all(a) a.begin(), a.end()
#define LINF ((ll)1ll < 60)
#define INF ((int)1 << 30)
#define EPS (1e-9)
#define MOD (1000000007)
template <class S, class T> ostream &operator<<(ostream &os, pair<S, T> p) {
  os << "[" << p.first << ", " << p.second << "]";
  return os;
};
template <class S> auto &operator<<(ostream &os, vector<S> t) {
  bool b = 1;
  for (auto s : t)
    os << (exchange(b, 0) ||
                   strlen(abi::__cxa_demangle(typeid(S).name(), 0, 0, 0)) > 20
               ? ""
               : " ")
       << s;
  return os << endl;
}

constexpr int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
constexpr int lcm(int a, int b) { return a * b / gcd(a, b); }

int ma;

int solve(int p, int q, int r, int a, int n) {
  int ans = 0;
  if (p == 0)
    return 1;
  if (n == 0)
    return 0;
  for (int nq = r; a * nq <= ma; nq++) {
    if (p * nq > n * q)
      break;
    if (p * nq >= q) {
      int P = p * nq - q;
      int Q = q * nq;
      ans += solve(P / gcd(P, Q), Q / gcd(P, Q), nq, a * nq, n - 1);
    }
  }
  return ans;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int p, q, n;
  while (cin >> p >> q >> ma >> n, p) {
    cout << solve(p, q, 1, 1, n) << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <cxxabi.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
#define rep(i, n) range(i, 0, n)
#define range(i, a, n) for (int i = a; i < n; i++)
#define all(a) a.begin(), a.end()
#define LINF ((ll)1ll < 60)
#define INF ((int)1 << 30)
#define EPS (1e-9)
#define MOD (1000000007)
template <class S, class T> ostream &operator<<(ostream &os, pair<S, T> p) {
  os << "[" << p.first << ", " << p.second << "]";
  return os;
};
template <class S> auto &operator<<(ostream &os, vector<S> t) {
  bool b = 1;
  for (auto s : t)
    os << (exchange(b, 0) ||
                   strlen(abi::__cxa_demangle(typeid(S).name(), 0, 0, 0)) > 20
               ? ""
               : " ")
       << s;
  return os << endl;
}

constexpr int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
constexpr int lcm(int a, int b) { return a * b / gcd(a, b); }

int ma;

int solve(int p, int q, int r, int a, int n) {
  int ans = 0;
  if (p == 0)
    return 1;
  if (n == 0)
    return 0;
  for (int nq = r; a * nq <= ma; nq++) {
    if (p * nq > n * q)
      break;
    // if(p*nq>=q){
    int P = p * nq - q;
    int Q = q * nq;
    ans += solve(P / gcd(P, Q), Q / gcd(P, Q), nq, a * nq, n - 1);
    //}
  }
  return ans;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

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

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

typedef long long ll;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define each(itr, c)                                                           \\
  for (__typeof(c.begin()) itr = c.begin(); itr != c.end(); ++itr)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second

ll p, q, a;
int n;

inline bool same(ll num, ll den) { return num * q == den * p; }

ll dfs(ll num, ll den, int dep, ll b) {
  if (same(num, den))
    return 1;

  if (dep == n)
    return 0;

  // printf(" now: %lld %lld %d %lld\\n", num,den,dep,b);
  ll x = q * den, y = p * den - q * num;
  // printf("x=%lld y=%lld\\n", x,y);

  ll l = x / y;
  if (x % y != 0)
    ++l;
  l = max(l, b);
  ll r = a / den;

  ll ret = 0;
  for (ll i = l; i <= r; ++i)
    ret += dfs(num * i + den, i * den, dep + 1, i);
  return ret;
}

int main() {
  while (cin >> p >> q >> a >> n, p)
    printf("%lld\\n", dfs(0, 1, 0, 1));
  return 0;
}

Error: TLE

Fixed submission:

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

typedef long long ll;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define each(itr, c)                                                           \\
  for (__typeof(c.begin()) itr = c.begin(); itr != c.end(); ++itr)
#define all(x) (x).begin(), (x).end()
#define mp make_pair
#define pb push_back
#define fi first
#define se second

typedef pair<ll, ll> frac;
typedef pair<frac, ll> pl;
typedef pair<ll, int> pi;
typedef pair<pl, pi> P;

ll ab(ll x) { return (x > 0 ? x : -x); }

ll lcm(ll x, ll y) { return x / __gcd(x, y) * y; }

frac add(frac p, frac q) {
  //??????
  ll nd = lcm(p.se, q.se);
  ll nn = p.fi * (nd / p.se) + q.fi * (nd / q.se);

  ll g = __gcd(ab(nn), nd);
  nd /= g;
  nn /= g;

  return frac(nn, nd);
}

frac sub(frac p, frac q) {
  q.fi = -q.fi;
  return add(p, q);
}

ll p, q, a;
int n;

map<P, ll> dp;

//??????????????°,?????¨??????,1????????????,??±???(?????°)
ll dfs(frac rem, ll mul, ll b, int dep) {
  // P now=P(pl(rem,mul),pi(b,dep));
  // if(dp.find(now)!=dp.end()) return dp[now];

  if (dep == n)
    return 0;

  // printf(" now= %lld/%lld, mul,b,dep= %lld,%lld,%d\\n",rem.n,rem.d,mul,b,dep);

  ll ret = 0;

  // 1????????\\??????????????????
  for (ll i = b; i <= a; ++i) {
    if (mul * i > a)
      break;

    frac nx = sub(rem, frac{1, i});

    if (nx.fi < 0)
      continue;
    else if (nx.fi == 0) {
      // printf(" now= %lld/%lld, mul,b,dep=
      // %lld,%lld,%d\\n",rem.n,rem.d,mul,b,dep);
      ret += 1;
    } else
      ret += dfs(nx, mul * i, i, dep + 1);
  }

  return ret;
  // return dp[now]=ret;
}

int main() {
  while (cin >> p >> q >> a >> n, p) {
    ll g = __gcd(p, q);
    p /= g;
    q /= g;

    dp.clear();
    cout << dfs(frac{p, q}, 1, 1, 0) << endl;
  }
  return 0;
}
Problem p00712 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl;
int p, q, a, n, ans;
double eps = 1e-12;
void dfs(int pro, int cnt, double sum, int mx) {
  if (abs(sum - (double)p / q) < eps)
    ans++;
  if (sum + eps > (double)p / q)
    return;
  if (cnt == n)
    return;
  rep1(i, mx) {
    if (i * pro > a)
      break;
    dfs(i * pro, cnt + 1, sum + 1.0 / i, min(mx, i));
  }
}
int main() {
  while (true) {
    cin >> p >> q >> a >> n;
    if (p == 0)
      break;
    ans = 0;
    dfs(1, 0, 0, a);
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl;
int p, q, a, n, ans;
double eps = 1e-12;
void dfs(int pro, int cnt, double sum, int mx) {
  if (abs(sum - (double)p / q) < eps)
    ans++;
  if (cnt == n)
    return;
  rep1(i, mx) {
    if (i * pro > a)
      break;
    dfs(i * pro, cnt + 1, sum + 1.0 / i, min(mx, i));
  }
}
int main() {
  while (true) {
    cin >> p >> q >> a >> n;
    if (p == 0)
      break;
    ans = 0;
    dfs(1, 0, 0, a);
    cout << ans << endl;
  }
}