Language: C++

#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;

#define fst first
#define snd second
#define MP make_pair
#define PB push_back

VI PS, QS, AS, NS, cnts;
int P, Q, A, N;
int T;

VVI have_n_ts;

// p / q

PII ymemo[10000][10000] = {};

int p_, q_;

inline void yakubun(int p, int q) {
    p_ = p, q_ = q;
    if (p_ < 10000 && q_ < 10000 && ymemo[p_][q_]) {
        p = ymemo[p_][q_].fst;
        q = ymemo[p_][q_].snd;
        return;
    }

    int c = __gcd(p, q);
    if (c - 1) return;
    p /= c;
    q /= c;

    if (p_ < 10000 && q_ < 10000) {
        ymemo[p_][q_].fst = p;
        ymemo[p_][q_].snd = q;
    }
}

inline void add(int p1, int q1, int p2, int q2, int &np, int &nq) {
    np = p1 * q2 + p2 * q1;
    nq = q1 * q2;
    yakubun(np, nq);
}

inline void sub(int p1, int q1, int p2, int q2, int &np, int &nq) {
    add(p1, q1, -1 * p2, q2, np, nq);
}

inline void dfs(int p, int q, int a, int n, int before) {
    if (a > 12000) return;
    if (n >= 7) return;

    for (int t : have_n_ts[n]) {
        cnts[t] += a <= AS[t] && p == PS[t] && q == QS[t];
    }

    int np, nq;

    if (n == 6) {
        for (int t : have_n_ts[7]) {
            if (AS[t] > a) continue;
            sub(PS[t], QS[t], p, q, np, nq);
            cnts[t] += np == 1 && nq * a <= AS[t];
        }

        return;
    }

    for (int i = before; i * a <= 12000; i++) {
        add(p, q, 1, i, np, nq);
        dfs(np, nq, a * i, n + 1, i);
    }
}

int main(void) {
    have_n_ts.resize(8);
    T = 0;
    while (cin >> P >> Q >> A >> N, P) {
        yakubun(P, Q);
        PS.PB(P);
        QS.PB(Q);
        AS.PB(A);
        NS.PB(N);
        for (int i = 1; i <= N; i++) {
            have_n_ts[i].PB(T);
        }
        ++T;
    }

    cnts.resize(T);

    dfs(0, 1, 1, 0, 1);

    for (auto cnt : cnts) cout << cnt << endl;

	return 0;
}

Compiler output:
a.cc: In function 'void yakubun(int, int)':
a.cc:27:34: error: no match for 'operator&&' (operand types are 'bool' and 'PII' {aka 'std::pair<int, int>'})
   27 |     if (p_ < 10000 && q_ < 10000 && ymemo[p_][q_]) {
      |         ~~~~~~~~~~~~~~~~~~~~~~~~ ^~ ~~~~~~~~~~~~~
      |                    |                            |
      |                    bool                         PII {aka std::pair<int, int>}
a.cc:27:34: note: candidate: 'operator&&(bool, bool)' (built-in)
   27 |     if (p_ < 10000 && q_ < 10000 && ymemo[p_][q_]) {
      |         ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
a.cc:27:34: note:   no known conversion for argument 2 from 'PII' {aka 'std::pair<int, int>'} to 'bool'
In file included from /usr/include/c++/14/valarray:605,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:166,
                 from a.cc:1:
/usr/include/c++/14/bits/valarray_after.h:415:5: note: candidate: 'template<class _Dom1, class _Dom2> std::_Expr<std::__detail::_BinClos<std::__logical_and, std::_Expr, std::_Expr, _Dom1, _Dom2>, typename std::__fun<std::__logical_and, typename _Dom1::value_type>::result_type> std::operator&&(const _Expr<_Dom1, typename _Dom1::value_type>&, const _Expr<_Dom2, typename _Dom2::value_type>&)'
  415 |     _DEFINE_EXPR_BINARY_OPERATOR(&&, struct std::__logical_and)
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/valarray_after.h:415:5: note:   template argument deduction/substitution failed:
a.cc:27:49: note:   mismatched types 'const std::_Expr<_Dom1, typename _Dom1::value_type>' and 'bool'
   27 |     if (p_ < 10000 && q_ < 10000 && ymemo[p_][q_]) {
      |                                                 ^
/usr/include/c++/14/bits/valarray_after.h:415:5: note: candidate: 'template<class _Dom> std::_Expr<std::__detail::_BinClos<std::__logical_and, std::_Expr, std::_Constant, _Dom, typename _Dom::value_type>, typename std::__fun<std::__logical_and, typename _Dom1::value_type>::result_type> std::operator&&(const _Expr<_Dom1, typename _Dom1::value_type>&, const typename _Dom::value_type&)'
  415 |     _DEFINE_EXPR_BINARY_OPERATOR(&&, struct std::__logical_and)
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/valarray_after.h:415:5: note:   template argument deduction/substitution failed:
a.cc:27:49: note:   mismatched types 'const std::_Expr<_Dom1, typename _Dom1::value_type>' and 'bool'
   27 |     if (p_ < 10000 && q_ < 10000 && ymemo[p_][q_]) {
      |                                                 ^
/usr/include/c++/14/bits/valarray_after.h:415:5: note: candidate: 'template<class _Dom> std::_Expr<std::__detail::_BinClos<std::__logical_and, std::_Constant, std::_Expr, typename _Dom::value_type, _Dom>, typename std::__fun<std::__logical_and, typename _Dom1::value_type>::result_type> std::operator&&(const typename _Dom::value_type&, const _Expr<_Dom1, typename _Dom1::value_type>&)'
  415 |     _DEFINE_EXPR_BINARY_OPERATOR(&&, struct std::__logical_and)
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/valarray_after.h:415:5: note:   template argument deduction/substitution failed:
a.cc:27:49: note:   'PII' {aka 'std::pair<int, int>'} is not derived from 'const std::_Expr<_Dom1, typename _Dom1::value_type>'
   27 |     if (p_ < 10000 && q_ < 10000 && ymemo[p_][q_]) {
      |                                                 ^
/usr/include/c++/14/bits/valarray_after.h:415:5: note: candidate: 'template<class _Dom> std::_Expr<std::__detail::_BinClos<std::__logical_and, std::_Expr, std::_ValArray, _Dom, typename _Dom::value_type>, typename std::__fun<std::__logical_and, typename _Dom1::value_type>::result_type> std::operator&&(const _Expr<_Dom1, typename _Dom1::value_type>&, const valarray<typename _Dom::value_type>&)'
  415 |     _DEFINE_EXPR_BINARY_OPERATOR(&&, struct std::__logical_and)
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/valarray_after.h:415:5: note:   template argument deduction/substitution failed:
a.cc:27:49: note:   mismatched types 'const std::_Expr<_Dom1, typename _Dom1::value_type>' and 'bool'
   27 |     if (p_ < 10000 && q_ < 10000 && ymemo[p_][q_]) {
      |                                                 ^
/usr/include/c++/14/bits/valarray_after.h:415:5: note: candidate: 'template<class _Dom> std::_Expr<std::__detail::_BinClos<std::__logical_and, std::_ValArray, std::_Expr, typename _Dom::value_type, _Dom>, typename std::__fun<std::__logical_and, typename _Dom1::value_type>::result_type> std::operator&&(const valarray<typename _Dom::value_type>&, const _Expr<_Dom1, typename _Dom1::value_type>&)'
  415 |     _DEFINE_EXPR_BINARY_OPERATOR(&&, struct std::__logical_and)
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/valarray_after.h:415:5: note:   template argument deduction/substitution failed:
a.cc:27:49: note:   'PII' {aka 'std::pair<int, int>'} is not derived from 'const std::_Expr<_Dom1, typename _Dom1::value_type>'
   27 |     if (p_ < 10000 && q_ < 10000 && ymemo[p_][q_]) {
      |                                                 ^
/usr/include/c++/14/valarray:1206:1: note: candidate: 'template<class _Tp> std::_Expr<std::__detail::_BinClos<std::__logical_and, std::_ValArray, std::_ValArray, _Tp, _Tp>, typename std::__fun<std::__logical_and, _Tp>::result_type> std::operator&&(const valarray<_Tp>&, const valarray<_Tp>&)'
 1206 | _DEFINE_BINARY_OPERATOR(&&, __logical_and)
      | ^~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/valarray:1206:1: note:   template argument deduction/substitution failed:
a.cc:27:49: note:   mismatched types 'const std::valarray<_Tp>' and 'bool'
   27 |     if (p_ < 10000 && q_ < 10000 && ymemo[p_][q_]) {
      |                                                 ^
/usr/include/c++/14/valarray:1206:1: note: candidate: 'template<class _Tp> std::_Expr<std::__detail::_BinClos<std::__logical_and, std::_ValArray, std::_Constant, _Tp, _Tp>, typename std::__fun<std::__logical_and, _Tp>::result_type> std::operator&&(const valarray<_Tp>&, const typename valarray<_Tp>::value_type&)'
 1206 | _DEFINE_BINARY_OPERATOR(&&, __logical_and)
      | ^~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/valarray:1206:1: note:   template argument deduction/substitution failed:
a.cc:27:49: note:   mismatched types 'const std::valarray<_Tp>' and 'bool'
   27 |     if (p_ < 10000 && q_ < 10000 && ymemo[p_][q_]) {
      |                                                 ^
/usr/include/c++/14/valarray:1206:1: note: candidate: 'template<class _Tp> std::_Expr<std::__detail::_BinClos<std::__logical_and, std::_Constant, std::_ValArray, _Tp, _Tp>, typename std::__fun<std::__logical_and, _Tp>::result_type> std::operator&&(const typename valarray<_Tp>::value_type&, const valarray<_Tp>&)'
 1206 | _DEFINE_BINARY_OPERATOR(&&, __logical_and)
      | ^~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/valarray:1206:1: note:   template argument deduction/substitution failed:
a.cc:27:49: note:   'PII' {aka 'std::pair<int, int>'} is not derived from 'const std::valarray<_Tp>'
   27 |     if (p_ < 10000 && q_ < 10000 && ymemo[p_][q_]) {
      |                                                 ^