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

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

template <class T> void readVector(int n, vector<T> &v) {
  for (int i = 0; i < n; ++i) {
    T t;
    cin >> t;
    v.push_back(t);
  }
}
typedef pair<long long, long long> P;
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};

void solve(int w, int h) {
  vector<string> v;
  readVector(h, v);
  int x = 0, y = 0, ans = 0;
  for (int i = 0; i < h; ++i)
    for (int j = 0; j < w; ++j)
      if (v[i][j] == '@')
        x = i, y = j;
  queue<P> q;
  q.push(P(x, y));
  v[x][y] = ' ';
  ++ans;
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    int nx = p.first;
    int ny = p.second;
    for (int i = 0; i < 4; ++i) {
      int a = nx + dx[i];
      int b = ny + dy[i];
      if (0 <= a && 0 <= b && a < h && b < w && v[a][b] == '.') {
        q.push(P(a, b));
        v[a][b] = ' ';
        ++ans;
      }
    }
  }
  cout << ans << "\\n";
}

int main() {
  //	cin.tie(0);
  //	ios::sync_with_stdio(false);
  while (1) {
    int w, h;
    cin >> w >> h;
    if (w == 0 && h == 0)
      return 0;
    solve(w, h);
  }
}


Error: TLE

Fixed submission:

#include <cstdio>
using namespace std;

int n, m;
char field[100][100];

int dfs(int x, int y) {
  field[x][y] = '.';

  for (int dx = -1; dx <= 1; dx++) {
    for (int dy = -1; dy <= 1; dy++) {
      int nx = x + dx, ny = y + dy;

      if (0 <= nx && nx < n && 0 <= ny && ny < m && field[nx][ny] == 'W')
        dfs(nx, ny);
    }
  }
  return 0;
}

int main() {
  while (1) {
    scanf("%d %d", &n, &m);
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < 100; i++) {
      for (int j = 0; j < 100; j++) {
        field[i][j] = '.';
      }
    }
    for (int i = 0; i < n; i++) {
      scanf("%s", field[i]);
    }
    int ans = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (field[i][j] == 'W') {
          dfs(i, j);
          ans++;
        }
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p00711 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
bool b[21][21];
int w, h, count = 0;

void dfs(int x, int y) {
  b[y][x] = 1;
  count++;
  int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
  int nx, ny;
  for (int i = 0; i < 4; i++) {
    nx = dx[i] + x;
    ny = dy[i] + y;
    if (0 <= nx && nx < w && 0 <= ny && ny < h)
      if (b[ny][nx] == 0)
        dfs(nx, ny);
  }
}

int main() {
  char a;
  int sx, sy;
  while (1) {
    cin >> w >> h;
    count = 0;
    if (w + h == 0)
      break;
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) {
        cin >> a;
        b[i][j] = 0;
        if (a == '#')
          b[i][j] = 1;
        if (a == '@') {
          sx = j;
          sy = i;
        }
      }
    dfs(sx, sy);
    cout << count << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
bool b[21][21];
int w, h, count = 0;

void dfs(int x, int y) {
  b[y][x] = 1;
  count++;
  int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
  int nx, ny;
  for (int i = 0; i < 4; i++) {
    nx = dx[i] + x;
    ny = dy[i] + y;
    if (0 <= nx && nx < w && 0 <= ny && ny < h)
      if (b[ny][nx] == 0)
        dfs(nx, ny);
  }
}

int main() {
  char a;
  int sx, sy;
  while (1) {
    cin >> w >> h;
    count = 0;
    b[21][21] = {};
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) {
        cin >> a;
        if (a == '#')
          b[i][j] = 1;
        if (a == '@') {
          sx = j;
          sy = i;
        }
      }
    dfs(sx, sy);
    cout << count << endl;
  }
  return 0;
}
Problem p00712 (C++) — buggy submission, verdict: Time Limit Exceeded

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

struct Frac {
  int num, den;
  inline Frac(int _num, int _den) {
    int gcd = __gcd(_num, _den);
    num = _num / gcd;
    den = _den / gcd;
  }
  inline Frac operator+(const Frac &o) const {
    return Frac(num * o.den + o.num * den, den * o.den);
  }
  inline Frac operator-(const Frac &o) const {
    return Frac(num * o.den - o.num * den, den * o.den);
  }
  inline Frac operator*(int n) const { return Frac(num * n, den); }
  inline bool operator<(const Frac &o) const {
    return num * o.den < o.num * den;
  }
  inline bool operator<(int n) const { return num < den * n; }
};

inline int count_pat(int min_den, Frac f, int a, int n) {
  if (f.num == 0)
    return 1;
  if (f < 0)
    return 0;
  if (min_den > a || a == 1)
    return 0;
  if (n == 0)
    return 0;
  if (Frac(1, min_den) * n < f)
    return 0;
  int sum = 0;
  for (int i = min_den; i <= a; i++) {
    if (Frac(1, i) * n < f)
      break;
    sum += count_pat(i, f - Frac(1, i), a / i, n - 1);
  }
  return sum;
}

int main() {
  int p, q, a, n;
  while (cin >> p >> q >> a >> n) {
    if (!(p || q || a || n))
      break;
    cout << count_pat(1, Frac(p, q), a, n) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

struct Frac {
  int num, den;
  inline Frac(int _num, int _den) {
    int gcd = __gcd(_num, _den);
    num = _num / gcd;
    den = _den / gcd;
  }
  inline Frac operator+(const Frac &o) const {
    return Frac(num * o.den + o.num * den, den * o.den);
  }
  inline Frac operator-(const Frac &o) const {
    return Frac(num * o.den - o.num * den, den * o.den);
  }
  inline Frac operator*(int n) const { return Frac(num * n, den); }
  inline bool operator<(const Frac &o) const {
    return num * o.den < o.num * den;
  }
  inline bool operator<(int n) const { return num < den * n; }
};

inline int count_pat(int min_den, Frac f, int a, int n) {
  if (f.num == 0)
    return 1;
  if (f < 0)
    return 0;
  if (min_den > a)
    return 0;
  if (n == 0)
    return 0;
  if (Frac(1, min_den) * n < f)
    return 0;
  int sum = 0;
  for (int i = min_den; i <= a; i++) {
    sum += count_pat(i, f - Frac(1, i), a / i, n - 1);
  }
  return sum;
}

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

#include <iostream>

using namespace std;

int solve(int cur, int mul, int p, int q, int a, int n) {
  if (p == 0)
    return 1;
  if (n == 0)
    return 0;
  int res = 0;
  for (int i = cur; mul * i <= a; i++) {
    int num = p * i - q, den = q * i;
    if (num >= 0 && n - 1 >= 0)
      res += solve(i, mul * i, num, den, a, n - 1);
  }
  return res;
}

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

  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstdio>

using namespace std;

int abs(int n) { return n < 0 ? -n : n; }
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int lcm(int a, int b) { return a / gcd(a, b) * b; }

struct Frac {
  int num, den; // ツ閉ェツ子ツ，ツ閉ェツ陛ェ
  Frac() {}
  Frac(int n, int d) : num(n), den(d) {}
  Frac operator-(const Frac &f) const {
    int d = lcm(den, f.den);
    int n = (d / den * num) - (d / f.den * f.num);
    int g = gcd(abs(n), abs(d));
    return Frac(n / g, d / g);
  }
  int comp(const Frac &f) const { return num * f.den - f.num * den; }
  bool operator>(const Frac &f) const { return comp(f) > 0; }
};

int solve(int cur, int mul, const Frac &rem, int a, int n) {
  if (rem.num == 0)
    return 1;
  if (rem > Frac(n, cur))
    return 0;

  int res = 0;
  for (int i = cur; mul * i <= a; i++) {
    Frac f = rem - Frac(1, i);
    if (f.num >= 0)
      res += solve(i, mul * i, f, a, n - 1);
  }
  return res;
}

int main() {
  for (int p, q, a, n; scanf("%d%d%d%d", &p, &q, &a, &n), p | q | a | n;)
    printf("%d\\n", solve(1, 1, Frac(p, q), a, n));

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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, a, n) for (i = a; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define foreach(it, x)                                                         \\
  for (typeof(x.begin()) it = x.begin(); it != x.end(); it++)

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

int solve(int p, int q, int a, int c, int n, int prod) {
  int i, j, res = 0;

  // printf("p = %d, q = %d, a = %d, c = %d, n = %d, prod =
  // %d\\n",p,q,a,c,n,prod);
  if (p == 0)
    return 1;
  if (n == 0)
    return 0;

  // c >= q / p
  // printf("%d/%d = %d, c = %d, prod = %d\\n",q,p,q/p,c,prod);
  for (i = max(min(q / p, a), c); i * prod <= a; i++) {
    if (p * i < q)
      continue;

    int a1 = p * i - q;
    int b1 = i * q;
    // int x = gcd(a1, b1);

    /*
    if( a1 < 0 || b1 < 0 ){
        printf("%d,%d\\n",a1,b1); exit(0);
    }

    if( x == 0 ||1) x = 1;
    */
    res += solve(a1, b1, a, i, n - 1, prod * i);
  }

  return res;
}

/*
  NÂÈºi


  p/q - 1/b1 >= 0
  p*b1 / (b1*q) - { q / (q*b1) }
  p*b1 >= q@ªK{
  (p*b1 - q) / (b1 * q)
 */

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

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

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, a, n) for (i = a; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define foreach(it, x)                                                         \\
  for (typeof(x.begin()) it = x.begin(); it != x.end(); it++)

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

int solve(int p, int q, int a, int c, int n, int prod) {
  int i, j, res = 0;

  // printf("p = %d, q = %d, a = %d, c = %d, n = %d, prod =
  // %d\\n",p,q,a,c,n,prod);
  if (p == 0)
    return 1;
  if (n == 0)
    return 0;

  for (i = c; i * prod <= a; i++) {
    if (p * i < q)
      continue;

    int a1 = p * i - q;
    int b1 = i * q;
    int x = gcd(a1, b1);

    if (x == 0)
      x = 1;
    res += solve(a1 / x, b1 / x, a, i, n - 1, prod * i);
  }

  return res;
}

/*
  NÂÈºi


  p/q - 1/b1 >= 0
  p*b1 / (b1*q) - { q / (q*b1) }
  p*b1 >= q@ªK{
  (p*b1 - q) / (b1 * q)
 */

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

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

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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(n) rep(i, n)
#define all(n) n.begin(), n.end()

typedef pair<int, int> pp;

pp add(pp a, pp b) // a + b
{
  pp ans;
  ans.first = a.first * b.second + a.second * b.first;
  ans.second = a.second * b.second;
  return ans;
}

bool eq(pp a, pp b) { return a.first * b.second == b.first * a.second; }

pp target; // p/q
int limit, n, cnt;

void search(int num, int now, pp sum, int l) //
{
  if (num == n)
    return;
  for (int i = now; i < limit; i++) {
    if (l * i >= limit)
      break;
    pp tmp = pp(1, i);
    pp next = add(sum, tmp);
    if (eq(next, target))
      cnt++;
    else if (next.first / next.second > target.first / target.second)
      continue;
    else
      search(num + 1, i, next, l * i);
  }
  return;
}

int main() {
  int p, q, a;
  while (cin >> p >> q >> limit >> n && n) {
    limit++;
    target.first = p;
    target.second = q;
    cnt = 0;
    for (int i = 1; i < limit; i++) {
      pp tmp = pp(1, i);
      if (eq(target, tmp))
        cnt++;
      search(1, i, tmp, i);
    }
    cout << cnt << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(n) rep(i, n)
#define all(n) n.begin(), n.end()

const int MAXP = 810, MAXQ = 12010;
int GCD[MAXQ][MAXP];

int gcd(int a, int b) {
  if (a < MAXQ && b < MAXP) {
    if (GCD[a][b] > 0)
      return GCD[a][b];
    if (b == 0)
      return GCD[a][b] = a;
    return GCD[a][b] = gcd(b, a % b);
  } else {
    if (b == 0)
      return a;
    return gcd(b, a % b);
  }
}
int maxa;

int solve(int p, int q, int r, int d, int n) {
  if (n == 0)
    return 0;

  int ans = 0;
  int p2, q2, r2, d2, t;

  for (int d2 = d; r * d2 <= maxa; d2++) {
    p2 = p * d2 - q;
    if (p2 < 0)
      continue;
    if (p2 == 0) {
      ans++;
      continue;
    }
    q2 = d2 * q;
    t = gcd(q2, p2);
    p2 /= t;
    q2 /= t;
    ans += solve(p2, q2, r * d2, d2, n - 1);
  }
  return ans;
}

int main() {
  int p, q, a, n;
  while (cin >> p >> q >> maxa >> n && p) {
    int t = gcd(p, q);
    cout << solve(p / t, q / t, 1, 1, n) << endl;
  }

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

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

int p, q, a, n;

int dfs(int A, int B, int C, int D, int MAX) {

  int res = 0;

  if (B * p == A * q) {
    // cout<<A<<' '<<B<<' '<<C<<' '<<D<<' '<<MAX<<endl;
    return 1;
  }

  if (B * p < A * q)
    return 0;

  if (D == n)
    return 0;

  for (int i = MAX; i * C <= a; i++) {
    int t = __gcd(B, i);
    int bb = B * i / t;
    int aa = bb / B * A + bb / i;
    // cout<<A<<' '<<B<<' '<<i<<' '<<aa<<' '<<bb<<endl;
    res += dfs(aa, bb, C * i, D + 1, i);
  }

  return res;
}

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


Error: TLE

Fixed submission:

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

int p, q, a, n;

int dfs(int A, int B, int C, int D, int MAX) {

  int res = 0;

  if (B * p == A * q) {
    // cout<<A<<' '<<B<<' '<<C<<' '<<D<<' '<<MAX<<endl;
    return 1;
  }
  if (D == n)
    return 0;

  for (int i = MAX; i * C <= a; i++) {
    int t = __gcd(B, i);
    int bb = B * i / t;
    int aa = bb / B * A + bb / i;
    // cout<<A<<' '<<B<<' '<<i<<' '<<aa<<' '<<bb<<endl;
    res += dfs(aa, bb, C * i, D + 1, i);
  }

  return res;
}

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

#include <iostream>
#define eps 0.0000001
using namespace std;
int p, q, a, n, ans;
double pq;
void saiki(double sum, int x, int kai, int start) {
  if (-eps <= (sum - pq) && (sum - pq) <= eps)
    ans++;
  if (kai >= n || x >= a || sum > pq + eps)
    return;
  for (int i = start; i * x <= a; i++)
    saiki(sum + 1.0 / i, x * i, kai + 1, i);
}

int main() {
  while (1) {
    cin >> p >> q >> a >> n;
    if (!p && !q && !a && !n)
      break;
    pq = 1.0 * p / q;
    ans = 0;
    saiki(0, 1, 0, 1);
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
using namespace std;
int p, q, a, n, ans;
double pq;
void saiki(double sum, int x, int kai, int start) {
  //  cout<<sum<<endl;
  if (kai > n || x > a || sum > pq + 0.000001)
    return;
  if (abs(pq - sum) <= 0.0000001)
    ans++;
  for (int i = start; i <= a / n; i++)
    saiki(sum + 1.0 / i, x * i, kai + 1, i);
}

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

#include <cmath>
#include <iostream>

using namespace std;

int p, q, a, n;
double EPS = 1e-9;

int solve(double sum, int mul, int prev, int num) {
  if (num > n)
    return 0;
  if (mul > a)
    return 0;

  if (fabs(sum - (double)p / (double)q) < EPS)
    return 1;
  if (sum > (double)p / (double)q)
    return 0;

  int ret = 0;
  for (int i = prev; mul * i <= a; i++) {
    ret += solve(sum + 1.0 / (double)i, mul * i, i, num + 1);
  }
  return ret;
}

int main() {
  while (1) {
    cin >> p >> q >> a >> n;
    if (p == 0 && q == 0 && a == 0 && n == 0)
      break;

    cout << solve(0.0, 1, 1, 0) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>

using namespace std;

int p, q, a, n;
double EPS = 1e-9;

int solve(double sum, int mul, int prev, int num) {
  if (num > n)
    return 0;
  if (mul > a)
    return 0;

  if (fabs(sum - (double)p / (double)q) < EPS)
    return 1;
  if (sum > (double)p / (double)q)
    return 0;

  int ret = 0;
  for (int i = prev; i <= 800; i++) {
    ret += solve(sum + 1.0 / (double)i, mul * i, i, num + 1);
  }
  return ret;
}

int main() {
  while (1) {
    cin >> p >> q >> a >> n;
    if (p == 0 && q == 0 && a == 0 && n == 0)
      break;

    cout << solve(0.0, 1, 1, 0) << endl;
  }

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

#include <cstdio>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

int dfs(int p, int q, int a, int n, int t) {
  if (!p)
    return 1;
  if (p < 0 || n == 0)
    return 0;
  int ret = 0;
  for (int i = t; i <= a; ++i) {
    if (p * i - n * q > 0)
      break;
    if (p * i - q < 0)
      continue;
    ret += dfs((p * i - q), (q * i), a / i, n - 1, i);
  }
  return ret;
}

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


Error: MLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <map>
using namespace std;

int gcd(int x, int y) {

  while (min(x, y)) {
    if (x < y)
      swap(x, y);
    x %= y;
    swap(x, y);
  }
  /*
    if(x < y) swap(x, y);
  if(y == 0) return x;
  return gcd(y, x%y);
  */
  return max(x, y);
}

struct Fraction {
  int numerator, denominator; // 分子、分母
  Fraction(int p, int q) : numerator(p), denominator(q) {}
};

bool operator<(Fraction a, Fraction b) {
  if (a.numerator != b.numerator)
    return a.numerator != b.numerator;
  return a.denominator < b.denominator;
}

Fraction operator-(Fraction a, Fraction b) {
  int p = a.numerator, q = a.denominator, r = b.numerator, s = b.denominator,
      g = gcd(q, s), n = s / g * p - q / g * r, d = q / g * s,
      g_ = gcd(abs(n), d);
  return Fraction(n / g_, d / g_);
}

int main() {
  int p, q, a, n;
  while (cin >> p >> q >> a >> n, p) {
    map<pair<Fraction, int>, int> mp;
    mp[make_pair(Fraction(p, q), a)] = 1;
    int ans = 0;
    for (int i = 0; i < n; ++i) {
      map<pair<Fraction, int>, int> mp_;
      for (auto itr = mp.begin(); itr != mp.end(); ++itr) {
        Fraction f = itr->first.first;
        int b = itr->first.second, c = itr->second;
        // printf("(%d / %d), %d = %d\\n", f.numerator, f.denominator, b,
        // itr->second);
        for (int j = c; j <= b; ++j) {
          Fraction f_ = f - Fraction(1, j);
          if (f_.numerator > 0) {
            mp_[make_pair(f_, b / j)] = j;
          }
          if (f_.numerator == 0)
            ++ans;
        }
      }
      mp = mp_;
    }
    Fraction f = Fraction(0, 1);
    // for(int i = 0; i <= a; ++i) ans += mp[make_pair(f,i)];
    cout << ans << endl;
  }
  return 0;
}
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 <vector>
using namespace std;

int p, q, a, n, ans;

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

class Fraction {
public:
  int p, q;
  Fraction(int p_, int q_) {
    int g = gcd(p_, q_);
    p_ /= g;
    q_ /= g;
    p = p_, q = q_;
  }
  Fraction operator+(Fraction f) {
    Fraction result(p * f.q + f.p * q, q * f.q);
    return result;
  }
};

void solve(Fraction sum, int q_, int a_, int n_, vector<Fraction> vc) {
  if (a_ > a || n_ > n || (p * sum.q - sum.p * q) < 0) {
    return;
  }
  if (p == sum.p && q == sum.q) {
    ans++;
    /*cout << p << "/" << q << " = ";
    for(int i=0 ; i < (int)vc.size() ; i++ ){
           cout << vc[i].p << "/" << vc[i].q;
           cout << ( (i==(int)vc.size()-1)? "\\n" : " + " );
    }*/
  }
  for (int q__ = q_; a_ * q__ <= a; ++q__) {
    Fraction f(1, q__);
    vc.push_back(f);
    solve(sum + f, q__, a_ * q__, n_ + 1, vc);
  }
}

int main() {

  while (cin >> p >> q >> a >> n, p || q || a || n) {
    ans = 0;
    vector<Fraction> vc;

    for (int q_ = 1; q_ <= a; ++q_) {
      if ((p * q_ - q) < 0)
        continue;
      Fraction f(1, q_);
      vc.push_back(f);
      solve(f, q_, q_, 1, vc);
    }
    cout << ans << endl;
  }
}
Problem p00712 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>

int dfs(int p, int q, int a, int n, int pre) {
  if (p == 0)
    return 1;
  if (n == 0)
    return 0;
  int res = 0;
  for (int i = pre; i <= 6000; i++) {
    if ((a / i) == 0)
      break;
    res += dfs(i * p - q, i * q, a / i, n - 1, i);
  }
  return res;
}

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

Error: TLE

Fixed submission:

#include <stdio.h>

int dfs(int p, int q, int a, int n, int pre) {
  if (p == 0)
    return 1;
  if (n == 0)
    return 0;
  int res = 0;
  for (int i = pre; i <= 12000; i++) {
    if ((a / i) == 0)
      break;
    res += dfs(i * p - q, i * q, a / i, n - 1, i);
  }
  return res;
}

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