Here’s a jolly and simple game: line up a row of N identical coins, all with the heads facing
down onto the table and the tails upwards, and for exactly K times take one of the coins, toss it
into the air, and replace it as it lands either heads-up or heads-down. You may keep all of the
coins that are face-up by the end.
Being, as we established last year, a ruthless capitalist, you have resolved to play optimally to
win as many coins as you can. Across all possible combinations of strategies and results, what
is the maximum expected (mean average) amount you can win by playing optimally?
Input
One line containing two space-separated integers:
N(1<=N<=400), the number of coins at your mercy;
K(1<=K<=400), the number of flips you must perform.
Output
Output the expected number of heads you could have at the end, as a real number. The output
must be accurate to an absolute or relative error of at most 10-6.
----------------------------------------------------------------------------------------------------------------------
题意:给出N个硬币,开始均反面朝上。每次挑出其中一个抛,连续K次,求正面朝上的最大数学期望。
----------------------------------------------------------------------------------------------------------------------
由于是求最大数学期望,所以每次抛硬币即要优先选择反面硬币
所以只有两种挑选硬币的情况:
1.正面数量为 0 ~ n-1 ,选择反面硬币抛,抛出结果正面数量比原本 +1 或 不变
2.正面数量为 n,只能够选择正面硬币抛,抛出结果正面数量比原本 -1 或 不变
----------------------------------------------------------------------------------------------------------------------
设 dp[i][j] 表示: 第 i 次抛硬币后, j 个硬币正面朝上的概率
1.当 j < n 时,dp[i][j]的概率一分为二,各给dp[i+1][j]和dp[i+1][j+1],即
for(int j=0;j<n;j++){ dp[i+1][j]+=dp[i][j]/2; dp[i+1][j+1]+=dp[i][j]/2; }
2.当 j == n 时,dp[i][j]的概率一分为二,各给dp[i+1][j]和dp[i+1][j-1],即
dp[i+1][n]+=dp[i][n]/2; dp[i+1][n-1]+=dp[i][n]/2;
如此即可求出n个硬币抛k次的各个正面朝上的概率,最后求数学期望即可
附:n=2,k=4时的dp转移表格:
1 #include <stdio.h> 2 #include <math.h> 3 #include <string.h> 4 #include <algorithm> 5 #include <iostream> 6 #include <queue> 7 #include <stack> 8 #include <functional> 9 #define INF 0x3f3f3f3f 10 using namespace std; 11 typedef long long ll; 12 double dp[410][410], ans; 13 int main(){ 14 int n, k; 15 while (~scanf("%d %d", &n, &k)){ 16 for (int i = 0; i <= 400; i++) 17 for (int j = 0; j <= 400; j++) 18 dp[i][j] = 0; 19 dp[0][0] = 1; 20 for (int i = 0; i < k; i++){ 21 for (int j = 0; j < n; j++){ 22 dp[i + 1][j] += dp[i][j] * 0.5; 23 dp[i + 1][j + 1] += dp[i][j] * 0.5; 24 } 25 dp[i + 1][n] += dp[i][n] * 0.5; 26 dp[i + 1][n - 1] += dp[i][n] * 0.5; 27 } 28 ans = 0; 29 for (int i = 1; i <= n; i++) 30 ans += i*dp[k][i]; 31 printf("%.8lf\n", ans); 32 } 33 return 0; 34 }
AC代码
原文地址:https://www.cnblogs.com/kang000/p/8571071.html