TITLE: How to solve this recurrence relation?
QUESTION [6 upvotes]: There's a frog who could climb either 1 stair or 3 stairs in one shot. In how many ways he could reach at 100th stair?
I came up with the solution $g(n) = g(n-3) + g(n-1)$, where $g(0)=g(1)=g(2)=1$ and $g(3)=2$. But to solve $g(100)$ is quite difficult if I use substitution method so I just want to know if there is a feasible method to solve for $g(100)$ ?

REPLY [3 votes]: As a possibly computationally more efficient way of getting the answer I proffer the following (only requires integer arithmetic, and AFAICT at least asymptotically less operations than Marvis' sum of binomial coefficients). We can turn the recurrence formula into a matrix equation. Define the vector $\vec{g}(n)=(g(n),g(n+1),g(n+2))$. Then $\vec{g}(n+1)=\vec{g}(n)A$, where $A$ is the $3\times3$ matrix
$$
A=\left(\begin{array}{ccc}0&0&1\\1&0&0\\0&1&1\end{array}\right).
$$
An obvious induction shows that $\vec{g}(n)=A^n\vec{g}(0)$. You already calculated that $\vec{g}(0)=(1,1,1)$, so `all' we need to do is to compute $A^{100}$ (actually $A^{98}$ would suffice, but that doesn'r really matter this time). There you should use the ever useful square-and-multiply-algorithm: Repeated squaring gives you $A^2$, $A^4=(A^2)^2$, $A^8$, $\ldots$, $A^{64}$ after six matrix multiplications. Three more matrix multiplications (a total of nine) then gives you
$$
A^{100}=A^{64+32+4}=A^{64}\cdot A^{32}\cdot A^4.
$$