UVa 10288
From Algorithmist
Contents |
[edit] 10288 - Coupons
[edit] 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?
[edit] Explanation
Suppose, you already have n - k distinct coupons. Let ak 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:
, a0 = 0.
Simplifying, you can obtain this simple formula for the answer:
[edit] Gotchas
- Standard 32-bit int's are not big enough for this problem, but 64-bit integers with gcd should be sufficient.
[edit] An Alternative Explanation
As above, suppose you already have n - k distinct coupons. Let Xk denote the number of cereal boxes that you need to collect the k remaining coupons. Let Yk denote the number of cereal boxes that you need to collect 1 of the k coupons that you don't have yet.
Based on these definitions, the following equation holds:
Xk = Yk + Xk - 1
Using
to denote expectation, we have:
E(Xk) = E(Yk) + E(Xk - 1)
But Yk is a geometric random variable with probability of success p = k / n. The expectation of a geometric random variable is
1 / p = n / k.
Plugging this into the formula above and unrolling the recursion yields the same formula given in the previous section:
[edit] Input
1 2 3 4 5 10 20 30 31 32 33
[edit] 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

