Find the the coefficient of $\,x^r\,$ in $\,(1+x+x^2)^n$ I want to be able to explicitly write it as $a_r = \dots $
When using multinomial theorem, I'm getting stuck at 2 conditions, but I'm not able to simplify from there.
I wrote $(1+x+x^2)^n =\displaystyle \sum_{a,b,c}^{a+b+c=n}\frac{n!}{a!b!c!}(1)^a(x)^b(x^2)^c = \frac{n!}{a!b!c!}x^{b+2c} $
so my conditions are $b+2c=r$ and $a+b+c=n$, how do I proceed from here?
Edit: Since in this particular case, we are able to write $
(1+x+x^2)^n = \displaystyle(\frac{1-x^3}{1-x})^n$, how can we do it for any random multinomial like $(1+3x+7x^2)^n $?

Here is an easier method to solve this without complex analysis:
$\left(1+x+x^{2}\right)^{n}=\left(\frac{1-x^{3}}{1-x}\right)^{n}=\left(1-x^{3}\right)^{n}(1-x)^{-n}$
Now,
$\begin{aligned}\left[x^{r}\right]\left(1+x+x^{2}\right)^{n} &=\left[x^{r}\right]\left(1-{ }^{n} C_{1} x^{3}+{ }^{n} C_{2} x^{6} -\ldots\right )(1-x)^{-n} \\ &=\left[x^{r}\right]\left(\sum_{k=0}^{n} { (-1)^k} \  ^{n} C_{k} x^{3 k}\right)(1-x)^{-n} \end{aligned}$
We also know that cofficient of $x^{n}$ in $(1-x)^{-r}$ is ${ }^{n+r-1} C_{r-1}$
$\begin{aligned} \therefore \ \left[x^{r}\right]\left(1+x+x^{2}\right)^{n} &=\sum_{k=0}^{n}(-1)^{k}\ { }^{n} C_{k}\left[x^{r-3 k}\right](1-x)^{-r} \\
\left[x^{r}\right]\left(1+x+x^{2}\right)^{n} &=\sum_{k=0}^{n}(-1)^{k}\ \  {}^{n} C_{k} \ { }^{n+r-3 k-1}C_{n-1} \end{aligned}$
Which is the required coefficient.
You are welcome to do any sanity checks as desired.
