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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

#define DBG cerr << '!' << endl;
#define REP(i, n) for (int(i) = (0); (i) < (n); ++i)
#define rep(i, s, g) for (int(i) = (s); (i) < (g); ++i)
#define rrep(i, s, g) for (int(i) = (s); i >= (g); --(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n)                                                           \\
  {                                                                            \\
    for (int i = 0; i < (n); i++)                                              \\
      cerr << v[i] << ' ';                                                     \\
    cerr << endl << endl;                                                      \\
  }
#define SHOW2d(v, i, j)                                                        \\
  {                                                                            \\
    for (int aaa = 0; aaa < i; aaa++) {                                        \\
      for (int bbb = 0; bbb < j; bbb++)                                        \\
        cerr << v[aaa][bbb] << ' ';                                            \\
      cerr << endl;                                                            \\
    }                                                                          \\
    cerr << endl;                                                              \\
  }
#define ALL(v) v.begin(), v.end()

using namespace std;

typedef long long ll;
typedef vector<int> iv;
typedef vector<iv> iiv;
typedef vector<string> sv;

double p, q, a, n, syou;
int ans;

int check(double a, double b) {
  if (a - b < 1e-9 && b - a < 1e-9)
    return 0;
  else if (a > b)
    return 1;
  else
    return 2;
}

void dfs(double sum, int seki, int now, int mae) {
  for (int i = mae; i < a; i++) {
    if (seki * i > a)
      break;
    double aa = (double)1 / i;
    int tmp = check(syou, sum + aa);
    if (tmp == 0) {
      //	cout << "!!!!!" << i << endl;
      ans++;
    } else if (tmp == 1) {
      if (now < n) {
        dfs(sum + aa, seki * i, now + 1, i);
      }
    }
  }
}

int main() {

  while (cin >> p >> q >> a >> n, p != 0) {
    syou = p / q;
    ans = 0;
    dfs(0, 1, 1, 1);
    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

#define DBG cerr << '!' << endl;
#define REP(i, n) for (int(i) = (0); (i) < (n); ++i)
#define rep(i, s, g) for (int(i) = (s); (i) < (g); ++i)
#define rrep(i, s, g) for (int(i) = (s); i >= (g); --(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n)                                                           \\
  {                                                                            \\
    for (int i = 0; i < (n); i++)                                              \\
      cerr << v[i] << ' ';                                                     \\
    cerr << endl << endl;                                                      \\
  }
#define SHOW2d(v, i, j)                                                        \\
  {                                                                            \\
    for (int aaa = 0; aaa < i; aaa++) {                                        \\
      for (int bbb = 0; bbb < j; bbb++)                                        \\
        cerr << v[aaa][bbb] << ' ';                                            \\
      cerr << endl;                                                            \\
    }                                                                          \\
    cerr << endl;                                                              \\
  }
#define ALL(v) v.begin(), v.end()

using namespace std;

typedef long long ll;
typedef vector<int> iv;
typedef vector<iv> iiv;
typedef vector<string> sv;

double p, q, a, n, syou;
int ans;

int check(double a, double b) {
  if (a - b < 1e-9 && b - a < 1e-9)
    return 0;
  else if (a > b)
    return 1;
  else
    return 2;
}

void dfs(double sum, int seki, int now, int mae) {
  for (int i = mae; i > 0; i--) {
    if (seki * i > a)
      continue;
    double aa = (double)1 / i;
    int tmp = check(syou, sum + aa);
    if (tmp == 0) {
      //	cout << "!!!!!" << i << endl;
      ans++;
    } else if (tmp == 1) {
      if (now < n) {
        dfs(sum + aa, seki * i, now + 1, i);
      }
    } else
      break;
  }
}

int main() {

  while (cin >> p >> q >> a >> n, p != 0) {
    syou = p / q;
    ans = 0;
    dfs(0, 1, 1, a / 2 + 1);
    cout << ans << endl;
  }

  return 0;
}