Distribution of $n$ distinct objects into $r$ different boxes if empty boxes are not allowed or in each box at least one object is put is:
$$r^n - \dbinom{r}{1}(r-1)^n+ \dbinom{r}{2}(r-2)^n- \dbinom{r}{3}(r-3)^n + \cdots + (-1)^{r-1}\dbinom{r}{r-1}·1$$
It is a really daunting formula. I do not know how to prove it. It is given that this formula can be proved using principle of inclusions and exclusion and using set theory, but how?
Edit: This formula is also used to find the number of onto functions from a $A$ with $|A|=n$ to $B$ with $|B|=r$.
I would like to see the proof using elementary combinatorics and principle of inclusion and exclusion.
$\def\peq{\mathrel{\phantom{=}}{}}$For each $1 \leqslant k \leqslant r$, denote by $A_k$ the set of scenarios in which the $k$-th box is empty. The quantity to be found is$$ |\overline{A_1} \cap \cdots \cap \overline{A_r}| = |S| - |A_1 \cup \cdots \cup A_r|, $$ where $S$ is the set of all scenarios.
Now, by inclusion and exclusion principle,\begin{align*} |A_1 \cup \cdots \cup A_r| &= \sum_{1 \leqslant k \leqslant r} |A_k| - \sum_{k_1 < k_2} |A_{k_1} \cap A_{k_2}| + \cdots + (-1)^{j - 1} \sum_{k_1 < \cdots < k_j} |A_{k_1} \cap \cdots \cap A_{k_j}|\\ &\peq + \cdots + (-1)^{r - 1} |A_1 \cap \cdots \cap A_r|. \tag{1} \end{align*} Because for any $k_1 < \cdots < k_j$, the set $A_{k_1} \cap \cdots \cap A_{k_j}$ contains exactly all the scenarios in which the $k_1$-th, …, $k_j$-th boxes are empty, then$$ |A_{k_1} \cap \cdots \cap A_{k_j}| = (r - j)^n. $$ Also, there are $\displaystyle\binom{r}{j}$ ways to select $j$ boxes from these $r$ boxes, thus\begin{align*} (1) &= \sum_{1 \leqslant k \leqslant r} (r - 1)^n - \sum_{k_1 < k_2} (r - 2)^n + \cdots + (-1)^{j - 1} \sum_{k_1 < \cdots < k_j} (r - j)^n + \cdots + (-1)^{r - 1} · 0^n\\ &= (r - 1)^n \sum_{1 \leqslant k \leqslant r} 1 - (r - 2)^n \sum_{k_1 < k_2} 1 + \cdots + (-1)^{j - 1} (r - j)^n \sum_{k_1 < \cdots < k_j} 1 + \cdots + (-1)^{r - 1} · 0^n\\ &= (r - 1)^n \binom{r}{1} - (r - 2)^n \binom{r}{2} + \cdots + (-1)^{j - 1} (r - j)^n \binom{r}{j} + \cdots + (-1)^{r - 1} · 0^n, \end{align*} and\begin{align*} &\peq |\overline{A_1} \cap \cdots \cap \overline{A_r}| = |S| - |A_1 \cup \cdots \cup A_r|\\ &= r^n - \left( (r - 1)^n \binom{r}{1} - (r - 2)^n \binom{r}{2} + \cdots + (-1)^{j - 1} (r - j)^n \binom{r}{j} + \cdots + (-1)^{r - 1} · 0^n \right)\\ &= r^n - (r - 1)^n \binom{r}{1} + (r - 2)^n \binom{r}{2} + \cdots + (-1)^j (r - j)^n \binom{r}{j} + \cdots + (-1)^r · 0^n\\ &= r^n - (r - 1)^n \binom{r}{1} + (r - 2)^n \binom{r}{2} + \cdots + (-1)^j (r - j)^n \binom{r}{j} + \cdots + (-1)^{r - 1} · 1^n. \end{align*}