Find the value of three expressions 
Consider $$(1+x+x^2)^n=\sum_{r=0}^{2n}a_rx^r$$ where $a_0,a_1,a_2,\cdots,a_{2n}$ are real numbers and $n$ is a positive integer.

Then find the value of

$$\sum_{r=0}^{n-1}a_{2r}$$

And

$$\sum_{r=1}^na_{2r-1}$$

And

$$a_2$$


My work:
I expanded $(1+x+x^2)^n$ as $$\sum_{i+j+k=n}\binom{n}{i,j,k}1^ix^jx^{2k}$$
But i don't think that it's any useful to us. Then I tried putting $x=1,\omega,\omega^2$ but then I got,
$$3^{n-1}=a_0+a_3+\cdots$$
Any help is greatly appreciated.
 A: If
$$
(1+x+x^2)^n=\sum_{k=0}^{2n}a_kx^k,
$$
then
$$
3^n=(1+1+1^2)^n=\sum_{k=0}^{2n}a_k\quad\text{and}\quad
1=(1-1+1^2)^n=\sum_{k=0}^{2n}(-1)^ka_k.
$$
Hence
$$
\sum_{k=0}^{n}a_{2k}=\frac{1+3^n}{2}\quad\text{and}\quad
\sum_{k=1}^{n}a_{2k-1}=\frac{3^n-1}{2}
$$
and if $f(x)=(1+x+x^2)^n=\sum_{k=0}^{2n}a_kx^k$, then $f''(0)=2a_2$. But
$$
f''(x)=2n(1+x+x^2)^{n-1}+n(n-1)(1+2x)^2(1+x+x^2)^{n-2}
$$
and finally
$$
2a_2=2n+n(n-1)=n(n+1).
$$
A: If you think about $$(1+x+x^2)^n = \sum_{r=0}^{2n} a_r x^r$$ then you can think of $a_r$ as the number of solutions of equation $$t_1+t_2+\ldots+t_n = r,\ t_i\in\{0,1,2\}.$$
Why is this? Think of the expansion of $(1+x+x^2)^n$. From each factor you choose some power $x^{t_i}$, so the corresponding monomial is $x^{t_1+t_2+\ldots + t_n}$, and to find $a_r$ you want to find all the choices of $t_i$ such that $x^{t_1+t_2+\ldots + t_n} = x^r$, i.e. $t_1+\ldots+t_n = r$.
Let us interpret $e_n=\sum_{r=0}^{n}a_{2r}$ in these terms. It's the number of choices of $t_i\in\{0,1,2\}$ such that $t_1+\ldots+t_n$ is even. Similarly, $o_n=\sum_{r=1}^{n}a_{2r-1}$ is the number of choices of $t_i\in\{0,1,2\}$ such that $t_1+\ldots+t_n$ is odd.
First note that $e_n+o_n$ gives us all possible choices of which we have $3^n$, since we have $3$ choices for each factor. Finally, the sum $t_1+\ldots+t_{n-1}+t_n$ is even precisely in two cases $t_1+\ldots+t_{n-1}$ is even and $t_n$ is even, $t_1+\ldots+t_{n-1}$ is odd and $t_n$ is odd. This gives us recurrence relation $$e_n = 2e_{n-1}+o_{n-1} = 2e_{n-1}+(3^{n-1}-e_{n-1}) = e_{n-1}+3^{n-1},\quad e_0 = 1.$$ Of course, this is then just partial sum of geometric series, i.e. $$e_n = e_0 + 3^0+3^1+\ldots+3^{n-1} = 1 + \frac{3^n-1}{3-1} = \frac{3^n+1}{2},$$ and automatically we get $o_n = \frac{3^n-1}2$ as well.
You also need $a_{2n}$. This is just the number of solutions of $$t_1+\ldots+t_n = 2n,\ t_i\in\{0,1,2\},$$ and it should be clear that the only solution is $t_i = 2$, for all $i$, and therefore $a_{2n} = 1$.
To find $a_2$, we are looking for the number of solutions of $$t_1+\ldots+t_n = 2,\ t_i\in\{0,1,2\},$$ and we either have precisely one $t_i = 2$ and all others $0$, or we have precisely two of them equal to $1$ and all other $0$. Therefore, $a_2 = n + \binom n2 = \frac{n(n+1)}{2}.$
