Number of functions $f\colon\{1,…,n\}\to\{1,…,5\} $ such that the image is exactly of size $4$ 
Let $n > 5$. Find the number of functions $f\colon\{1,…,n\}\to\{1,…,5\} $ such that the image is exactly of size $4$.

I found that it is easier to find the number of series of size $n$ which can be made of the letters $\{1,..,5\} $ with exactly $4$ letters.
The total number is $\binom{5}{4}4^n$. However, it also includes series which are made of less different letters such as $(1,1,1...1)$
So I thought I should use the inclusion-exclusion principle, but I am not quite sure which sets to define.
Thanks in advance.
 A: Let $S(n,k)$ denote the Stirling numbers of the second kind and $[n]=\{1,\dots,n\}$. By definition, there are $S(n,k)$ partitions of $[n]$ into $k$ non-empty subsets.
To count the number of maps $[n]\to[5]$ that have an image of cardinality $4$, first partition $[n]$ into $4$ non-empty subset. Now to each of the four subsets assign a different element of $[5]$. Hence, there are
$$
S(n,4) \cdot 5\cdot 4\cdot 3\cdot 2 = 120\cdot S(n,4)
$$
such maps.
See this question for a combinatorial calculation of $S(n,k)$ using the inclusion-exclusion principle.
A: There is a problem that $\binom 544^n$ not only counts cases where fewer than four numbers are used, it counts them multiple times.
By symmetry, however, the answer is going to be $\binom 54a_n$, where $a_n$ is the number of functions $f:\{1,...,n\}\to\{1,2,3,4\}$ which are surjective (use all four numbers). This can be calculated by inclusion-exclusion. There are $4^n$ functions, and for each $i\in\{1,2,3,4\}$ there are $3^n$ which don't use $i$. For each pair $i\neq j$ there are $2^n$ which don't use $i$ or $j$, and for each $i,j,k$ all different there is only $1$ which doesn't use any of them. Therefore we get (where in each sum $i,j$ etc are distinct and in $\{1,2,3,4\}$):
$$a_n=4^n-\sum_{i}3^n+\sum_{i,j}2^n-\sum_{i,j,k}1=4^n-4\times 3^n+6\times 2^n-4.$$
As a sanity check, this gives $a_n=0$ for $n=1,2,3$ as it should do.
A: The problem requires us to find the number of mappings from domain to co-domain, where domain contains n elements and the co-domain contains 5 elements and the range should contain exactly 4 elements belonging to the co-domain. To find the number of functions, first, select the 4 elements out of those 5 elements. Hence the first factor is $\ \binom{5}{4} $.
Then, we have to place numbers 1 to n in these 4 "boxes" such that each box contains at least 1 element. Also, the order in which we place 2 numbers in the "box" doesn't matter. In the sense, the order of elements in a "box" doesn't matter.
Hence, placing the n elements in a line and making a total of 4 partitions, is over-counting, since we are counting the order in which elements are placed. To remove the multiple-count, divide the factorial of the number of elements in each partition. For example, let n=8. Suppose 1st "box" contains 3 elements, 2nd contains 2 elements, 3rd contains 1 element and 4th contains 2 elements. Then the total number of mappings will be $\ \frac{8!}{3!2!1!2!} $. But this is only the number of mappings where 1st "box" and other boxes contain a given number of elements. Hence, for general, the answer is $$\ \sum { \frac { n! }{ p!q!r!s! }  } $$ and $$\ p+q+r+s=n,\quad p,q,r,s\in  \mathbb{N} $$
To find the answer in a closed form, consider the expression $$\ n!{ \left( \frac { x }{ 1! } +\frac { { x }^{ 2 } }{ 2! } +\frac { { x }^{ 3 } }{ 3! } +.... \right)  }^{ 4 } $$
The co-efficient of $\ {x}^{n} $ would be the sum of elements whose general term is $\ { \frac { n! }{ p!q!r!s! }  } $, where $\ p+q+r+s=n,\quad p,q,r,s\in  \mathbb{N} $ which is the required answer to the given question.
$$\ n!{ \left( \frac { x }{ 1! } +\frac { { x }^{ 2 } }{ 2! } +\frac { { x }^{ 3 } }{ 3! } +.... \right)  }^{ 4 }=n!{ \left( 1+\frac { x }{ 1! } +\frac { { x }^{ 2 } }{ 2! } +\frac { { x }^{ 3 } }{ 3! } +....-1 \right)  }^{ 4 }=n!{ \left( { e }^{ x }-1 \right)  }^{ 4 }$$
Now, the problem will be solved for general $\ r $ which can be equated to 4 for the above problem.
$$\ n!{ \left( { e }^{ x }-1 \right)  }^{ r }=n!\left( { e }^{ rx }-\binom{r}{1}{ e }^{ (r-1)x }+\binom{r}{2}{ e }^{ (r-2)x }+... \right)  $$
The answer to the above question would be the co-efficient of $\ {x}^{n}$ in the above expression. Thus the answer is
$$\ n!\left( \frac { { r }^{ n } }{ n! } -\binom{ r }{ 1 }\frac { { (r-1) }^{ n } }{ n! } +\binom{ r }{ 2 }\frac { { (r-2) }^{ n } }{ n! } +... \right) ={ (r) }^{ n }-\binom{ r }{ 1 }{ (r-1) }^{ n }+\binom{ r }{ 2 }{ (r-2) }^{ n }+...=\sum _{ k=0 }^{ r }{ {\binom{r}{k} ({-1})^{k}(r-k) }^{ n } }  $$
The same can be got by principle of inclusion and exclusion easily, but I thought this would be a rather "new" method. Hope it helps!
EDIT: For this problem, put r=4 in the above formula and then multiply by $\ \binom{5}{4} $
