UVa 10648 - Chocolate Box

10648 - Chocolate Box

 * http://acm.uva.es/p/v106/10648.html

Summary
Given $$m$$ distinguishable boxes and $$n$$ chocolates to put in those boxes with equal probability, what is the probability that at least one box is empty? This set of numbers can be calculated exactly by the Stirling Number of the Second Kind.

Explanation
The Stirling Number of the Second Kind (using Knuth's notation $$\{^n_m\}$$) counts the number of ways to partition a set of $$n$$ elements into $$m$$ non-empty sets. How does this solve our problem?

For a given $$n$$, each of the $$n^m$$ occur with equal probability. $$\{^n_m\}$$ calculates the number of sets, so the boxes would not be distinctive. e.g.: $$\{^3_2\}$$ = $$\{\{1,2\},\{3\}\},\{\{1\},\{2,3\}\},\{\{1,3\},\{2\}\}$$, but since the boxes are distinctive, we would have have to multiply it by $$2! = 2$$ to get all the ordering.

Now there are two (equivalent) ways to go about:

The first is calculating $$\{^n_m\}$$ - the number of ways to put $$n$$ distinctive chocolates into $$m$$ boxes - or having no empty boxes. Since the boxes are distinct, multiply it by $$m!$$ to get the total number of ways we can have no empty boxes. The distribution is equal, and every way happens with an equal probability, thus we can divide it by the total number of ways (which is simply $$n^m$$) to get the probability of having no empty boxes. To get the inverse of this, we subtract that probability from 1. (The sets are totally disjoint, afterall.)

The second is calculating $$\Sigma_{i = 1}^{m-1}\{^n_i\}$$ - the number of ways to put $$n$$ distinctive chocolates into $$1, 2, \ldots, m-1$$ boxes. This will count the number of ways there will be $$m-1,m-2,\ldots,1$$ empty boxes, respectively. For each way, we have to account for the fact that all the boxes are distinctive, so we multiply it by $$m!$$, but to factor out the double counting we put on all the empty boxes (since it doesn't matter), we have to divide it by $$(m-i)!$$ - the number of empty boxes. This means we are actually calculate $$m! \Sigma_{i = 1}^{m-1}(m-i)!\{^n_i\}$$. As with the first method, we can then divide it by the total number of ways (which is simply $$n^m$$) to get the probability.

Input
50 12 50 12 100 3 100 12 32 7 -1

Output
Case 1: 0.1476651 Case 2: 0.1476651 Case 3: 0.0000000 Case 4: 0.0019960 Case 5: 0.0500009