If $n$ distinct balls are distributed to $r$ distinct boxes in such a way that each n ball can go any one of the r boxes. If $n$ distinct balls are distributed to $r$ distinct boxes in such a way that each n ball can go any one of the r boxes. Then find the probability that,
A) All boxes are occupied.
B) Exactly K boxes are occupied.

My attempt: Firstly I choose r many balls from $n$ and then distribute these to $r$ boxes one by one. Then the possible cases to occupy the r boxes is ( $n_{C_r}\times r!$), so the probability will be $\frac{n_{C_r}\times r!\times r^{n-r}}{r^n}$ but in this way I am not getting the correct answer. I think it can be done using Starling Number. But I am not getting what is the problem in my attempt. Can anyone please provide the explicite solution of this problem.

 A: You heavily overcount. Assume some distribution of the balls. If some box contains more than one ball your multiply count every combination (every ball can come as first as you "choose" it among the first $r$ balls or as a next one which is arbitrarily distributed).
The correct way to deal with this problem is to use the inclusion-exclusion principle or its substitute - the Stirling numbers of the second kind. Using the latter the number of ways to occupy exactly $K$ boxes can be computed as:
$$\binom rK{n \brace K}K!
$$
where $\binom rK$ counts the ways to choose $K$ occupied boxes and ${n \brace K}K!$ counts the ways to distribute $n$ balls between these $K$ boxes so that no box is empty.
The corresponding probability can be obtained dividing the number by $r^n$.
A: There are $r^n$ possible ways to distribute the balls.
There are ${(r-1)}^n$ ways to distribute the balls that avoid one particular box.
Of those, there are ${(r-2)}^n$ ways to distribute that avoid two particular boxes, and so on.  We have to deal with overlap, however.
For $K = 1$, there are only $r$ ways to have all the balls in the same box.  So $P(K=1) = \frac{r}{r^n}$.
For $K = 2$, there are ${r \choose 2}$ ways to choose which two boxes, but we care about the order, so it is $r(r-1)$ instead, and then given that choice, there are $2^n$ ways to arrange the balls in those two boxes, including the possibility of none in one of the two boxes.  So $$P(K\leq 2) = \frac{r(r-1) \cdot 2^n}{r^n}$$
Continuing in this way, the probability of all boxes being occupied is $1-P(K \leq (r-1))
$.
