题目链接:Coins
Description
Alice and Bob are playing a simple game. They line up a row of nn identical coins, all with the heads facing down onto the table and the tails upward.
For exactly mm times they select any kk of the coins and toss them into the air, replacing each of them either heads-up or heads-down with the same possibility. Their purpose is to gain as many coins heads-up as they can.
Input
The input has several test cases and the first line contains the integer $t (1 \le t \le 1000)$ which is the total number of cases.
For each case, a line contains three space-separated integers $n$, $m (1 \le n, m \le 100)$ and $k (1 \le k \le n)$.
Output
For each test case, output the expected number of coins heads-up which you could have at the end under the optimal strategy, as a real number with the precision of $3$ digits.
Sample input
1 | 6 |
Sample output
1 | 0.500 |
Solution
题意
桌上放置着 $n$ 个反面朝上的硬币,有 $m$ 此操作,每次选择任意 $k$ 个硬币抛向空中,每个硬币落到桌子后正面朝上和反面朝上的概率相同,求最终正面朝上的硬币的期望。
题解
概率DP 期望
期望 = 概率 * 总数
$f(i, j)$ 表示为抛 $i$ 枚硬币 $j$ 枚硬币朝上的概率。则有 $f(i, j) = 0.5 \times f(i - 1, j) + 0.5 \times f(i - 1, j - 1)$,其中 $f(i, 0) = 2 ^ i$ 。
$DP(i, j)$ 表示第 $i$ 次操作后有 $j$ 枚正面朝上的硬币的概率,则反面硬币的个数为 $n - j$。
如果 $n - j >= k$,那么只要在反面朝上的硬币中选择 $k$ 枚抛即可。抛完 $k$ 枚硬币后有 $0 \sim k$ 枚硬币可能会正面朝上,递推方程为 $DP(i + 1, j + l) = \sum_{l = 0}^{k} DP(i, j) * f(k, l)$。
如果 $n - j < k$,那么除了要抛 $n - j$ 枚反面朝上的硬币,还要选择 $k - (n - j)$ 枚正面朝上的硬币,这样最后正面朝上的个数是本来正面就朝上的 $j-(k-(n-j))$ 枚加上抛了之后朝上的 $l (0\le l\le k)$ 枚,递推方程为 $DP(i + 1, j - (k - (n - j)) + l) = \sum_{l = 0}^{k} DP(i, j) * f(k, l)$。
Code
1 |
|