Language: C++

#include <cmath>
#include <cstdio>
#pragma warning(disable : 4996)
template<typename Type> Type gcd(Type a, Type b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}
// ----- Fraction Class ------ //
template<typename Type> class Fraction<Type> {
public:
    // ------ Variable ------ //
    Type p, q;
    // ------ Constructor ------ //
    Fraction<Type>() : p(0), q(1) {}
    Fraction<Type>(Type p_, Type q_) : p(p_ / gcd(abs(p_), abs(q_))), q(q_ / gcd(abs(p_), abs(q_))) { if (q < 0) p = -p, q = -q; }
    // ------ Operator ------ //
    template<typename Type> friend bool operator==(const Fraction<Type>& f1, const Fraction<Type>& f2) { return f1.p == f2.p && f1.q == f2.q; }
    template<typename Type> friend bool operator!=(const Fraction<Type>& f1, const Fraction<Type>& f2) { return f1.p != f2.p || f1.q != f2.q; }
    template<typename Type> friend Fraction operator+(const Fraction<Type>& f1, const Fraction<Type>& f2) { return Fraction(f1.p * f2.q + f1.q * f2.p, f1.q * f2.q); }
    template<typename Type> friend Fraction operator-(const Fraction<Type>& f1, const Fraction<Type>& f2) { return Fraction(f1.p * f2.q - f1.q * f2.p, f1.q * f2.q); }
    template<typename Type> friend Fraction operator*(const Fraction<Type>& f1, const Fraction<Type>& f2) { return Fraction(f1.p * f2.p, f1.q * f2.q); }
    template<typename Type> friend Fraction operator/(const Fraction<Type>& f1, const Fraction<Type>& f2) { return Fraction(f1.p * f2.q, f1.q * f2.p); }
    template<typename Type> friend bool operator<(const Fraction<Type>& f1, const Fraction<Type>& f2) { Fraction f3 = f1 - f2; return f3.p < 0; }
    template<typename Type> friend bool operator>(const Fraction<Type>& f1, const Fraction<Type>& f2) { Fraction f3 = f1 - f2; return f3.p > 0; }
    template<typename Type> friend bool operator<=(const Fraction<Type>& f1, const Fraction<Type>& f2) { Fraction f3 = f1 - f2; return f3.p <= 0; }
    template<typename Type> friend bool operator>=(const Fraction<Type>& f1, const Fraction<Type>& f2) { Fraction f3 = f1 - f2; return f3.p >= 0; }
    template<typename Type> Fraction& operator+=(const Fraction<Type>& f1) { Fraction f2 = Fraction(p * f1.q + q * f1.p, q * f1.q); p = f2.p; q = f2.q; return *this; }
    template<typename Type> Fraction& operator-=(const Fraction<Type>& f1) { Fraction f2 = Fraction(p * f1.q - q * f1.p, q * f1.q); p = f2.p; q = f2.q; return *this; }
    template<typename Type> Fraction& operator*=(const Fraction<Type>& f1) { Fraction f2 = Fraction(p * f1.p, q * f1.q); p = f2.p; q = f2.q; return *this; }
    template<typename Type> Fraction& operator/=(const Fraction<Type>& f1) { Fraction f2 = Fraction(p * f1.q, q * f1.p); p = f2.p; q = f2.q; return *this; }
};
Fraction<int> f; int p, q, a, n;
int solve(Fraction<int> f1, int a1, int c, int d) {
    if (f1 > f) return 0;
    if (f1 == f) return 1;
    if (d >= n) return 0;
    int ret = 0;
    for (int i = c; a1 * i <= a; i++) {
        ret += solve(f1 + Fraction<int>(1, i), a1 * i, i, d + 1);
    }
    return ret;
}
int main() {
    while (~scanf("%d%d%d%d", &p, &q, &a, &n)) {
        if (p == 0 && q == 0 && a == 0 && n == 0) break;
        f = Fraction<int>(p, q);
        printf("%d\n", solve(Fraction<int>(0, 1), 1, 1, 0));
    }
    return 0;
}

Compiler output:
a.cc:9:31: error: 'Fraction' is not a class template
    9 | template<typename Type> class Fraction<Type> {
      |                               ^~~~~~~~
a.cc:17:14: error: declaration of template parameter 'Type' shadows template parameter
   17 |     template<typename Type> friend bool operator==(const Fraction<Type>& f1, const Fraction<Type>& f2) { return f1.p == f2.p && f1.q == f2.q; }
      |              ^~~~~~~~
a.cc:9:10: note: template parameter 'Type' declared here
    9 | template<typename Type> class Fraction<Type> {
      |          ^~~~~~~~
a.cc:18:14: error: declaration of template parameter 'Type' shadows template parameter
   18 |     template<typename Type> friend bool operator!=(const Fraction<Type>& f1, const Fraction<Type>& f2) { return f1.p != f2.p || f1.q != f2.q; }
      |              ^~~~~~~~
a.cc:9:10: note: template parameter 'Type' declared here
    9 | template<typename Type> class Fraction<Type> {
      |          ^~~~~~~~
a.cc:19:14: error: declaration of template parameter 'Type' shadows template parameter
   19 |     template<typename Type> friend Fraction operator+(const Fraction<Type>& f1, const Fraction<Type>& f2) { return Fraction(f1.p * f2.q + f1.q * f2.p, f1.q * f2.q); }
      |              ^~~~~~~~
a.cc:9:10: note: template parameter 'Type' declared here
    9 | template<typename Type> class Fraction<Type> {
      |          ^~~~~~~~
a.cc:20:14: error: declaration of template parameter 'Type' shadows template parameter
   20 |     template<typename Type> friend Fraction operator-(const Fraction<Type>& f1, const Fraction<Type>& f2) { return Fraction(f1.p * f2.q - f1.q * f2.p, f1.q * f2.q); }
      |              ^~~~~~~~
a.cc:9:10: note: template parameter 'Type' declared here
    9 | template<typename Type> class Fraction<Type> {
      |          ^~~~~~~~
a.cc:21:14: error: declaration of template parameter 'Type' shadows template parameter
   21 |     template<typename Type> friend Fraction operator*(const Fraction<Type>& f1, const Fraction<Type>& f2) { return Fraction(f1.p * f2.p, f1.q * f2.q); }
      |              ^~~~~~~~
a.cc:9:10: note: template parameter 'Type' declared here
    9 | template<typename Type> class Fraction<Type> {
      |          ^~~~~~~~
a.cc:22:14: error: declaration of template parameter 'Type' shadows template parameter
   22 |     template<typename Type> friend Fraction operator/(const Fraction<Type>& f1, const Fraction<Type>& f2) { return Fraction(f1.p * f2.q, f1.q * f2.p); }
      |              ^~~~~~~~
a.cc:9:10: note: template parameter 'Type' declared here
    9 | template<typename Type> class Fraction<Type> {
      |          ^~~~~~~~
a.cc:23:14: error: declaration of template parameter 'Type' shadows template parameter
   23 |     template<typename Type> friend bool operator<(const Fraction<Type>& f1, const Fraction<Type>& f2) { Fraction f3 = f1 - f2; return f3.p < 0; }
      |              ^~~~~~~~
a.cc:9:10: note: template parameter 'Type' declared here
    9 | template<typename Type> class Fraction<Type> {
      |          ^~~~~~~~
a.cc:24:14: error: declaration of template parameter 'Type' shadows template parameter
   24 |     template<typename Type> friend bool operator>(const Fraction<Type>& f1, const Fraction<Type>& f2) { Fraction f3 = f1 - f2; return f3.p > 0; }
      |              ^~~~~~~~
a.cc:9:10: note: template parameter 'Type' declared here
    9 | template<typename Type> class Fraction<Type> {
      |          ^~~~~~~~
a.cc:25:14: error: declaration of template parameter 'Type' shadows template parameter
   25 |     template<typename Type> friend bool operator<=(const Fraction<Type>& f1, const Fraction<Type>& f2) { Fraction f3 = f1 - f2; return f3.p <= 0; }
      |              ^~~~~~~~
a.cc:9:10: note: template parameter 'Type' declared here
    9 | template<typename Type> class Fraction<Type> {
      |          ^~~~~~~~
a.cc:26:14: error: declaration of template parameter 'Type' shadows template parameter
   26 |     template<typename Type> friend bool operator>=(const Fraction<Type>& f1, const Fraction<Type>& f2) { Fraction f3 = f1 - f2; return f3.p >= 0; }
      |              ^~~~~~~~
a.cc:9:10: note: template parameter 'Type' declared here
    9 | template<typename Type> class Fraction<Type> {
      |          ^~~~~~~~
a.cc:27:14: error: declaration of template parameter 'Type' shadows template parameter
   27 |     template<typename Type> Fraction& operator+=(const Fraction<Type>& f1) { Fraction f2 = Fraction(p * f1.q + q * f1.p, q * f1.q); p = f2.p; q = f2.q; return *this; }
      |              ^~~~~~~~
a.cc:9:10: note: template parameter 'Type' declared here
    9 | template<typename Type> class Fraction<Type> {
      |          ^~~~~~~~
a.cc:28:14: error: declaration of template parameter 'Type' shadows template parameter
   28 |     template<typename Type> Fraction& operator-=(const Fraction<Type>& f1) { Fraction f2 = Fraction(p * f1.q - q * f1.p, q * f1.q); p = f2.p; q = f2.q; return *this; }
      |              ^~~~~~~~
a.cc:9:10: note: template parameter 'Type' declared here
    9 | template<typename Type> class Fraction<Type> {
      |          ^~~~~~~~
a.cc:29:14: error: declaration of template parameter 'Type' shadows template parameter
   29 |     template<typename Type> Fraction& operator*=(const Fraction<Type>& f1) { Fraction f2 = Fraction(p * f1.p, q * f1.q); p = f2.p; q = f2.q; return *this; }
      |              ^~~~~~~~
a.cc:9:10: note: template parameter 'Type' declared here
    9 | template<typename Type> class Fraction<Type> {
      |          ^~~~~~~~
a.cc:30:14: error: declaration of template parameter 'Type' shadows template parameter
   30 |     template<typename Type> Fraction& operator/=(const Fraction<Type>& f1) { Fraction f2 = Fraction(p * f1.q, q * f1.p); p = f2.p; q = f2.q; return *this; }
      |              ^~~~~~~~
a.cc:9:10: note: template parameter 'Type' declared here
    9 | template<typename Type> class Fraction<Type> {
      |          ^~~~~~~~