UVa 10288

From Algorithmist
Jump to navigation Jump to search

10288 - Coupons[edit]

Summary[edit]

There are 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[edit]

Suppose, you already have distinct coupons. Let denote the expected number of boxes you still need to collect the remaining coupons.

With probability the next coupon will be useless to you, and with probability it will be of the kind, which you don't yet have. Or, mathematically:

, .

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

Gotchas[edit]

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

An Alternative Explanation[edit]

As above, suppose you already have distinct coupons. Let denote the number of cereal boxes that you need to collect the remaining coupons. Let denote the number of cereal boxes that you need to collect 1 of the coupons that you don't have yet.

Based on these definitions, the following equation holds:

Using to denote expectation, we have:

But is a geometric random variable with probability of success . The expectation of a geometric random variable is

.

Plugging this into the formula above and unrolling the recursion yields the same formula given in the previous section:

Input[edit]

1
2
3
4
5
10
20
30
31
32
33

Output[edit]

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