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

#include <algorithm>
#include <bitset>
#include <climits>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#define rep(i, m, n) for (int i = int(m); i < int(n); i++)
#define EACH(i, c) for (auto &(i) : c)
#define all(c) begin(c), end(c)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(begin(c), end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())

// #define LOCAL 0
// #ifdef LOCAL
// #define DEBUG(s) cout << (s) << endl
// #define dump(x)  cerr << #x << " = " << (x) << endl
// #define BR cout << endl;
// #else
// #define DEBUG(s) do{}while(0)
// #define dump(x) do{}while(0)
// #define BR
// #endif

// 改造
typedef long long int ll;
using namespace std;
#define INF (1 << 30) - 1
#define INFl (ll)5e15
#define DEBUG 0 // デバッグする時1にしてね
#define dump(x) cerr << #x << " = " << (x) << endl
#define MOD 1000000007
// ここから編集する

/**
 * p / q
 */
struct Bunsuu {
  using T = ll;
  T p, q;

  Bunsuu(T p_, T q_) : p(p_), q(q_) {}

  Bunsuu(pair<T, T> aPair) : p(aPair.first), q(aPair.second) {}

  Bunsuu operator+(Bunsuu right) {
    T bunbo = q * right.q;
    T bunshi = p * right.q + right.p * q;
    return Bunsuu(yakubun(bunshi, bunbo));
  }

  Bunsuu operator-(Bunsuu right) {
    T bunbo = q * right.q;
    T bunshi = p * right.q - right.p * q;
    return Bunsuu(yakubun(bunshi, bunbo));
  }

  Bunsuu operator*(Bunsuu right) {
    T bunbo = q * right.q;
    T bunshi = p * right.p;
    return Bunsuu(yakubun(bunshi, bunbo));
  }

  Bunsuu operator/(Bunsuu right) {
    T bunbo = q * right.p;
    T bunshi = p * right.q;
    return Bunsuu(yakubun(bunshi, bunbo));
  }

  void print() { cout << p << "/" << q << endl; }

  ll gcd(ll a, ll b) {
    a = abs(a);
    b = abs(b);
    if (a < b)
      swap(a, b);
    if (a % b == 0)
      return b;

    return gcd(b, a % b);
  }

  pair<T, T> yakubun(T p, T q) {
    if (p == 0)
      return pair<T, T>(0ll, 1ll);
    T gcd_v = gcd(p, q);
    p /= gcd_v;
    q /= gcd_v;
    return pair<T, T>(p, q);
  }

  double value() { return 1.0 * p / q; }
};

ll dfs(Bunsuu bunsuu, Bunsuu a, int n, int low) {
  if (bunsuu.p == 0) {
    return 1;
  }
  if (bunsuu.value() < 0)
    return 0;
  if (n == 0)
    return 0;

  ll ret = 0;
  for (int i = low; i <= a.value(); i++) {
    Bunsuu tmp = Bunsuu(1, i);
    Bunsuu hantei = bunsuu - (tmp * Bunsuu(n, 1));
    if (hantei.p > 0) {
      break;
    }
    ret += dfs(bunsuu - tmp, a * tmp, n - 1, i);
  }
  return ret;
}

ll dfs(Bunsuu bunsuu, int a, int n) { return dfs(bunsuu, Bunsuu(a, 1), n, 1); }

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while (true) {
    int p, q, a, n;
    cin >> p >> q >> a >> n;
    if (p == 0)
      break;
    cout << dfs(Bunsuu(p, q), a, n) << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#define rep(i, m, n) for (int i = int(m); i < int(n); i++)
#define EACH(i, c) for (auto &(i) : c)
#define all(c) begin(c), end(c)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(begin(c), end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())

// #define LOCAL 0
// #ifdef LOCAL
// #define DEBUG(s) cout << (s) << endl
// #define dump(x)  cerr << #x << " = " << (x) << endl
// #define BR cout << endl;
// #else
// #define DEBUG(s) do{}while(0)
// #define dump(x) do{}while(0)
// #define BR
// #endif

// 改造
typedef long long int ll;
using namespace std;
#define INF (1 << 30) - 1
#define INFl (ll)5e15
#define DEBUG 0 // デバッグする時1にしてね
#define dump(x) cerr << #x << " = " << (x) << endl
#define MOD 1000000007
// ここから編集する

/**
 * p / q
 */
struct Bunsuu {
  using T = ll;
  T p, q;

  Bunsuu(T p_, T q_) : p(p_), q(q_) {}

  Bunsuu(pair<T, T> aPair) : p(aPair.first), q(aPair.second) {}

  Bunsuu operator+(Bunsuu right) {
    T bunbo = q * right.q;
    T bunshi = p * right.q + right.p * q;
    return Bunsuu(yakubun(bunshi, bunbo));
  }

  Bunsuu operator-(Bunsuu right) {
    T bunbo = q * right.q;
    T bunshi = p * right.q - right.p * q;
    return Bunsuu(yakubun(bunshi, bunbo));
  }

  Bunsuu operator*(Bunsuu right) {
    T bunbo = q * right.q;
    T bunshi = p * right.p;
    return Bunsuu(yakubun(bunshi, bunbo));
  }

  Bunsuu operator/(Bunsuu right) {
    T bunbo = q * right.p;
    T bunshi = p * right.q;
    return Bunsuu(yakubun(bunshi, bunbo));
  }

  void print() { cout << p << "/" << q << endl; }

  ll gcd(ll a, ll b) {
    a = abs(a);
    b = abs(b);
    if (a < b)
      swap(a, b);
    if (a % b == 0)
      return b;

    return gcd(b, a % b);
  }

  pair<T, T> yakubun(T p, T q) {
    if (p == 0)
      return pair<T, T>(0ll, 1ll);
    T gcd_v = gcd(p, q);
    p /= gcd_v;
    q /= gcd_v;
    return pair<T, T>(p, q);
  }

  double value() { return 1.0 * p / q; }
};

ll dfs(Bunsuu bunsuu, Bunsuu a, int n, int low) {
  if (bunsuu.p == 0) {
    return 1;
  }
  if (bunsuu.value() < 0)
    return 0;
  if (n == 0)
    return 0;

  ll ret = 0;
  for (int i = low; i <= a.value(); i++) {
    Bunsuu tmp = Bunsuu(1, i);
    ret += dfs(bunsuu - tmp, a * tmp, n - 1, i);
  }
  return ret;
}

ll dfs(Bunsuu bunsuu, int a, int n) { return dfs(bunsuu, Bunsuu(a, 1), n, 1); }

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while (true) {
    int p, q, a, n;
    cin >> p >> q >> a >> n;
    if (p == 0)
      break;
    cout << dfs(Bunsuu(p, q), a, n) << endl;
  }

  return 0;
}