A counting problem involving ternary sequences 
A ternary sequence is a sequence all of whose elements are the  digits 0, 1 or 2. Find the number of ternary sequences of length 8 in which the digits 0 and 1 each occur an even number of times.  

The first case that should be considered is when we have all 2s, in which case there are certainly an even number of 1s and 0s.  For each 'dilution' of 2s thereafter there are two options: two 1s are placed or two 0s are placed.  There are four such dilutions that can take place so we have $4\times 2 + 1 = 9$ possibilities so far.  However, this method makes counting more difficult than it should be.  Is there a more efficient way of continuing the count?
 A: Short answer: the number of such sequences is the $8!$ times the coefficient of $x^8$ in the Taylor expansion of $(\cosh x)^3.$
Why?  The method uses exponential generating functions.
First some generalities.  If we have a problem that requires counting objects that have a "size" parameter, $n,$ and there are $a_0$ objects of size $0,$ $a_1$ objects of size $1,$ $a_2$ objects of size $2,$ and so on, then the ordinary generating function for the problem is
$$g(x)=a_0+a_1x+a_2x^2+\ldots$$
and the exponential generating function for the problem is
$$e(x)=\frac{a_0}{0!}+\frac{a_1}{1!}x+\frac{a_2}{2!}x^2+\ldots.$$
Suppose we now have a counting problem that involves combining objects of two types.  Suppose that an object of the first type of size $n$ combines with an object of the second type of size $m$ to produce an object of size $n+m.$  We can often achieve this combining effect by multiplying the generating function for objects of the first type by the generating function for objects of the second type.  Whether we use the ordinary generating function or the exponential generating function depends on whether the combining is done in an unordered way or an ordered way.  Let the number of objects of the first type of size $n$ be $a_n$ and let the number of objects of the second type of size $n$ be $b_n$.  Then the coefficient of $x^n$ in the product of the ordinary generating functions is
$$a_0b_n+a_1b_{n-1}+\ldots+a_nb_0$$
while the coefficient of $x^n/n!$ in the exponential generating function is
$$\binom{n}{0}a_0b_n+\binom{n}{1}a_1b_{n-1}+\ldots+\binom{n}{n}a_nb_0.$$
If the coefficients $a_n$ count sequences of some type of length $n,$ and the coefficients $b_n$ count sequences of some other type of length $n$, then the binomial coefficient $\binom{n}{j}$ accounts for the number of ways of interleaving the elements of an "$a$" sequence of length $j$ with the elements of a "$b$" sequence of length $n-j$ to get a combined sequence of length $n.$
To make this concrete and relate it to your problem, suppose we want to count the number of multisets of size $n$ made from the letters 0 and 1.  There is one multiset of size $n$ made of 0s only and one multiset of size $n$ made of 1s only.  Hence the ordinary generating functions for multisets made of 0s only or 1s only are
$$g_0(x)=g_1(x)=1+x+x^2+x^3+\ldots.$$
Order doesn't matter in a multiset, so the number of multisets of size $n$ made of 0s and 1s is the coefficient of $x^n$ in the product of $g_0(x)$ and $g_1(x)$.  You can check that this coefficient is $n+1.$  To count the number of multisets made of 0s, 1s, and 2s, we simply extend this idea: the number of multisets of length $n$ is the coefficient of $x^n$ in $g_0(x)g_1(x)g_2(x),$ where $g_2(x)=1+x+x^2+x^3+\ldots$ as well.  Since $1+x+x^2+x^3+\ldots=1/(1-x),$ we have that the number of multisets of length $n$ made of 0s, 1s, and 2s is the coefficient of $x^n$ in the expansion of $1/(1-x)^3.$  By Taylor expansion or using the generalized binomial theorem, you find that this coefficient is $\binom{n+2}{2}.$  Suppose now that we wanted multisets in which 0, 1, and 2 each appear an even number of times.  We use the same method: the generating function for multisets consisting of an even number of 0s is
$$h_0(x)=1+x^2+x^4+x^6+\ldots=\frac{1}{1-x^2}.$$
The same goes for multisets consisting of an even number of 1s or of an even number of 2s.  Hence the number of multisets of size $n$ consisting of an even number of 0s, and even number of 1s, and an even number of 2s is the coefficient of $x^n$ in $1/(1-x^2)^3.$  This coefficient is $\binom{n/2+2}{2}$ if $n$ is even and $0$ if $n$ is odd.
Of course, you want sequences rather than multisets.  So the order in which the 0s, 1s, and 2s get arranged when a sequence made of 0s, a sequence made of 1s, and a sequence made of 2s  get combined matters.  Hence we use exponential generating functions.  There is only one sequence of length $n$ made of 0s only, and the same goes for sequences made of 1s only or 2s only.  The exponential generating function for such sequences is therefore
$$1+x+\frac{1}{2}x^2+\frac{1}{3!}x^3+\ldots=e^x.$$
The number of sequences of length $n$ consisting of 0s, 1s, and 2s is therefore the coefficient of $x^n/n!$ in the expansion of $(e^x)^3=e^{3x}.$  This is simply $3^n,$ as one would expect.  If we now require that the number of 0s, 1s, and 2s in the sequence be even, the exponential generating functions for sequences using one letter only are
$$1+\frac{1}{2}x^2+\frac{1}{4!}x^4+\ldots=\cosh x.$$
When all three letters are used, we need the coefficient of $x^n/n!$ in the expansion of $(\cosh x)^3.$
Added: How does one extract the coefficient of $x^n/n!$?  This is straightforward to do in this problem, and leads to a closed-form solution.  In general, the coefficient of $x^n/n!$ of a formal power series is the constant term of the $n\text{th}$ derivative of the series.  Equivalently, it is obtained by taking the $n\text{th}$ derivative and setting $x$ to $0.$
Writing
$$(\cosh x)^3=\frac{1}{8}(e^x+e^{-x})^3=\frac{1}{8}(e^{3x}+3e^x+3e^{-x}+e^{-3x}),$$
we obtain
$$\begin{aligned}\frac{d^n}{dx^n}(\cosh x)^3\Big|_{x=0}&=\frac{1}{8}(3^ne^{3x}+3e^x+3(-1)^ne^{-x}+(-3)^ne^{-3x})\Big|_{x=0}\\
&=\begin{cases}{\textstyle\frac{1}{4}}(3^n+3) & \text{if $n$ is even,}\\ 0 & \text{if $n$ is odd.}\end{cases}\end{aligned}$$
The reason the coefficient is zero when $n$ is odd is that I have required that the number of 2s—and not just the numbers of 0s and 1s—be even.  (Which of course it is when $n=8$.)  If this requirement is dropped, the exponential generating function becomes $\cosh^2 x\,e^x$.  Carrying out a similar process to extract the coefficient of $x^n/n!$ leads to the closed form
$$\frac{1}{4}3^n+\frac{1}{4}(-1)^n+\frac{1}{2}.$$
This is vonbrand's solution.
A: Do as follows: Define $a_n$ as the number of $n$-sequences with an even number of 0 and an even number of 1, $b_n$ for even/odd, $c_n$ for odd/even and $d_n$ for odd/odd. Then $a_0 = 1$, and $b_0 = c_0 = d_0 = 0$. You can set up recurrences for all for them (think how you can get e.g. odd/even by adding a symbol):
$$
\begin{align*}
a_{n + 1} &= a_n + b_n + c_n \\
b_{n + 1} &= a_n + b_n + d_n \\
c_{n + 1} &= a_n + c_n + d_n \\
d_{n + 1} &= b_n + c_n + d_n
\end{align*}
$$
Define generating functions $A(z) = \sum_{n \ge 0} a_n z^n$ and so on, by the properties of generating functions:
$$
\begin{align*}
\frac{A(z) - a_0}{z} &= A(z) + B(z) + C(z) \\
\frac{B(z) - b_0}{z} &= A(z) + B(z) + D(z) \\
\frac{C(z) - c_0}{z} &= A(z) + C(z) + D(z) \\
\frac{D(z) - d_0}{z} &= B(z) + C(z) + D(z)
\end{align*}
$$
The solution of this nice linear system is:
$$
\begin{align*}
A(z) &= \frac{1 - 2 z - z^2}{(1 - z) (1 + z) (1 - 3 z)} \\
B(z) &= \frac{z}{(1 + z) (1 - 3 z)} \\
C(z) &= \frac{z}{(1 + z) (1 - 3 z)} \\
D(z) &= \frac{2 z}{1 - z) (1 + z) (1 - 3 z)}
\end{align*}
$$
We are really only interested in the $a_n$. Expanding $A(z)$ as partial fractions:
$$
A(z) = \frac{1}{4} \cdot \frac{1}{1 - 3 z} 
         + \frac{1}{4} \cdot \frac{1}{1 + z}
         + \frac{1}{2} \cdot \frac{1}{1 - z}
$$
Reading the geometric series:
$$
a_n = \frac{1}{4} \cdot 3^n + \frac{1}{4} \cdot (-1)^n + \frac{1}{2}
$$
