4. As shown in the figure, let $A, E$ be opposite vertices of a regular octagon. There is a kangaroo at vertex $A$. The kangaroo can jump to any of the two adjacent vertices from any vertex of the octagon except vertex $E$. The kangaroo stops when it lands on vertex $E$. Let $a_{n}$ be the number of ways the kangaroo can jump exactly $n$ times from vertex $A$ to vertex $E$. Find $a_{n}$.
4. Clearly, $a_{1}=a_{2}=a_{3}=0, a_{4}=2$, and jumping an odd number of times cannot reach $E$, i.e., $a_{2 m-1}=0\left(m \in \mathbf{N}^{*}\right)$. Let the number of ways the kangaroo can jump from point $C$ to $E$ in $n$ steps be $b_{n}$, then $b_{1}=b_{3}=0, b_{2}=1, b_{4}=2$. As shown in the figure, we divide all the ways the kangaroo can jump from point $C$ to $E$ in $n(n \geqslant 3)$ steps into three categories:
First category: The kangaroo jumps from point $C$ to $D$, then back to $C$, and then jumps $n-2$ steps to reach $E$, the number of methods is $b_{n-2}$;
Second category: The kangaroo jumps from point $C$ to $B$, then back to $C$, and then jumps $n-2$ steps to reach $E$, the number of methods is $b_{n-2}$;
Third category: The kangaroo jumps from point $C$ to $B$, then to $A$, and then jumps $n-2$ steps to reach $E$, the number of methods is $a_{n-2}$. According to the principle of classification counting, we have $b_{n}=a_{n-2}+2 b_{n-2}$.
Similarly, we have
$$
a_{n}=2 b_{n-2}+2 a_{n-2} .
$$

By eliminating $b_{n-2}$ from (1)-(2), we get $b_{n}=a_{n}-a_{n-2}$, thus $b_{n-2}=a_{n-2}-a_{n-4}$.
Combining (2), we get $a_{n}=4 a_{n-2}-2 a_{n-4}$.
When $n=2 m, a_{2}=0, a_{4}=2$, let $c_{m}=a_{2 m}$, then
$$
c_{m}-4 c_{m-1}+2 c_{m-2}=0 \text {, and } c_{1}=0, c_{2}=2 .
$$

By the method of characteristic equations, we get $c_{m}=A(2+\sqrt{2})^{m}+B(2-\sqrt{2})^{m}$,
From the initial conditions, we get $A=\frac{1}{\sqrt{2}(2+\sqrt{2})}, B=\frac{-1}{\sqrt{2}(2-\sqrt{2})}$, hence $\quad a_{2 m}=c_{m}=\frac{1}{\sqrt{2}}(2+\sqrt{2})^{m-1}-\frac{1}{\sqrt{2}}(2-\sqrt{2})^{m-1}$.