TITLE: All the ternary n-words with an even sum of digits and a zero.
QUESTION [6 upvotes]: I'm trying to find a recursive formula for all the ternary (using ${0,1,2}$) sequences of length $n$ which contain at least one zero, and have an even sum of digits.
My attempt so far is added below. I think the idea is right, but the recurrence relation I get fails when setting in numbers (and it's not always an integer..). any ideas?

Denote with $f\left(n\right)$ the number of legal sequences as in the question, $\bar{f}\left(n\right)$ as the number of sequences with at least one 0 with an odd number of digits, and as $g\left(n\right)$ the number of sequences with an even sum of digits and with no zeroes.
For any sequence, the sum of digits would be even if and only if there is an even number of 1s, therefore any legal sequence of length $n$ can be achieved in a unique way out of the following:
• Starting from a sequence where the digits are even and there is no zero, append zero at the end. There are $g\left(n-1\right)$ such sequences.
• Starting from a legal sequence of length $n-1$, append 0 or 2 at the end, adding $2f\left(n-1\right)$ sequences.
• Starting from a sequence with a zero where the sum of digits is odd, append 1 at the end, adding $\bar{f}\left(n-1\right)$ sequences.
These gives us that $$f\left(n\right)=2f\left(n-1\right)+\bar{f}\left(n-1\right)+g\left(n-1\right)$$
Considering $g\left(n\right)$ first, we are interested in strings of length n from $\left\{ 1,2\right\} $ with an even number of 1s. There's a simple bijection to the set of strings of length $n$ with an odd number of 1s by “flipping” the first value, and as these sets include all possibilities with no overlap, it follows that $g\left(n\right)=2^{n-1}$.
Next looking at $\bar{f}\left(n-1\right)$. We can use the fact that the sequences with a zero where the sum of digits is odd and the sequences with a zero where the sum of digits is even are exactly a disjoint union of all the sequences with a zero.
As there are $n\cdot3^{n-1}$ such sequences (we first choose one location to be 0, and fill the rest normally), we get that $\bar{f}\left(n\right)+f\left(n\right)=n\cdot3^{n-1}$, or $\bar{f}\left(n\right)=n\cdot3^{n-1}-f\left(n\right)$
Putting this all together we get that $$f\left(n\right)=2f\left(n-1\right)+\left(n-1\right)\cdot3^{n-2}-f\left(n-1\right)+2^{n-2}
 or f\left(n\right)=f\left(n-1\right)+\left(n-1\right)\cdot3^{n-2}+2^{n-2}$$

REPLY [2 votes]: As an addendum to Barry's answer above, here I show how to solve for the closed form of the recurrence relation directly.
Given the recurrence of the form $f(n)=f(n-1)+3^{n-1}-2^{n-2}$ and the initial condition $f(1)=1$ we first find what the homogeneous solution will look like.
The characteristic polynomial will be of the form $x-1=0$, so we know the final solution will be of the form: $f(n)=c_1\cdot (1)^n + p(n)$ for some particular solution $p(n)$.
Given our non-homogenous part, we expect $p(n)=d_13^n+d_22^n$ for some constants $d_1$ and $d_2$.  Plugging this in for $f(n)$ and $f(n-1)$ respectively we have:
$d_13^n+d_22^n=d_13^{n-1}+d_22^{n-1}+3^{n-1}-2^{n-2}$
By grouping similarly ordered terms, this implies the following system of equations:
$\begin{cases}3d_1=d_1+1\\4d_2=2d_2-1\end{cases}$
which implies $d_1=\frac{1}{2}$ and $d_2=-\frac{1}{2}$
So, we have $f(n)=c_1+\frac{1}{2}3^n-\frac{1}{2}2^n$
Using our initial condition, we solve for $c_1$
$1=c_1+\frac{1}{2}3^1-\frac{1}{2}2^1=c_1+\frac{1}{2}$ implying $c_1=\frac{1}{2}$
Thus, $f(n)=\frac{1}{2}+\frac{1}{2}3^n-\frac{1}{2}2^n=\frac{3^n-2^n+1}{2}$