2017 ICPC Urumqi A.coins (期望)

题目链接: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 which is the total number of cases.

For each case, a line contains three space-separated integers , and .

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 digits.

Sample input

6
2 1 1
2 3 1
5 4 3
6 2 3
6 100 1
6 100 2

Sample output

0.500
1.250
3.479
3.000
5.500
5.000

Solution

题意

桌上放置着 个反面朝上的硬币,有 此操作,每次选择任意 个硬币抛向空中,每个硬币落到桌子后正面朝上和反面朝上的概率相同,求最终正面朝上的硬币的期望。

题解

概率DP 期望

期望 = 概率 * 总数

表示为抛 枚硬币 枚硬币朝上的概率。则有 ,其中

表示第 次操作后有 枚正面朝上的硬币的概率,则反面硬币的个数为

如果 ,那么只要在反面朝上的硬币中选择 枚抛即可。抛完 枚硬币后有 枚硬币可能会正面朝上,递推方程为

如果 ,那么除了要抛 枚反面朝上的硬币,还要选择 枚正面朝上的硬币,这样最后正面朝上的个数是本来正面就朝上的 枚加上抛了之后朝上的 枚,递推方程为

Code

#include <bits/stdc++.h>

using namespace std;
const int maxn = 110;
double dp[maxn][maxn];
double f[maxn][maxn];
int n, k, m;

void init() {
    f[0][0] = 1;
    for (int i = 1; i <= 100; ++i) {
        f[i][0] = pow(0.5, i);
        for (int j = 1; j <= 100; ++j) {
            f[i][j] = (f[i - 1][j] + f[i - 1][j - 1]) / 2.0;
        }
    }
}

int main() {
    init();
    int T;
    scanf("%d", &T);
    while (T--) {
        scanf("%d%d%d", &n, &m, &k);
        memset(dp, 0, sizeof(dp));
        dp[0][0] = 1;
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j <= n; ++j) {
                for (int l = 0; l <= k; ++l) {
                    if (n - j >= k) {
                        dp[i + 1][j + l] += dp[i][j] * f[k][l];
                    } else {
                        dp[i + 1][j + l - (k - (n - j))] += dp[i][j] * f[k][l];
                    }
                }
            }
        }
        double ans = 0;
        for (int i = 1; i <= n; ++i) {
            ans += 1.0 * i * dp[m][i];
        }
        printf("%.3f\n", ans);
    }
    return 0;
}
全部评论

相关推荐

评论
点赞
收藏
分享
牛客网
牛客企业服务