UVa 10288 - Coupons

10288 - Coupons

 * http://acm.uva.es/p/v102/10288.html

Summary
There are $$n$$ different kinds of coupons. Each box contains one coupon of random type. What's the

expected number of boxes you need in order to collect at least one coupon of every kind?

Explanation
Suppose, you already have $$n-k$$ distinct coupons. Let $$a_k$$ denote the expected number of boxes you still need to collect the remaining $$k$$ coupons.

With probability $${(n-k) / n}$$ the next coupon will be useless to you, and with probability $${k / n}$$ it will be of the kind, which you don't yet have. Or, mathematically:

$$a_k = 1 + {n-k \over n} a_k + {k \over n} a_{k-1}$$, $$a_0 = 0$$.

Simplifying, you can obtain this simple formula for the answer:

$$a_n = n \sum_{k=1}^n {1 \over k}$$

Gotchas

 * Standard 32-bit int's are not big enough for this problem, but 64-bit integers with gcd should be sufficient.

Input
1 2 3 4 5 10 20 30 31 32 33

Output
1 3 1 5 -  2  1 8 -  3   5 11 --   12   73 29 ---   252   3704479 71 ---   3879876    65960897707 119 ---    77636318760    1967151510157 124 -    2329089562800    3934303020314 129 -    4512611027925    4071048809039 134 -    4375865239200