# Inclusion Exclusion principle question [duplicate]

What is the number of surjective (onto) functions from the set [3] to the set [3].

-

## marked as duplicate by Gerry Myerson, Sasha, Paul, Micah, Hagen von EitzenFeb 9 '13 at 7:59

We first count the complement. There are $2^5$ functions that "miss" $1$, also $2^5$ that miss $2$, and $2^5$ that miss $3$. Add. We get $3\cdot 2^5$.
But we have double-counted the functions that miss both $1$ and $2$, also the functions that miss $2$ and $3$, also the functions that miss $3$ and $1$. There is $1$ (or if you prefer, $1^5$) of each kind, so we subtract $3\cdot 1^5$.
Thus the total number of onto functions is $3^5-3\cdot 2^5+3\cdot 1^5$.