题目链接:LightOJ - 1248
Description
Given a dice with n sides, you have to find the expected number of times you have to throw that dice to see all its faces at least once. Assume that the dice is fair, that means when you throw the dice, the probability of occurring any face is equal.
For example, for a fair two sided coin, the result is 3. Because when you first throw the coin, you will definitely see a new face. If you throw the coin again, the chance of getting the opposite side is 0.5, and the chance of getting the same side is 0.5. So, the result is
$1 + (1 + 0.5 (1 + 0.5 …))$
$= 2 + 0.5 + 0.5^2 + 0.5^3 + …$
$= 2 + 1 = 3$
Input
Input starts with an integer $T (≤ 100)$, denoting the number of test cases.
Each case starts with a line containing an integer $n (1 ≤ n ≤ 10^5)$.
Output
For each case, print the case number and the expected number of times you have to throw the dice to see all its faces at least once. Errors less than $10^{-6}$ will be ignored.
Sample Input
1 | 5 |
Sample Output
1 | Case 1: 1 |
Solution
题意
给定一个 $n$ 面的骰子,每个面出现的概率相同,现在要所有的面都至少出现一次,求投掷次数的期望。
思路
期望DP
期望DP一般是倒推的。
设 $dp[i]$ 为已经出现了 $i$ 个面,还需要投掷次数的期望值。
那么每次投掷只有两种情况:出现已经出现过的面、出现未出现的面。前者概率为 $\frac{i}{n}$,后者概率为 $\frac{n - i}{n}$。
则状态转移方程为 $dp[i] = (dp[i] + 1) \frac{i}{n} + (dp[i + 1] + 1) \frac{n - i}{n}$
化简得 $dp[i] = dp[i + 1] + \frac{n}{n - i}$
$dp[n] = 0$,倒推即可。
1 |
|
其实是满足几何分布的。
第一个出现的面可以是 $1, 2, 3, …, n$,有 $n$ 个。
如果第一个出现的面是 $1$,那么第二个出现的面可以是 $2, 3, …, n$,有 $n - 1$ 个。
…
第一个面第一次出现的概率为 $p_1 = \frac{n}{n}$
第二个面第一次出现的概率为 $p_2 = \frac{n - 1}{n}$
第三个面第一次出现的概率为 $p_3 = \frac{n - 2}{n}$
…
第 $i$ 个面第一次出现的概率为 $p_i = \frac{n - i + 1}{n}$
几何分布的期望 $E(X) = \frac{1}{p}$
所以所有面至少出现一次的期望为 $\sum_{i=1}^n \frac{1}{p_i} = \sum_{i=1}^n \frac{n}{n - i + 1}$。
注:几何分布指在 $n$ 次伯努利试验中,试验 $k$ 次才得到第一次成功的机率。
1 |
|