Four. (20 points) Given the sequence $\left\{a_{n}\right\}$, $S_{n}$ represents the sum of its first $n$ terms. If it satisfies the relation $S_{n}+a_{n}=n^{2}+3 n-1$, find the general formula for the sequence $\left\{a_{n}\right\}$, i.e., the expression for $a_{n}$.
Given that $S_{1}+a_{1}=3$, therefore $a_{1}=\frac{3}{2}$.
When $n \geqslant 2$, we have
$$
\begin{array}{l}
S_{n-1}+a_{n-1}=(n-1)^{2}+3(n-1)-1, \\
S_{n}+a_{n}=n^{2}+3 n-1 .
\end{array}
$$

Subtracting the two equations, we get $a_{n}=\frac{1}{2} a_{n-1}+n+1$.
Let $a_{n}+x n+y=\frac{1}{2}\left[a_{n-1}+x(n-1)+y\right]$.
(2) - (1) gives $x n+x+y=-2 n-2$.

By the principle of identity, we have
$$
\left\{\begin{array} { l } 
{ x = - 2 , } \\
{ x + y = - 2 }
\end{array} \Rightarrow \left\{\begin{array}{l}
x=-2 \\
y=0
\end{array}\right.\right.
$$

Then equation (2) becomes $a_{n}-2 n=\frac{1}{2}\left[a_{n-1}-2(n-1)\right]$.
That is, the sequence $\left\{a_{n}-2 n\right\}$ is a geometric sequence with the first term $a_{1}-2$ and common ratio $\frac{1}{2}$.

Thus, $a_{n}-2 n=\left(a_{1}-2\right)\left(\frac{1}{2}\right)^{n-1}=-\left(\frac{1}{2}\right)^{n}$. Therefore, $a_{n}=2 n-\frac{1}{2^{n}}$.