4. A password lock's password setting involves assigning one of the two numbers, 0 or 1, to each vertex of a regular $n$-sided polygon $A_{1} A_{2} \cdots A_{n}$, and coloring each vertex with one of two colors, red or blue, such that for any two adjacent vertices, at least one of the number or color is the same. How many different password settings are there for this type of password lock?
For a regular $n$-sided polygon $A_{1} A_{2} \cdots A_{n}(n \geqslant 3)$, let the number of ways to set a password lock be $a_{n}$.
It is easy to calculate that $a_{3}=28, a_{4}=84$.
For the vertices $A_{1}$ of the regular $n$-sided polygon $A_{1} A_{2} \cdots A_{n}$, there are 4 different settings for assigning values and coloring. Once $A_{1}$ is set, the setting of $A_{2}$ must have at least one of the number or color the same as $A_{1}$, so there are only 3 settings for $A_{2}$. This continues, and each subsequent point has 3 different settings, resulting in a total of $4 \times 3^{n-1}$ different settings. However, among these, the settings where the number and color of $A_{1}$ and $A_{n}$ are both different are not valid.
Let $b_{n}$ represent the number of settings for the regular $n$-sided polygon $A_{1} A_{2} \cdots A_{n}$ where the number or color of vertex $A_{i}$ and $A_{i-1}(i=2,3, \cdots, n)$ are at least one the same, and the number and color of $A_{1}$ and $A_{n}$ are both different. Then, 
\[
\left\{\begin{array}{l}
a_{n}+b_{n}=4 \times 3^{n-1}, \\
b_{n}=4 \times 3^{n-2}-a_{n-2}
\end{array}\right.
\]
(In the settings corresponding to $b_{n}$, the coordinates of $A_{1}$ and $A_{n}$ are different. Removing $A_{n}$, consider the $(n-1)$-sided polygon $A_{1} A_{2} \cdots A_{n-1}$. If the coordinates of $A_{1}$ and $A_{n-1}$ are the same, the number of methods is $a_{n-2}$, which satisfies the setting principle of the $(n-1)$-sided polygon but not the setting principle of the $n$-sided polygon.)
Thus, $a_{n}-a_{n-2}=8 \times 3^{n-2}$, and therefore 
\[
a_{2 m+1}=a_{3}+\sum_{j=2}^{m}\left(a_{2 j+1}-a_{2 j-1}\right) = 28+\sum_{j=2}^{m} 8 \times 3^{2 j-1} = 28+8 \cdot \frac{27\left(1-9^{m-1}\right)}{1-9} = 3^{2 m+1}+1;
\]
\[
a_{2 m}=a_{4}+\sum_{j=3}^{m}\left(a_{2 j}-a_{2 j-2}\right) = 84+\sum_{j=3}^{m} 8 \times 3^{2 j-2} = 84+8 \cdot \frac{81\left(1-9^{m-2}\right)}{1-9} = 3^{2 m}+3.
\]
In summary, 
\[
a_{n}=3^{n}+2+(-1)^{n}\left(n \geqslant 3, n \in \mathbf{N}^{*}\right).
\]