<s>[INST] Which is the explanation of the identity $\sum_{k=0}^n {n \choose k}k^2 = 2^{n-2}n(n+1)$? A friend of mine put me a problem some time ago, and after trying to do it, I finally surrendered. I looked for the answer online so maybe I could guess why is it like it is, but I just can't understand anything about it. Could someone explain, rather using logic, why is the identity as it is?
$$\sum_{k=0}^n  {n \choose k}k^2 = 2^{n-2}n(n+1)$$
 [/INST] By definition,
$$\binom nk=\frac{n!}{k!(n-k)!},$$ so that
$$\binom nkk=\frac{n!}{(k-1)!(n-k)!}==\frac{n(n-1)!}{(k-1)!((n-1)-(k-1))!}=\binom {n-1}{k-1}n.$$
This trick allows you to "absorb" the factor $k$ inside the binomial number.
The trick doesn't work with $k^2$ because the denominator doesn't have a second factor $k$, but it does work with $k^2-k$:
$$\binom nkk(k-1)=\frac{n!}{(k-2)!(n-k)!}==\frac{n(n-1)(n-2)!}{(k-2)!((n-2)-(k-2))!}=\binom {n-2}{k-2}n(n-1).$$
Adding these two results and summing over $k$,
$$S=2^{n-1}n+2^{n-2}n(n-1)=2^{n-2}n(n+1).$$

Check with $n=5$.
$$\begin{align}k&\to\color{blue}1\cdot0+\color{blue}5\cdot1+\color{blue}{10}\cdot2+\color{blue}{10}\cdot3+\color{blue}5\cdot4+\color{blue}1\cdot5=2^4\cdot 5=80\\
k^2-k&\to\color{blue}1\cdot0\cdot\bar1+\color{blue}5\cdot1\cdot0+\color{blue}{10}\cdot2\cdot1+\color{blue}{10}\cdot3\cdot2+\color{blue}5\cdot4\cdot3+\color{blue}1\cdot5\cdot4=2^3\cdot 5\cdot4=160\end{align}$$
and summing,
$$k^2\to\color{blue}1\cdot0^2+\color{blue}5\cdot1^2+\color{blue}{10}\cdot2^2+\color{blue}{10}\cdot3^2+\color{blue}5\cdot4^2+\color{blue}1\cdot5^2=2^3\cdot 5\cdot6=240.$$
</s>