Solution (source code)

= Solution

Let
$$
c=q\log(1+b)+(1-q)\log(1+a),
\qquad m=N-n.
$$
Risk-neutral valuation or direct backward induction gives
$$
V(n,s)=(1+r)^{-m}\bigl(\log s+mc\bigr).
$$
Thus
$$
A_n=(1+r)^{-(N-n)},
\qquad
B_n=(N-n)c(1+r)^{-(N-n)}.
$$
Substitution into the recursion verifies the terminal condition and the induction step.