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

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define int long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define INF ((long long)1e18)
#define MOD ((int)1e9 + 7)
#define endl "\\n"

#define yn(f) ((f) ? "Yes" : "No")
#define YN(f) ((f) ? "YES" : "NO")

#define MAX
#define NUM

#define numerator first
#define denominator second

int con;

inline pair<int, int> add(pair<int, int> a, pair<int, int> b) {
  int nume = a.numerator * b.denominator + b.numerator * a.denominator;
  int deno = a.denominator * b.denominator;
  int gcd = __gcd(abs(nume), abs(deno));
  nume /= gcd;
  deno /= gcd;

  return make_pair(nume, deno);
}

inline int solve(pair<int, int> fra, int b, int a, int n) {
  int res = 0, temp = 1, hoge;
  // cout<<fra.numerator<<"/"<<fra.denominator<<" "<<b<<" "<<a<<" "<<n<<endl;
  if (n == 0)
    return 0;
  if (n == 1) {
    if (fra.numerator == 1 && b <= fra.denominator && fra.denominator <= a)
      return 1;
    else
      return 0;
  }
  if (fra.numerator * b > fra.denominator * n)
    return 0;

  // cout<<"temp "<<temp<<endl;
  for (int i = 1; fra.numerator * b > fra.denominator * i; i++) {
    temp *= b;
    if (temp > a) {
      // con++;
      return -1;
    }
  }
  // cout<<"temp "<<temp<<endl;
  for (int i = max(b, fra.denominator / fra.numerator); i <= a; i++) {
    if (fra.numerator * i == fra.denominator)
      res++;
    else if (fra.numerator * i > fra.denominator) {
      hoge = solve(add(fra, make_pair(-1, i)), i, a / i, n - 1);
      if (hoge == -1)
        break;
      res += hoge;
    }
  }
  return res;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // cout<<fixed<<setprecision(10);

  int p, q, a, n;

  while (true) {
    cin >> p >> q >> a >> n;

    if (!p && !q && !a && !n)
      break;

    int gcd = __gcd(p, q);
    p /= gcd;
    q /= gcd;
    // cout<<con<<endl;
    cout << solve(make_pair(p, q), 1, a, n) << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define int long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define INF ((long long)1e18)
#define MOD ((int)1e9 + 7)
#define endl "\\n"

#define yn(f) ((f) ? "Yes" : "No")
#define YN(f) ((f) ? "YES" : "NO")

#define MAX
#define NUM

#define numerator first
#define denominator second

int con;

pair<int, int> add(pair<int, int> a, pair<int, int> b) {
  int nume = a.numerator * b.denominator + b.numerator * a.denominator;
  int deno = a.denominator * b.denominator;
  int gcd = __gcd(abs(nume), abs(deno));
  nume /= gcd;
  deno /= gcd;

  return make_pair(nume, deno);
}

int solve(pair<int, int> fra, int b, int a, int n) {
  int res = 0, temp = 1;
  // cout<<fra.numerator<<"/"<<fra.denominator<<" "<<b<<" "<<a<<" "<<n<<endl;
  if (n == 0)
    return 0;
  if (n == 1) {
    if (fra.numerator == 1 && b <= fra.denominator && fra.denominator <= a)
      return 1;
    else
      return 0;
  }
  if (fra.numerator * b > fra.denominator * n)
    return 0;

  // cout<<"temp "<<temp<<endl;
  for (int i = 1; fra.numerator * b > fra.denominator * i; i++) {
    temp *= b;
    if (temp > a) {
      // con++;
      return 0;
    }
  }
  // cout<<"temp "<<temp<<endl;
  for (int i = max(b, fra.denominator / fra.numerator); i <= a; i++) {
    if (fra.numerator * i == fra.denominator)
      res++;
    else if (fra.numerator * i > fra.denominator) {
      res += solve(add(fra, make_pair(-1, i)), i, a / i, n - 1);
    }
  }
  return res;
}

signed main() {
  cin.tie(0);
  // ios::sync_with_stdio(false);
  // cout<<fixed<<setprecision(10);

  int p, q, a, n;

  while (true) {
    cin >> p >> q >> a >> n;

    if (!p && !q && !a && !n)
      break;

    int gcd = __gcd(p, q);
    p /= gcd;
    q /= gcd;
    // cout<<con<<endl;
    cout << solve(make_pair(p, q), 1, a, n) << endl;
  }

  return 0;
}