Let $X=\{1,...,n\}$ and $Y=\{1,...,k\}$. If $n\gt\ k$ then the number of surjective functions $f:X\to Y$ $\;\;$is $\;\sum_{j=0}^{k-1} (-1)^j\binom{k}{j}(k-j)^n$
Here I show what I have done. I can't find my mistake:
Let $A=\{f:X\to Y\}$, $B=\{f:X\to Y:f\text{ surjective}\}$, $C=\{f:X\to Y:f\text{ not surjective}\}$. $\;$ We have that $\;$ $|A|=k^n$ $\;$ so $\;$ $|B|= k^n-|C|$
My strategy to calculate $|C|$ is to differentiate between different cases and I have considered that \begin{align} |C|&=|\{f:X\to Y:|\operatorname{Im}(f)|=k-1\}|+|\{f:X\to Y:|\operatorname{Im}(f)|=k-2\}|+{}\\ &\qquad+\dots+|\{f:X\to Y:|\operatorname{Im}(f)|=2\}|+|\{f:X\to Y:|\operatorname{Im}(f)|=1\}|\\[1ex] &= k(k-1)^n+\frac{k(k-1)(k-2)^n}{2!}+\dots+\frac{k(k-1)(k-2)\dots(k-(k-1))^n}{(k-1)!}\\&=\sum_{j=1}^{k-1} \binom{k}{j}(k-j)^n \end{align}
For instance, to calculate $|\{f:X\to Y:|\operatorname{Im}(f)|=k-2\}|=\frac{k(k-1)(k-2)^n}{2!}$ I take out the first element of Y where I have k possibilities. Then I take out the second element of Y and I have k-1 possibilities. As the order doesn't matter I divide by $2!$ . Now I can make $(k-2)^n$ functions with the rest of elements. (excuse my english)
And then $|B|=k^n-|C|=k^n-\sum_{j=1}^{k-1} \binom{k}{j}(k-j)^n$