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

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)

#define PB push_back
#define MP make_pair
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define CLR(a) memset(a, 0, sizeof(a))
#define SET(a, c) memset(a, c, sizeof(a))

#define DEBUG(x)                                                               \\
  cout << "#x"                                                                 \\
       << ": " << x << endl

using namespace std;

typedef long long int ll;
typedef vector<int> vi;
typedef vector<ll> vl;

const ll INF = INT_MAX / 3;
const ll MOD = 1000000007;
const double EPS = 1e-14;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

const int MAX_P = 800;
const int MAX_Q = 800;
const int MAX_N = 8;

int p, q, a, n;
double pdq;
int gcd(int a, int b) { return a % b == 0 ? b : gcd(b, a % b); }

pair<int, int> calDiv(int p, int q, int tq) {
  return MP(p * tq + q, q * tq);
  // int a = p * tq + q;
  // int b = q * tq;
  // int c = gcd(a,b);
  // return MP(a/c,b/c);
}

int dfs(int tp, int tq, int tn, int prev, int pro) {
  double tpdq = double(tp) / tq;
  if (tpdq > pdq)
    return 0;
  if (tpdq - pdq > tn)
    return 0;
  if (tpdq == pdq)
    return 1;
  // if( tp == p && tq == q) {
  //     return 1;
  // }
  if (tn <= 0)
    return 0;
  int ret = 0;

  int f = 0, s = 0;
  for (int i = prev; i * pro <= a; i++) {
    auto temp = calDiv(tp, tq, i);
    f = temp.first;
    s = temp.second;
    ret += dfs(f, s, tn - 1, i, pro * i);
  }
  return ret;
}

int main() {

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

    int ta = p / gcd(p, q);
    int tb = q / gcd(p, q);
    p = ta;
    q = tb;
    pdq = double(p) / double(q);

    int ans = dfs(0, 1, n, 1, 1);
    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)

#define PB push_back
#define MP make_pair
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define CLR(a) memset(a, 0, sizeof(a))
#define SET(a, c) memset(a, c, sizeof(a))

#define DEBUG(x)                                                               \\
  cout << "#x"                                                                 \\
       << ": " << x << endl

using namespace std;

typedef long long int ll;
typedef vector<int> vi;
typedef vector<ll> vl;

const ll INF = INT_MAX / 3;
const ll MOD = 1000000007;
const double EPS = 1e-14;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

const int MAX_P = 800;
const int MAX_Q = 800;
const int MAX_N = 8;

int p, q, a, n;
double pdq;
int gcd(int a, int b) { return a % b == 0 ? b : gcd(b, a % b); }

pair<int, int> calDiv(int p, int q, int tq) {
  int a = p * tq + q;
  int b = q * tq;
  int c = gcd(a, b);
  return MP(a / c, b / c);
}

int dfs(int tp, int tq, int tn, int prev, int pro) {
  double tpdq = double(tp) / tq;
  if (tpdq > pdq)
    return 0;
  if (tpdq - pdq > tn)
    return 0;
  if (tp == p && tq == q) {
    return 1;
  }
  if (tn <= 0)
    return 0;
  int ret = 0;

  int f = 0, s = 0;
  for (int i = prev; i * pro <= a; i++) {
    auto temp = calDiv(tp, tq, i);
    f = temp.first;
    s = temp.second;
    if (s > a)
      continue;
    ret += dfs(f, s, tn - 1, i, pro * i);
  }
  return ret;
}

int main() {

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

    int ta = p / gcd(p, q);
    int tb = q / gcd(p, q);
    p = ta;
    q = tb;
    pdq = double(p) / double(q);

    int ans = dfs(0, 1, n, 1, 1);
    cout << ans << endl;
  }

  return 0;
}