Problem p00712 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, a, b) for (int i = (a); i < (b); ++i)
#define rrep(i, n) for (int i = (n)-1; i >= 0; --i)
#define rrep2(i, a, b) for (int i = (a)-1; i >= b; --i)
#define range(i, a, b, c) for (int i = a; ((c > 0) ? i < b : i > b); i += c)
#define chmax(a, b) (a = (a) < (b) ? (b) : (a))
#define chmin(a, b) (a = (a) > (b) ? (b) : (a))

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define ALL(a) begin(a), end(a)
#define ifnot(a) if (not(a))
#define dump(x) cerr << #x << " = " << (x) << endl
using namespace std;

// #define int ll
#ifdef _MSC_VER
const bool test = true;
#else
const bool test = false;
#endif

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
const int INF = 1 << 28;
const ll INFL = (ll)1 << 58;
ll mod = (int)1e9 + 7;
const double eps = 1e-10;
typedef long double Real;
// return -1, 0, 1
int sgn(const Real &r) { return (r > eps) - (r < -eps); }
int sgn(const Real &a, const Real &b) { return sgn(a - b); }

//.....................
const int MAX = (int)2e5 + 5;

int p, q, a, n;
int b[10];

double sum(int n_) {
  double v = 0;
  rep(i, n_) { v += (double)1 / b[i]; }
  return v;
}

bool larger(int n_) {
  double origin = (double)p / q;
  double v = sum(n_);
  return sgn(origin, v) == -1;
}

bool ok(int n_) {
  double origin = (double)p / q;
  double v = sum(n_);
  // {
  // 	rep(i, n_) cerr << b[i] << ' ';
  // 	cerr << endl;
  // }
  if (sgn(origin, v) == 0) {
    if (test) {
      rep(i, n_) cerr << b[i] << ' ';
      cerr << endl;
    }
    return true;
  }
  return false;
}

int ans(int a_, int n_, int max_v) {
  int res = 0;
  if (larger(n_))
    return 0;
  if (ok(n_))
    res++;
  if (n_ == n)
    return res;
  rep2(i, max_v, a_ + 1) {
    b[n_] = i;
    res += ans(a_ / i, n_ + 1, max(max_v, i));
  }
  return res;
}

void solve() {
  cin >> p >> q >> a >> n;
  if (p == 0)
    exit(0);
  cout << ans(a, (ll)0, 1) << endl;
}

signed main() {
  int T = (int)1e9;
  rep(i, T) {
    char s[MAX];
    if (scanf("%s", s) == EOF)
      break;
    rrep(i, strlen(s)) { ungetc(s[i], stdin); }
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, a, b) for (int i = (a); i < (b); ++i)
#define rrep(i, n) for (int i = (n)-1; i >= 0; --i)
#define rrep2(i, a, b) for (int i = (a)-1; i >= b; --i)
#define range(i, a, b, c) for (int i = a; ((c > 0) ? i < b : i > b); i += c)
#define chmax(a, b) (a = (a) < (b) ? (b) : (a))
#define chmin(a, b) (a = (a) > (b) ? (b) : (a))

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define ALL(a) begin(a), end(a)
#define ifnot(a) if (not(a))
#define dump(x) cerr << #x << " = " << (x) << endl
using namespace std;

// #define int ll
#ifdef _MSC_VER
const bool test = true;
#else
const bool test = false;
#endif

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
const int INF = 1 << 28;
const ll INFL = (ll)1 << 58;
ll mod = (int)1e9 + 7;
const double eps = 1e-10;
typedef long double Real;
// return -1, 0, 1
int sgn(const Real &r) { return (r > eps) - (r < -eps); }
int sgn(const Real &a, const Real &b) { return sgn(a - b); }

//.....................
const int MAX = (int)2e5 + 5;

int p, q, a, n;
int b[10];

bool ok(int n_) {
  double origin = (double)p / q;
  double v = 0;
  // {
  // 	rep(i, n_) cerr << b[i] << ' ';
  // 	cerr << endl;
  // }
  rep(i, n_) { v += (double)1 / b[i]; }
  if (sgn(origin, v) == 0) {
    if (test) {
      rep(i, n_) cerr << b[i] << ' ';
      cerr << endl;
    }
    return true;
  }
  return false;
}

int ans(int a_, int n_, int max_v) {
  int res = 0;
  if (ok(n_))
    res++;
  if (n_ == n)
    return res;
  rep2(i, max_v, a_ + 1) {
    b[n_] = i;
    res += ans(a_ / i, n_ + 1, max(max_v, i));
  }
  return res;
}

void solve() {
  cin >> p >> q >> a >> n;
  if (p == 0)
    exit(0);
  cout << ans(a, (ll)0, 1) << endl;
}

signed main() {
  int T = (int)1e9;
  rep(i, T) {
    char s[MAX];
    if (scanf("%s", s) == EOF)
      break;
    rrep(i, strlen(s)) { ungetc(s[i], stdin); }
    solve();
  }
  return 0;
}