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

/*
????´???????????????????????????????????????£??????(a/b)+(c/d)??????(a*d+c*b)/(b*d)??¨??????????????§?????????
???????????????????????¨LTE???
????????????????????????????????´??\\?????????????????§??????????????§?¢?????????????
*/

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

#define CH(N, A, B) (A <= N && N < B)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define RREP(i, a, b) for (int i = (b - 1); a <= i; i--)

using namespace std;

int P, Q, A, N, ans;

pair<int, int> partSum(int a1, int b1, int a2, int b2) {
  pair<int, int> p;
  p.first = a1 * b2 + a2 * b1;
  p.second = b1 * b2;
  return p;
}

void dfs(int step, int minnum, int pro, int sum /*pair<int,int> pn*/) {
  if (pro > A || N < step)
    return;
  // if(pn.first*Q == pn.second*P/*pn.first == P && pn.second ==  Q*/){
  if (sum * Q == pro * P) {
    ans++;
    return;
  }

  // REP(i,minnum,A/pro+1){
  for (int i = minnum; pro * i <= A; i++) {
    // pair<int, int> tpn;
    // tpn = partSum(pn.first, pn.second, 1, i);
    dfs(step + 1, i, pro * i, sum * i + pro /*tpn*/);
  }
  return;
}

int main() {

  while (1) {
    cin >> P >> Q >> A >> N;
    if (P == 0 && Q == 0 && A == 0 && N == 0)
      break;
    ans = 0;
    REP(i, 1, A + 1) { //????????????????±?????????????
      // pair<int ,int> pp(1,i);
      dfs(1, i, i, 1 /*pp*/);
    }
    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

/*
????´???????????????????????????????????????£??????(a/b)+(c/d)??????(a*d+c*b)/(b*d)??¨??????????????§?????????
???????????????????????¨LTE???
*/

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

#define CH(N, A, B) (A <= N && N < B)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define RREP(i, a, b) for (int i = (b - 1); a <= i; i--)

using namespace std;

int P, Q, A, N, ans;

pair<int, int> partSum(int a1, int b1, int a2, int b2) {
  pair<int, int> p;
  p.first = a1 * b2 + a2 * b1;
  p.second = b1 * b2;
  return p;
}

void dfs(int step, int minnum, int pro, pair<int, int> pn) {
  if (pro > A || N < step)
    return;
  if (pn.first * Q == pn.second * P /*pn.first == P && pn.second ==  Q*/) {
    ans++;
    return;
  }

  REP(i, minnum, A / pro + 1) {
    pair<int, int> tpn;
    tpn = partSum(pn.first, pn.second, 1, i);
    dfs(step + 1, i, pro * i, tpn);
  }
  return;
}

int main() {

  while (1) {
    cin >> P >> Q >> A >> N;
    if (P == 0 && Q == 0 && A == 0 && N == 0)
      break;
    ans = 0;
    REP(i, 1, A) { //????????????????±?????????????
      pair<int, int> pp(1, i);
      dfs(1, i, i, pp);
    }
    cout << ans << endl;
  }

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

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

int a, p, q, n;

int gcd(int x, int y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}

int search(int np, int nq, int nn, int now, int aa) {
  // cout << np << "\\t" << nq << "\\t" << nn << "\\t" << now << "\\t" << aa <<
  // endl;
  if (nq * p - np * q == 0)
    return 1;
  else if (nq * p - np * q < 0 || nn < 1)
    return 0;

  int result = 0;
  for (int i = now; i * aa <= a; i++) {
    result += search(np * i + nq, nq * i, nn - 1, i, i * aa);
  }

  return result;
}

int main(void) {

  while (cin >> p >> q >> a >> n, p, q, a, n) {

    cout << search(0, 1, n, q / p, 1) << endl;
  }

  return 0;
}

/*
for(i=0;i<N;i++){
cin >> X[i];
}


for(i=0;i<N;i++){
for(j=0;j<N;j++){
cin >> X[i][j];
}
}
*/

Error: TLE

Fixed submission:

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

int a;

int gcd(int x, int y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}

int search(int p, int q, int n, int num, int aa) {
  int result = 0;

  for (int i = num; i * aa <= a; i++) {
    if (p * i - q > 0 && n > 1) {
      int np = p * i - q, nq = q * i;
      int gcd_pq = gcd(np, nq);
      result += search(np / gcd_pq, nq / gcd_pq, n - 1, i, i * aa);
    } else if (p * i - q == 0)
      result++;
  }

  return result;
}

int main(void) {

  int p, q, n;

  while (cin >> p >> q >> a >> n, p, q, a, n) {

    cout << search(p, q, n, 1, 1) << endl;
  }

  return 0;
}

/*
for(i=0;i<N;i++){
cin >> X[i];
}


for(i=0;i<N;i++){
for(j=0;j<N;j++){
cin >> X[i][j];
}
}
*/
Problem p00712 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
using pii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vector<int>>;

int p, q, a, n;
map<vector<int>, int> dp;

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

int dfs(int x, int y, int z, int w, int last) {
  vi arg = {x, y, z, w, last};
  // if(dp.count(arg))return dp[arg];
  if (z == n) {
    if (x == 0)
      return 1;
    else
      return 0;
  }
  if (x < 0)
    return 0;
  if (x == 0)
    return 1;
  // x/y - (n-z)*(1/last)>0
  if (x * last > (n - z) * y)
    return 0;

  int ret = 0;
  for (int i = last; i * w <= a; i++) {
    // x/y - 1/i
    // int bunbo=lcm(y,i);
    // int bunshi=bunbo/y*x - bunbo/i*1;
    // int g=gcd(bunbo,bunshi);
    // bunbo/=g;
    // bunshi/=g;

    ret += dfs(x * i - y, y * i, z + 1, i * w, i);
  }

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

signed main() {
  while (cin >> p >> q >> a >> n, p or q or a or n) {
    dp.clear();
    int g = gcd(p, q);
    p /= g;
    q /= g;
    cout << dfs(p, q, 0, 1, 1) << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
using pii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vector<int>>;

int p, q, a, n;
map<vector<int>, int> dp;

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

int dfs(int x, int y, int z, int w, int last) {
  vi arg = {x, y, z, w, last};
  // if(dp.count(arg))return dp[arg];
  if (z == n) {
    if (x == 0)
      return 1;
    else
      return 0;
  }
  if (x < 0)
    return 0;
  if (x == 0)
    return 1;
  // x/y - (n-z)*(1/last)>0
  if (x * last > (n - z) * y)
    return 0;

  int ret = 0;
  for (int i = last; i * w <= a; i++) {
    // x/y - 1/i
    int bunbo = lcm(y, i);
    int bunshi = bunbo / y * x - bunbo / i * 1;
    int g = gcd(bunbo, bunshi);
    bunbo /= g;
    bunshi /= g;
    ret += dfs(bunshi, bunbo, z + 1, i * w, i);
  }

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

signed main() {
  while (cin >> p >> q >> a >> n, p or q or a or n) {
    dp.clear();
    int g = gcd(p, q);
    p /= g;
    q /= g;
    cout << dfs(p, q, 0, 1, 1) << endl;
  }
}
Problem p00712 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;

void yakubun(int &p, int &q) {
  for (int i = 2; i <= min(q, p); i++) {
    if (p % i == 0 && q % i == 0) {
      p /= i;
      q /= i;
      i--;
    }
  }
}
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

int lcm(int a, int b) { return a * b / gcd(a, b); }
// ツ暗クツつォツ算ツづーツ行ツつ、
void subOperation(int p, int q, int r, int s, int &np, int &nq) {
  int t = lcm(s, q);
  int u = (t / q) * p - (t / s) * r;
  yakubun(u, t);
  np = u;
  nq = t;
}

#define EPS (1e-10)
int lim;
int cnt;

void dfs(int p, int q, int next, int rem, int a) {
  if (a > lim)
    return;
  else {
    if (p == 1)
      if (a * q <= lim && q >= next)
        cnt++;
    rem--;
    if (rem == 0)
      return;
    if (next * next * a > lim)
      return;
    for (int i = (lim / a); i >= next; i--) {
      if (i * i * a > lim)
        continue;
      int np, nq;
      subOperation(p, q, 1, i, np, nq);
      if (np <= 0)
        break;
      if (i * a * nq <= lim)
        dfs(np, nq, i, rem, i * a);
    }
  }
}

int main() {
  int p, q, a, n;
  while (cin >> p >> q >> a >> n && !(p == 0 && q == 0 && a == 0 && n == 0)) {
    lim = a;
    cnt = 0;
    yakubun(p, q);
    // ツ催渉可づ個青板つゥツづァツづづアツづづアツ暗クツつ「ツづつ「ツつュツ催帰
    dfs(p, q, 1, n, 1);
    printf("%d\\n", cnt);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;

void yakubun(int &p, int &q) {
  for (int i = 2; i <= q; i++) {
    if (p % i == 0 && q % i == 0) {
      p /= i;
      q /= i;
      i--;
    }
  }
}
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

int lcm(int a, int b) { return a * b / gcd(a, b); }
// ø«Zðs¤
void subOperation(int p, int q, int r, int s, int &np, int &nq) {
  int t = lcm(s, q);
  int u = (t / q) * p - (t / s) * r;
  yakubun(u, t);
  np = u;
  nq = t;
}

#define EPS (1e-10)
int lim;
int cnt;

void dfs(int p, int q, int next, int rem, int a) {
  if (a > lim)
    return;
  else {
    if (p == 1)
      if (a * q <= lim && q >= next)
        cnt++;
    rem--;
    if (rem == 0)
      return;
    if (next * next * a > lim)
      return;
    for (int i = (lim / a); i >= next; i--) {
      if (i * i * a > lim)
        continue;
      int np, nq;
      subOperation(p, q, 1, i, np, nq);
      if (np <= 0)
        break;
      // npñÈãremªcÁÄ¢È¯êÎ³
      // if(np>rem){
      //    continue;
      //}
      if (i * a * nq <= lim)
        dfs(np, nq, i, rem, i * a);
    }
  }
}

int main() {
  int p, q, a, n;
  while (cin >> p >> q >> a >> n && !(p == 0 && q == 0 && a == 0 && n == 0)) {
    lim = a;
    cnt = 0;
    yakubun(p, q);
    // ÅÌ©çÇñÇñø¢Ä¢­ÄA
    dfs(p, q, 1, n, 1);
    printf("%d\\n", cnt);
    // cout<<cnt<<endl;
  }
  return 0;
}
Problem p00712 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
void func(int, int, double, int);
int p, q, a, n, cnt;
double k;

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

void func(int x, int w, double y, int I) {
  if (-0.00000001 <= k - y && k - y <= 0.00000001)
    cnt++;
  if (x == n)
    return;
  for (int i = I; w * i <= a; i++) {
    if (y + 1.0 / i > k + 1)
      continue;
    func(x + 1, w * i, 1.0 / i + y, i);
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int func(int, int, double, int);
int p, q, a, n;
double k;

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

int func(int x, int w, double y, int I) {
  int cnt = 0;
  if (-0.00000001 <= k - y && k - y <= 0.00000001)
    cnt++;
  for (int i = I; w * i <= a; i++) {
    if (x + 1 == n && -0.00000001 <= k - 1.0 / i - y &&
        k - 1.0 / i - y <= 0.00000001)
      cnt++;
    else if (x + 1 != n)
      cnt += func(x + 1, w * i, 1.0 / i + y, i);
  }
  return cnt;
}
Problem p00712 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int solver(int p, int q, int a, int n, int now) {
  int ans = 0;
  if (p == 0)
    return 1;
  if (n == 0 || p <= 0)
    return 0;
  for (int i = max(now, q / p); i <= a; i++) {
    int tp = p * i - q, tq = q * i;
    //		cout << p << "/" << q << " - 1/" << i << " = " << tp << "/" << tq <<
    //endl; 		cout << "a: " << a << " n: " << n << endl;
    ans += solver(tp, tq, a / i, n - 1, i);
  }
  return ans;
}
int main() {
  int p, q, a, n; // p/q ??? n ???
  while (cin >> p >> q >> a >> n, p) {
    cout << solver(p, q, a, n, 1) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int solver(int p, int q, int a, int n, int now) {
  int ans = 0;
  if (p == 0)
    return 1;
  if (n == 0 || p <= 0)
    return 0;
  for (int i = now; i <= a; i++) {
    int tp = p * i - q, tq = q * i, g;
    g = abs(__gcd(tp, tq));
    //		cout << p << "/" << q << " - 1/" << i << " = " << tp/g << "/" << tq/g
    //<< endl; 		cout << "a: " << a << " n: " << n << " g: " << g << endl;
    ans += solver(tp / g, tq / g, a / i, n - 1, i);
  }
  return ans;
}
int main() {
  int p, q, a, n; // p/q ??? n ???
  while (cin >> p >> q >> a >> n, p) {
    cout << solver(p, q, a, n, 1) << endl;
  }
  return 0;
}
Problem p00712 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define INF 1.1e9
#define LINF 1.1e18
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define BIT(x, n) bitset<n>(x)
#define PI 3.14159265358979323846

typedef long long ll;
typedef pair<ll, int> P;
typedef pair<ll, P> PP;

//-----------------------------------------------------------------------------

int p, q, a, n, num[7], ans;

void dfs(int x, int y, int z) {
  int sum = 0;
  REP(i, x) sum += y / num[i];
  if (p * y == sum * q)
    ans++;
  if (p * y < sum * q)
    return;
  if (x == n)
    return;
  FOR(i, z, a + 1) {
    if (i * y <= a) {
      num[x] = i;
      dfs(x + 1, y * i, i);
    } else
      break;
  }
}

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

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

  return 0;
}


Error: TLE

Fixed submission:

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

#define INF 1.1e9
#define LINF 1.1e18
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define BIT(x, n) bitset<n>(x)
#define PI 3.14159265358979323846

typedef long long ll;
typedef pair<ll, int> P;
typedef pair<ll, P> PP;

//-----------------------------------------------------------------------------

int p, q, a, n, num[7], ans;

void dfs(int x, int y, int z) {
  int sum = 0;
  REP(i, x) sum += y / num[i];
  if (p * y == sum * q)
    ans++;
  if (p * y < sum * q)
    return;
  if (x == n)
    return;
  FOR(i, z, a + 1) {
    if (i * y <= a) {
      num[x] = i;
      dfs(x + 1, y * i, i);
    }
  }
}

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

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

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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
const ll MOD = 1000000007;

int p, q, a, n;

int dfs(int p, int q, int x, int a_, int n_) {
  if (ll(a_) * q > a)
    return 0;
  int ret = 0;
  if (p == 1 && q >= x)
    ret++;
  // cout << p << " " << q << " " << a << " " << a_ << endl;
  if (n_ >= n)
    return ret;
  if ((n - n_ + 1) * q < p * x)
    return 0;
  for (ll i = x; i * a_ <= a; i++) {
    if (i * p > q) {
      int np = i * p - q, nq = i * q;
      int div = __gcd(np, nq);
      np /= div;
      nq /= div;
      if (i * a_ * nq <= a) {
        ret += dfs(np, nq, i, i * a_, n_ + 1);
      }
    }
  }
  return ret;
}

int main() {
  while (true) {
    cin >> p >> q >> a >> n;
    if (q == 0)
      break;
    int div = __gcd(p, q);
    cout << dfs(p / div, q / div, 1, 1, 1) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
const ll MOD = 1000000007;

ll p, q, a, n;

ll dfs(ll p, ll q, ll x, ll a_, ll n_) {
  if (a_ * q > a)
    return 0;
  ll ret = 0;
  if (p == 1 && q >= x)
    ret++;
  if (n_ >= n)
    return ret;
  if ((n - n_ + 1) * q < p * x)
    return 0;
  for (ll i = x; i * a_ <= a; i++) {
    if (i * p > q) {
      ll np = i * p - q, nq = i * q;
      ll div = __gcd(np, nq);
      np /= div;
      nq /= div;
      if (i * a_ * nq <= a) {
        ret += dfs(np, nq, i, i * a_, n_ + 1);
      }
    }
  }
  return ret;
}

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

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <math.h>
using namespace std;

class point {
public:
  int p, q; // p/q
};

int a, n;

int count;
int r(point, int, int, int);
point pp(point, int);
int gcd(int, int);
int main() {
  point p;
  while (cin >> p.p >> p.q >> a >> n) {
    if (p.p == 0 && p.q == 0 && a == 0 && n == 0)
      break;
    count = 0;
    r(p, 1, 0, 1);
    cout << count << endl;
  }
  return 0;
}

int r(point src, int q, int c, int k) {
  // cout<<"   "<<src.p<<"/"<<src.q<<endl;
  int ret;
  if ((double)src.p * q > (double)(n - c) * src.q)
    return 2;
  if (k > a)
    return 0;
  if (c > n)
    return 0;
  if (src.p == 0) {
    count++;
    return 1;
  }
  if (src.p < 0)
    return 1;
  while (1) {
    ret = r(pp(src, q), q, c + 1, k * q);
    if (ret != 1)
      break;
    q++;
  }
  return 1;
}

point pp(point src, int q) {
  src.p = src.p * q - src.q;
  src.q = src.q * q;
  int g = gcd((abs(src.p) < src.q) ? abs(src.p) : src.q,
              (abs(src.p) < src.q) ? src.q : abs(src.p));
  src.p /= g;
  src.q /= g;
  return src;
}

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

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <math.h>
using namespace std;

class point {
public:
  int p, q; // p/q
};

int a, n;

int count;
int r(point, int, int, int);
point pp(point, int);
int gcd(int, int);
int main() {
  point p;
  while (cin >> p.p >> p.q >> a >> n) {
    if (p.p == 0 && p.q == 0 && a == 0 && n == 0)
      break;
    count = 0;
    r(p, 1, 0, 1);
    cout << count << endl;
  }
  return 0;
}

int r(point src, int q, int c, int k) {
  // cout<<"   "<<src.p<<"/"<<src.q<<endl;
  if (k > a)
    return 0;
  if (c > n)
    return 0;
  if (src.p == 0) {
    count++;
    return 1;
  }
  if (src.p < 0)
    return 1;
  while (r(pp(src, q), q, c + 1, k * q))
    q++;
  return 1;
}

point pp(point src, int q) {
  src.p = src.p * q - src.q;
  src.q = src.q * q;
  int g = gcd((abs(src.p) < src.q) ? abs(src.p) : src.q,
              (abs(src.p) < src.q) ? src.q : abs(src.p));
  src.p /= g;
  src.q /= g;
  return src;
}

int gcd(int a, int b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
Problem p00712 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <set>
#include <vector>

using namespace std;

int p, q, n, a;

int ans;
int rec(int sum_u, int sum_l, int nn, int k) {
  if (sum_l > a || nn == 0)
    return -1;

  if (sum_u * q == sum_l * p) {
    ans++;
    return 0;
  }
  if (p / double(q) < sum_u / double(sum_l))
    return 0;

  nn--;
  for (int x = k;; x++) {
    if (rec(sum_u * x + sum_l, sum_l * x, nn, x) < 0)
      break;
  }
  return 0;
}

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

Error: TLE

Fixed submission:

#include <iostream>
#include <set>
#include <vector>

using namespace std;

int p, q, n, a;

int ans;
int rec(int sum_u, int sum_l, int nn, int k) {
  if (sum_l > a || nn == 0)
    return -1;

  if (sum_u * q == sum_l * p) {
    ans++;
    return 0;
  }

  nn--;
  for (int x = k;; x++) {
    if (rec(sum_u * x + sum_l, sum_l * x, nn, x) < 0)
      break;
  }
  return 0;
}

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

#include <iostream>
#include <stack>
#include <tuple>
#include <vector>

const int pq_max = 800;
const int n_max = 7;
const int a_max = 12000;

int f(int p, int q, int a, int n, int low) {
  int S = 0;
  if (p == 0) {
    return 1;
  }
  if (n == 0) {
    return 0;
  }
  // int m = gcd(p, q);
  // p /= m; q /= m;

  if (q / p > low)
    low = q / p;
  int high = q * n / p;
  if (high >= (a)) {
    high = a;
  }

  // for ( int i = 1; i <= j; ++i ) {
  for (int i = low; i <= high; ++i) {
    // if ( i*p - q < 0 ) continue;
    // if ( i * b > a ) break;
    int s = i * p - q;
    int r = q * i;

    S += f(s, r, a / i, n - 1, i);
  }
  return S;
}

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

    std::cin >> p >> q >> a >> n;
    if (p == 0 && q == 0)
      break;
    std::cout << f(p, q, a, n, 1) << std::endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <stack>
#include <tuple>
#include <vector>

const int pq_max = 800;
const int n_max = 7;
const int a_max = 12000;

typedef std::tuple<int, int, int, int, int, int> T;

int gcd(int r, int l) {
  // if ( r == 0 ) return l;
  // return gcd(l % r, r);
  while (r != 0) {
    int b = r;
    int a = l % r;
    r = a;
    l = b;
  }
  return l;
}

int f(int p, int q, int a, int n, int b, int j) {
  std::stack<T> st;
  int S = 0;
  st.push(T(p, q, a, n, b, j));
  while (!st.empty()) {
    auto t = st.top();
    st.pop();
    int p = std::get<0>(t);
    int q = std::get<1>(t);
    int a = std::get<2>(t);
    int n = std::get<3>(t);
    int b = std::get<4>(t);
    int j = std::get<5>(t);

    if (p == 0) {
      S += 1;
      continue;
    }
    if (n == 0) {
      continue;
    }
    int m = gcd(p, q);
    p /= m;
    q /= m;

    for (int i = 1; i <= j; ++i) {
      if (i * p - q < 0)
        continue;
      if (i * b > a)
        break;
      int s = i * p - q;
      int r = q * i;
      if (s / r > n)
        continue;

      st.push(T(s, r, a, n - 1, i * b, i));
      // S += f(s, r, a, n-1, i * b, i);
    }
  }
  return S;
}

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

    std::cin >> p >> q >> a >> n;
    if (p == 0 && q == 0)
      break;
    std::cout << f(p, q, a, n, 1, pq_max) << std::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 k, int prev, int p, int q) {
  if (k == N)
    return 0;
  int sum = 0;
  for (int i = prev; q * i <= A; i++) {
    sum += DFS(k + 1, i, p * i + q, q * i);
    if ((p * i + q) * Q == q * i * P)
      sum++;
  }
  return sum;
}
int main() {
  while (cin >> P >> Q >> A >> N, P) {
    cout << DFS(0, 1, 0, 1) << endl;
  }
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
int P, Q, A, N;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int DFS(int k, int prev, int p, int q) {
  if (k == N)
    return 0;
  int sum = 0;
  for (int i = prev; q * i <= A; i++) {
    sum += DFS(k + 1, i, p * i + q, q * i);
    int g = gcd(p * i + q, q * i);
    if ((p * i + q) / g == P && (q * i) / g == Q)
      sum++;
  }
  return sum;
}
int main() {
  while (cin >> P >> Q >> A >> N, P) {
    int g = gcd(P, Q);
    P /= g;
    Q /= g;
    cout << DFS(0, 1, 0, 1) << endl;
  }
}