I think I'm having a math block these days with things I shouldn't have problems with. Having $|A| = n$ (the number of elements of A is n), and $|B| = p$, and ($n \geq p$), how many surjective functions should I have?
I was developing the following reasoning:
$\sum _{k=1}^{p}x_{k}=n$ is the problem associated with solving n arrows inside p boxes, since we have to complete the counterdomain, I should transform this problem with the change of variables $y_{k}=x_{k}-1$.
This gets me to $\sum _{k=1}^{p}y_{k}=(n-p)$, and the result $\frac{(n-1)!}{(n-p)!(p-1)!}=C_{n-1}^{n-p}$.
I tried some inclusion-exclusion with it, but I can't construct a sequence...
The book says the answer is $\sum _{k=0}^{p}(-1)^{k}C_{p}^{k}(p-k)^n$
Obs.: $C_{n}^{p}=\frac{n!}{(n-p)!(p)!}$
When you are trying to solve this problem using $\displaystyle\sum_{k=1}^p x_k$ $=n$, you are just saying how many elements of $A$ should be assigned to an element of $B$, and you do not consider which elements, i.e. you are saying $x_k$ elements of $A$ should be assigned to the $k$-th element of $B$, and not which $x_k$ elements. For instance, in your solution, these two functions, $f$ and $g$, are considered the same :
Since in both of them $x_a = 2$ and $x_b = 1$
You can solve this problem using inclusion-exclusion using $\displaystyle\sum_{k=0}^p (-1)^k{p \choose k}(p-k)^n$, because our final answer is the number of functions in which at least one element of $A$ is assigned to every element of $B$, and in ${p \choose k}(p-k)^n$, you first choose $k$ elements where you want no element of $A$ be assigned to them, and then you create an arbitrary function from $A$ to those $p-k$ remaining elements of $B$.
I hope my answer was helpful.