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

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

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define RSORT(c) sort((c).rbegin(), (c).rend())
#define CLR(a) memset((a), 0, sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};

struct UnionFind {
  vector<int> v;
  UnionFind(int n) : v(n) {
    for (int i = 0; i < n; i++)
      v[i] = i;
  }
  int find(int x) { return v[x] == x ? x : v[x] = find(v[x]); }
  void unite(int x, int y) { v[find(x)] = find(y); }
};

long long gcd(long long ag, long long bg) {
  if (ag < bg)
    swap(ag, bg);
  while (bg != 0) {
    ag %= bg;
    swap(ag, bg);
  }
  return ag;
}
long long p, q, a, n;
long long dfs(long long bunsi, long long bunbo, long long mal, long long num,
              long long mi) {
  long long cnt = 0;
  if (num == 1 && bunsi != 1)
    return 0;
  if (bunsi == 1 && bunbo * mal <= a && bunbo >= mi) {
    cnt++;
    if (num <= 1)
      return 1;
  }
  if (bunsi == 0)
    return 0;
  if (mi * mal > a)
    return cnt;
  for (long long i = mi; i * mal <= a; i++) {
    if (bunsi * i < bunbo)
      continue;
    if (num * bunbo < bunsi * i)
      break;
    long long g = gcd(bunsi * i - bunbo, bunbo * i);
    cnt += dfs((bunsi * i - bunbo) / g, bunbo * i / g, mal * i, num - 1, i);
  }
  return cnt;
}

int main() {
  while (cin >> p >> q >> a >> n, p | q | a | n) {
    int g = gcd(p, q);
    cout << dfs(p / g, q / g, 1, n, 1) << endl;
  }
}

Error: TLE

Fixed submission:

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

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define RSORT(c) sort((c).rbegin(), (c).rend())
#define CLR(a) memset((a), 0, sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};

struct UnionFind {
  vector<int> v;
  UnionFind(int n) : v(n) {
    for (int i = 0; i < n; i++)
      v[i] = i;
  }
  int find(int x) { return v[x] == x ? x : v[x] = find(v[x]); }
  void unite(int x, int y) { v[find(x)] = find(y); }
};

long long gcd(long long ag, long long bg) {
  if (ag < bg)
    swap(ag, bg);
  while (bg != 0) {
    ag %= bg;
    swap(ag, bg);
  }
  return ag;
}
long long p, q, a, n;
long long dfs(long long bunsi, long long bunbo, long long mal, long long num,
              long long mi) {
  long long cnt = 0;
  if (num == 1 && bunsi != 1)
    return 0;
  if (bunsi == 1 && bunbo * mal <= a && bunbo >= mi) {
    cnt++;
    if (num <= 1)
      return 1;
  }
  if (bunsi == 0)
    return 0;
  if (mi * mal > a)
    return cnt;
  for (long long i = mi; i * mal <= a; i++) {
    if (bunsi * i < bunbo)
      continue;
    long long g = gcd(bunsi * i - bunbo, bunbo * i);
    cnt += dfs((bunsi * i - bunbo) / g, bunbo * i / g, mal * i, num - 1, i);
  }
  return cnt;
}

int main() {
  while (cin >> p >> q >> a >> n, p | q | a | n) {
    int g = gcd(p, q);
    cout << dfs(p / g, q / g, 1, n, 1) << endl;
  }
}