Solution (source code)

= Solution

Let $D_n=p_{n-1}q_{n-2}-p_{n-2}q_{n-1}=(-1)^{n-2}$. Solving the given <Möbius transformation> for the <complete quotient> and rationalizing gives
$$
 \theta_n=\frac{p_{n-2}-\sqrt d\,q_{n-2}}{\sqrt d\,q_{n-1}-p_{n-1}}
 =\frac{D_n\sqrt d+dq_{n-1}q_{n-2}-p_{n-1}p_{n-2}}
 {p_{n-1}^2-dq_{n-1}^2}.
$$
Therefore
$$
\boxed{s_n=\frac{p_{n-1}^2-dq_{n-1}^2}{D_n},\qquad
 r_n=\frac{dq_{n-1}q_{n-2}-p_{n-1}p_{n-2}}{D_n}.}
$$
The denominator $D_n$ is $\pm1$, so both are <integers>. The <algebraic norm> denominator is nonzero because $\sqrt d$ is an <irrational number>.