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

#include <iostream>
using namespace std;

int P, Q, A, N;
int P_, Q_;

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

int solve(int a, int n, int sum, int b) {
  int res = 0;

  /*
  int div = gcd(sum , a);
  if(P_ == sum / div && Q_ == a / div)
          return 1;
  // */
  if (sum / P_ == a / Q_ && sum % P_ + a % Q_ == 0)
    return 1;
  if (P_ * a < Q_ * sum)
    return 0;
  if (n >= N)
    return res;

  for (int i = b; a * i <= A; i++)
    res += solve(a * i, n + 1, sum * i + a, i);
  return res;
}

int main() {
  while (cin >> P >> Q >> A >> N, P | Q | A | N) {
    //		cout << P << " " << Q << " " << A << " " << N << endl;
    int div = gcd(P, Q);
    // cout << div << endl;
    P_ = P / div;
    Q_ = Q / div;

    int res = 0;
    for (int i = 1; i <= A; i++) {
      int tmp;
      tmp = solve(i, 1, 1, i);
      if (tmp > 0)
        res += tmp;
    }
    cout << res << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int P, Q, A, N;
int P_, Q_;

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

int solve(int a, int n, int sum, int b) {
  int res = 0;

  int div = gcd(sum, a);
  if (P_ == sum / div && Q_ == a / div)
    return 1;
  if (P_ * a < Q_ * sum)
    return 0;
  if (n >= N)
    return res;

  for (int i = b; a * i <= A; i++)
    res += solve(a * i, n + 1, sum * i + a, i);
  return res;
}

int main() {
  while (cin >> P >> Q >> A >> N, P | Q | A | N) {
    //		cout << P << " " << Q << " " << A << " " << N << endl;
    int div = gcd(P, Q);
    // cout << div << endl;
    P_ = P / div;
    Q_ = Q / div;

    int res = 0;
    for (int i = 1; i <= A; i++) {
      int tmp;
      tmp = solve(i, 1, 1, i);
      if (tmp > 0)
        res += tmp;
    }
    cout << res << endl;
  }
}
Problem p00712 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
int p, q, a, n;
inline bool ge(int a, int b, int c, int d) { return a * d <= b * c; }
inline bool eq(int a, int b, int c, int d) { return a * d == b * c; }
int func(int np, int nq, int nn, int nu) {
  if (nn == n) {
    return eq(p, q, np, nq);
  }
  if (ge(p, q, np, nq)) {
    return eq(p, q, np, nq);
  }
  if (nq * nu > a)
    return 0;
  int res = func(np, nq, nn, nu + 1);
  np *= nu;
  np += nq;
  nq *= nu;
  res += func(np, nq, nn + 1, nu);
  return res;
}
int main() {
  while (cin >> p >> q >> a >> n && p) {
    cout << func(0, 1, 0, 1) << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int p, q, a, n;
int gcd(int a, int b) {
  while (b) {
    a %= b;
    swap(a, b);
  }
  return a;
}

void tubun(int &a, int &b) {
  int c = gcd(a, b);
  a /= c;
  b /= c;
}

bool ge(int a, int b, int c, int d) {
  int e = b * d / gcd(b, d);
  return a * e / b <= c * e / d;
}
int func(int np, int nq, int nn, int nu) {
  if (nn == n) {
    tubun(np, nq);
    return np == p && nq == q ? 1 : 0;
  }
  if (ge(p, q, np, nq)) {
    tubun(np, nq);
    return np == p && nq == q ? 1 : 0;
  }
  if (nq * nu > a)
    return 0;
  int res = 0;
  res += func(np, nq, nn, nu + 1);
  np *= nu;
  np += nq;
  nq *= nu;
  res += func(np, nq, nn + 1, nu);
  return res;
}
int main() {
  while (cin >> p >> q >> a >> n && p) {
    tubun(p, q);
    cout << func(0, 1, 0, 1) << endl;
  }
  return 0;
}
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;
  if (p * r > n * q)
    return 0;
  for (int nq = r; a * nq <= ma; nq++) {
    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 <algorithm>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define INF (1 << 29)

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
struct Rational {
  int x, y;
  Rational() {}
  Rational(int x, int y) {
    int z = gcd(x, y);
    this->x = x / z;
    this->y = y / z;
  }
  Rational inverse() const { return Rational(y, x); }
  Rational operator*(Rational a) const { return Rational(x * a.x, y * a.y); }
  Rational operator+(Rational a) const {
    int z = gcd(y, a.y);
    return Rational(a.y / z * x + y / z * a.x, y / z * a.y);
  }
  Rational operator/(Rational a) const { return (*this) * a.inverse(); }
  bool operator==(Rational a) const { return x * a.y == a.x * y; }
  bool operator!=(Rational a) const { return !((*this) == a); }
  bool operator<(Rational a) const { return x * a.y < a.x * y; }
  bool operator>(Rational a) const { return x * a.y > a.x * y; }
};

int p, q, a, n;
Rational pq;
int dfs(Rational c, int seki, int m, int d) {
  if (c == pq) {
    return 1;
  }
  if (pq < c)
    return 0;
  int res = 0;
  if (d == 0)
    return 0;
  for (; m * seki <= a; m++) {
    if (c + Rational(d, m) < pq)
      break;
    res += dfs(Rational(1, m) + c, seki * m, m, d - 1);
  }
  return res;
}
int main() {
  while (cin >> p >> q >> a >> n, p | q | a | n) {
    pq = Rational(p, q);
    cout << dfs(Rational(0, 1), 1, 1, n) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define INF (1 << 29)

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
// 有理数クラス(正の値のみ)
struct Rational {
  int x, y;
  Rational() {}
  Rational(int x, int y) {
    int z = gcd(x, y);
    this->x = x / z;
    this->y = y / z;
  }
  Rational inverse() const { return Rational(y, x); }
  Rational operator*(Rational a) const { return Rational(x * a.x, y * a.y); }
  Rational operator+(Rational a) const {
    int z = gcd(y, a.y);
    return Rational(a.y / z * x + y / z * a.x, y / z * a.y);
  }
  Rational operator/(Rational a) const { return (*this) * a.inverse(); }
  bool operator==(Rational a) const { return x * a.y == a.x * y; }
  bool operator!=(Rational a) const { return !((*this) == a); }
  bool operator<(Rational a) const { return x * a.y < a.x * y; }
  bool operator>(Rational a) const { return x * a.y > a.x * y; }
};

int p, q, a, n;
Rational pq;
int dfs(Rational c, int seki, int m, int d) {
  if (c == pq) {
    return 1;
  }
  if (pq < c)
    return 0;
  int res = 0;
  if (d == 0)
    return 0;
  for (; m * seki <= a; m++) {
    res += dfs(Rational(1, m) + c, seki * m, m, d - 1);
  }
  return res;
}
int main() {
  while (cin >> p >> q >> a >> n, p | q | a | n) {
    pq = Rational(p, q);
    cout << dfs(Rational(0, 1), 1, 1, n) << endl;
  }
  return 0;
}
Problem p00712 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < j; i++)
#define FOR(i, j, k) for (int i = j; i < k; i++)

int p, q, a, n;

// return
// 1 -> ans == x / y
// 0 -> ans < x / y
//-1 -> ans > x / y
int isOK(int x, int y) {
  int tmp_x = x * q;
  int tmp_p = p * y;
  if (tmp_x == tmp_p)
    return 1;
  else if (tmp_x > tmp_p)
    return 0;
  else
    return -1;
}

int dfs(int x, int y, int now, int step) {
  int tmp = isOK(x, y);
  if (tmp == 1)
    return 1;
  if (tmp == 0)
    return 0;
  if (step >= n)
    return 0;
  int ret = 0;
  for (int i = now; y * i <= a; i++) {
    int next_y = i * y;
    int next_x = (next_y / i) + (next_y / y * x);
    ret += dfs(next_x, next_y, i, step + 1);
  }
  return ret;
}

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < j; i++)
#define FOR(i, j, k) for (int i = j; i < k; i++)
#define EPS 1e-8
#define EQ(a, b) ((a)-EPS < (b) && (b) < (a) + EPS)

int p, q, a, n;

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

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

int dfs(int x, int y, int now, int step, int multi) {
  if (EQ((double)p / q, (double)x / y))
    return 1;
  if ((double)p / q < (double)x / y)
    return 0;
  if (step >= n)
    return 0;
  int ret = 0;
  for (int i = now; multi * i <= a; i++) {
    int next_y = lcm(max(i, y), min(i, y));
    int next_x = (next_y / i) + (next_y / y * x);
    ret += dfs(next_x, next_y, i, step + 1, multi * i);
  }
  return ret;
}

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

#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define all(x) x.begin(), x.end()
#define dbg(x) cout << #x << ":" << x << endl
#define int long long
typedef pair<int, int> P;
typedef pair<int, P> PP;

int p, q, a, n;
int b[7];
int dfs(int x = 0, int B = 1, int pn = 1) {
  int res = 0;
  if (x > 0) {
    int sum = 0;
    for (int i = 0; i < x; i++)
      sum += B / b[i];
    /*
    for(int i=0;i<x;i++){
         cout<<b[i]<<" ";
    }cout<<endl;
    */
    if (p * B == q * sum)
      return 1;
    if (p * B < q * sum)
      return 0;
  }
  if (x == n)
    return 0;
  for (int i = pn; i * B <= a; i++) {
    b[x] = i;
    res += dfs(x + 1, i * B, i);
  }
  return res;
}

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int p, q, a, n;
int ans;
int k[8];

void rec(int x = 0, int sumg = 1, int t = 1) {
  if (sumg > a)
    return;

  if (x > 1) {
    int tmp[8] = {1, 1, 1, 1, 1, 1, 1, 1};
    for (int i = 0; i < x; i++) {
      for (int j = 0; j < x; j++) {
        if (i == j)
          continue;
        tmp[j] *= k[i];
      }
    }
    int sum = 0;
    for (int i = 0; i < x; i++)
      sum += tmp[i];
    int gcd = __gcd(sum, sumg);

    if (sum / gcd == p && sumg / gcd == q) {
      ans++;
    }
  }

  if (x == n)
    return;

  for (int i = t; i <= a; i++) {
    k[x] = i;
    if (sumg * i > a)
      break;
    rec(x + 1, sumg * i, i);
  }
}

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

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

int a;
int solve(int p, int q, int n, int seki, int l) {
  if (p == 0)
    return 1;
  if (n <= 0)
    return 0;
  int ret = 0;
  for (int i = l; seki * i <= a; ++i) {
    int bunsi = p * i - q;
    int bunbo = q * i;
    if (bunsi < 0)
      continue;
    ret += solve(bunsi, bunbo, n - 1, seki * i, i);
  }
  return ret;
}

int main() {
  int p, q, n;
  while (cin >> p >> q >> a >> n, p) {
    // memset(memo,0,sizeof(memo));
    cout << solve(p, q, n, 1, 1) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int a;
int solve(int p, int q, int n, int seki, int l) {
  if (n < 0 || p < 0)
    return 0;
  if (p == 0)
    return 1;
  int ret = 0;
  for (int i = a / seki; i >= l; --i) {
    //  for(int i=l;seki*i<=a;++i){
    int bunsi = p * i - q;
    int bunbo = q * i;
    if (bunsi < 0)
      break;
    if (bunsi && bunbo) {
      int g = __gcd(bunsi, bunbo);
      bunsi /= g;
      bunbo /= g;
      if (bunbo < 0) {
        bunsi *= -1;
        bunbo *= -1;
      }
    }
    ret += solve(bunsi, bunbo, n - 1, seki * i, i);
  }
  return ret;
}

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

#include <algorithm>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)

int gcd(int q, int r) {
  if (!r)
    return q;
  return gcd(r, q % r);
}

int c(int p, int q, int a, int n, int m) {
  // cout<<p<<" "<<q<<" "<<a<<" "<<n<<" "<<m<<endl;
  if (p < 0 || q < 0)
    return 0;
  if (!a)
    return 0;
  if (!p)
    return 1;
  if (p > 1 && n == 1)
    return 0;
  int res = 0;
  for (int r = max(q / p, m); r <= min(q * n / p, a); r++) {
    int u = p * r - q, d = q * r;
    res += c(u / gcd(u, d), d / gcd(u, d), a / r, n - 1, r);
  }
  return res;
}

int p, q, a, n;

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)

int gcd(int q, int r) {
  if (!r)
    return q;
  return gcd(r, q % r);
}

int c(int p, int q, int a, int n, int m) {
  // cout<<p<<" "<<q<<" "<<a<<" "<<n<<" "<<m<<endl;
  if (!a)
    return 0;
  if (!p)
    return 1;
  if (p > 1 && n == 1)
    return 0;
  int res = 0;
  for (int r = max(q / p, m); r <= n * q / p; r++) {
    int u = p * r - q, d = q * r;
    res += c(u / gcd(u, d), d / gcd(u, d), a / r, n - 1, r);
  }
  return res;
}

int p, q, a, n;

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

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

int rec(int p, int q, int a, int n, int last, int prod) {
  if (n == 0)
    return 0;

  int res = 0;
  for (int i = min(last, a / prod); i >= 1; i--) {
    if (p * i - q <= 0) {
      if (p * i - q == 0)
        res++;
      return res;
    }

    int below = q * i, above = p * i - q;
    // tmp = __gcd(below,above);
    // below/=tmp; above/=tmp;

    res += rec(above, below, a, n - 1, i, prod * i);
  }
  return res;
}

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

Error: TLE

Fixed submission:

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

int rec(int p, int q, int a, int n, int last, int prod) {
  if (n == 0)
    return 0;

  int res = 0;
  for (int i = min(last, a / prod); i >= 1; i--) {
    if (p * i - q <= 0) {
      if (p * i - q == 0)
        res++;
      break;
    }

    int below = q * i, above = p * i - q, tmp = __gcd(below, above);
    below /= tmp;
    above /= tmp;

    res += rec(above, below, a, n - 1, i, prod * i);
  }
  return res;
}

int main() {
  int p, q, a, n;
  while (cin >> p >> q >> a >> n, p) {
    cout << rec(p, q, a, n, a, 1) << endl;
  }
}
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 <iostream>
using namespace std;
int p, q, a, n, ans;
// pre a*
// mツ陛ェ cツ子
void rec(int now, int pre, int c, int m) {
  if (c * q == m * p && now != 0) {
    ans++;
    return;
  }
  if (now == n)
    return;
  if (now == 0) {
    for (int i = 1; i <= a; i++) {
      // c/m+1/i=(i*c+m)/(m*i)
      rec(now + 1, i, 1, i);
    }
  } else {
    for (int i = pre; m * i <= a; i++) {
      // c/m+1/i=(i*c+m)/(m*i)
      rec(now + 1, i, i * c + m, m * i);
    }
  }
}
int main() {
  while (cin >> p >> q >> a >> n, p || q || a || n) {
    ans = 0;
    rec(0, 1, 1, 1);
    cout << ans << endl;
  }
  return 0;
}
Problem p00712 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
int f(int b[], int a, int n, int p, int q, int s, int t, double u) {
  int i;
  int sm = 0, ct = 0;
  if (t == n - 1) {
    for (i = 1; i < n; i++)
      sm += s / b[i];
    if (p * s - q * sm && q * s % (p * s - q * sm) == 0) {
      b[n] = q * s / (p * s - q * sm);
      if (s * b[n] <= a) {
        if (n == 1 || b[n - 1] <= b[n])
          ct++;
      }
    }
  } else if (u < (double)p / q) {
    for (i = b[t]; s * (int)pow(i, n - t) <= a; i++) {
      if (u + (double)(n - t) / i + 0.125 < (double)p / q)
        break;
      if (s * i <= a) {
        b[t + 1] = i;
        ct += f(b, a, n, p, q, s * i, t + 1, u + 1.0 / i);
      }
    }
  }
  return ct;
}
int main() {
  int i;
  int b[8], a, n, p, q;
  int ct;
  b[0] = 1;
  while (cin >> p >> q >> a >> n && p + q + a + n) {
    ct = 0;
    for (i = 1; i < n + 1; i++)
      ct += f(b, a, i, p, q, 1, 0, 0.0);
    cout << ct << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
int f(int b[], int a, int n, int p, int q, int s, int t, double u) {
  int i;
  int sm = 0, ct = 0;
  if (t == n) {
    for (i = 1; i < n + 1; i++)
      sm += s / b[i];
    if (sm * q == s * p)
      ct++;
  } else {
    for (i = b[t]; s * pow(i, n - t) <= a; i++) {
      if (u + (double)(n - t) / i + 1 < (double)p / q)
        break;
      if (u > (double)p / q)
        break;
      if (s * i <= a) {
        b[t + 1] = i;
        ct += f(b, a, n, p, q, s * i, t + 1, u + 1.0 / i);
      }
    }
  }
  return ct;
}
int main() {
  int i;
  int b[8], a, n, p, q;
  int ct;
  b[0] = 1;
  while (cin >> p >> q >> a >> n && p + q + a + n) {
    ct = 0;
    for (i = 1; i < n + 1; i++)
      ct += f(b, a, i, p, q, 1, 0, 0.0);
    cout << ct << endl;
  }
  return 0;
}
Problem p00712 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int p, q, a, n;
int num;

void calc(int depth, vector<int> &hist, int max_l) {
  if (depth >= n)
    return;
  int mul = 1;
  for (int i = 0; i < hist.size(); i++)
    mul *= hist[i];

  for (int i = max_l;; i++) {
    int next_mul = mul * i;
    if (next_mul > a)
      break;
    hist.push_back(i);
    int upper = 0;
    for (int j = 0; j < hist.size(); j++) {
      upper += next_mul / hist[j];
    }
    if (upper * q == p * next_mul) {
      num++;
    }
    if (upper * q < p * next_mul) {
      calc(depth + 1, hist, i);
    }
    hist.pop_back();
  }
}

int main() {
  while (cin >> p >> q >> a >> n) {
    if (p == 0 && q == 0 && a == 0 && n == 0)
      break;
    num = 0;
    vector<int> hist = vector<int>();
    calc(0, hist, 1);
    cout << num << endl;
  }
}

Error: TLE

Fixed submission:

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

int p, q, a, n;
int num;

void calc(int depth, vector<int> hist, int max_l) {
  if (depth >= 7)
    return;
  int mul = 1;
  for (int i = 0; i < depth; i++)
    mul *= hist[i];

  for (int i = max_l;; i++) {
    int next_mul = mul * i;
    if (next_mul > a)
      break;
    hist[depth] = i;
    int upper = 0;
    for (int j = 0; j < depth + 1; j++) {
      upper += next_mul / hist[j];
    }
    if (upper * q == p * next_mul)
      num++;
    if (upper * q < p * next_mul) {
      calc(depth + 1, hist, i);
    }
  }
}

int main() {
  while (cin >> p >> q >> a >> n) {
    if (p == 0 && q == 0 && a == 0 && n == 0)
      break;
    num = 0;
    vector<int> hist = vector<int>(7);
    calc(0, hist, 1);
    cout << num << endl;
  }
}