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

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define all(c) (c).begin(), (c).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second

const int INF = 100000000;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
using namespace std;
typedef pair<int, int> P;
typedef long long ll;

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

inline bool equal(double d1, double d2, double eps = 1e-8) {
  return ((d1 - eps) < d2 && (d2 < (d1 + eps)));
}

void dfs(int cnt, int den, int mul, double r) {
  if (cnt == n + 1) {
    return;
  }
  if (in < r - 1e-8)
    return;

  if (equal(in, r)) {
    // cout<<"count"<<endl;
    ans++;
  }
  // cout<<cnt<<","<<den<<","<<r<<endl;

  for (int i = den; i * mul <= a; i++) {
    // cout<<cnt<<","<<den<<" : "<<r<<" + "<<Ratio(1,i)<<" =
    // "<<r+Ratio(1,i)<<endl;
    dfs(cnt + 1, i, mul * i, r + 1.0 / i);
  }
}

void solve() {
  ans = 0;
  in = 1.0 * p / q;
  dfs(0, 1, 1, 0);
  cout << ans << endl;
}

int main() {
  while (cin >> p >> q >> a >> n) {
    if (!p)
      break;
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define all(c) (c).begin(), (c).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second

const int INF = 100000000;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
using namespace std;
typedef pair<int, int> P;
typedef long long ll;

int p, q, a, n;
int ans;
P in(0, 1);

P add_ratio(P p1, P p2) {
  int t1 = __gcd(p1.sc, p2.sc);

  int num1 = p2.sc / t1 * p1.fr;
  int num2 = p1.sc / t1 * p2.fr;
  int den1 = p1.sc / t1 * p2.sc;

  int t2 = __gcd(num1 + num2, den1);

  int num = (num1 + num2) / t2;
  int den = den1 / t2;

  return P(num, den);
}

P normalize(P p1) {
  int g = __gcd(p1.fr, p1.sc);
  p1.fr /= g;
  p1.sc /= g;

  return p1;
}

void dfs(int cnt, int den, int mul, P r) {
  if (cnt == n + 1) {
    return;
  }
  if ((1.0 * in.fr / in.sc) < (1.0 * r.fr / r.sc))
    return;
  r = normalize(r);

  if (r == in) {
    // cout<<"count"<<endl;
    ans++;
  }
  // cout<<cnt<<","<<den<<","<<r<<endl;

  for (int i = den; i * mul <= a; i++) {
    // cout<<cnt<<","<<den<<" : "<<r<<" + "<<Ratio(1,i)<<" =
    // "<<r+Ratio(1,i)<<endl;
    dfs(cnt + 1, i, mul * i, add_ratio(r, P(1, i)));
  }
}

void solve() {
  ans = 0;
  in = P(p, q);
  in = normalize(in);
  dfs(0, 1, 1, P(0, 1));
  cout << ans << endl;
}

int main() {
  while (cin >> p >> q >> a >> n) {
    if (!p)
      break;
    solve();
  }
  return 0;
}