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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define mp make_pair

template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << *i << " ";
  cout << endl;
}
template <class T> void chmin(T &t, T f) {
  if (t > f)
    t = f;
}
template <class T> void chmax(T &t, T f) {
  if (t < f)
    t = f;
}
int in() {
  int x;
  scanf("%d", &x);
  return x;
}

int p, q, a, n;

int mypow(int a, int b) {
  if (b == 0)
    return 1;
  return a * mypow(a, b - 1);
}

int gcd(int a, int b) {
  if (b < a) {
    int c = b;
    b = a;
    a = c;
  }
  if (a == 0)
    return b;
  return gcd(b % a, a);
}

// int cnt;

int buf[10];
int sss;

int f(int p, int q, int aa, int nn, int m) {
  // cout<<p<<" "<<q<<" "<<aa<<" "<<nn<<" "<<m<<endl;
  // cnt++;
  if (p == 1 && nn == n - 1 && q * aa <= a && q >= m) {
    // int k;
    // for(k=sss;k<n;k++)cout<<buf[k]<<" ";
    // cout<<endl;
    return 1;
  }
  if (nn == n - 1)
    return 0;
  int i, res = 0;
  for (i = m; mypow(i, n - nn) * aa <= a; i++) {
    buf[nn] = i;
    if (p * i - q < 0)
      continue;
    int g = gcd(p * i - q, q * i);
    res += f((p * i - q) / g, (q * i) / g, aa * i, nn + 1, i);
  }
  return res;
}

int main() {
  int res;
  while (cin >> p >> q >> a >> n, p) {
    // cnt=0;
    if (p > n * q) {
      cout << 0 << endl;
      continue;
    }
    res = 0;
    int i;
    int g = gcd(p, q);
    for (i = 0; i < n; i++) {
      // sss=i;
      res += f(p / g, q / g, 1, i, 1);
    }
    // cout<<" "<<cnt<<endl;
    cout << res << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define mp make_pair

template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << *i << " ";
  cout << endl;
}
template <class T> void chmin(T &t, T f) {
  if (t > f)
    t = f;
}
template <class T> void chmax(T &t, T f) {
  if (t < f)
    t = f;
}
int in() {
  int x;
  scanf("%d", &x);
  return x;
}

int p, q, a, n;

int mypow(int a, int b) {
  if (b == 0)
    return 1;
  return a * mypow(a, b - 1);
}

int gcd(int a, int b) {
  if (b < a) {
    int c = b;
    b = a;
    a = c;
  }
  if (a == 0)
    return b;
  return gcd(b % a, a);
}

int f(int p, int q, int aa, int nn, int m) {
  // cout<<p<<" "<<q<<" "<<aa<<" "<<nn<<" "<<m<<endl;
  if (aa > a)
    return 0;
  if (p == 0 && nn == n)
    return 1;
  if (nn == n)
    return 0;
  int i, res = 0;
  for (i = m; mypow(i, n - nn) * aa <= a; i++) {
    if (p * i - q < 0)
      continue;
    int g = gcd(p * i - q, q * i);
    res += f((p * i - q) / g, (q * i) / g, aa * i, nn + 1, i);
  }
  return res;
}

int main() {
  int res;
  while (cin >> p >> q >> a >> n, p) {
    if (p > n * q) {
      cout << 0 << endl;
      continue;
    }
    res = 0;
    int i;
    for (i = 0; i < n; i++) {
      res += f(p, q, 1, i, 1);
    }
    cout << res << endl;
  }
  return 0;
}