Flipping Coins Gym - 101606F

本文介绍了一种硬币翻转游戏,玩家通过有限次数的操作最大化正面向上的硬币数量的数学期望。使用动态规划的方法,定义状态dp[i][j]表示进行i次操作后有j个硬币正面向上的概率,并给出完整的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:Here’s a jolly and simple game: line up a row of N identical coins, all with the heads facingdown onto the table and the tails upwards, and for exactly K times take one of the coins, toss itinto the air, and replace it as it lands either heads-up or heads-down. You may keep all of thecoins that are face-up by the end.Being, as we established last year, a ruthless capitalist, you have resolved to play optimally towin as many coins as you can. Across all possible combinations of strategies and results, whatis the maximum expected (mean average) amount you can win by playing optimally?

给你一些硬币,硬币的初始状态都是正面向下,反面向上,现在你可以每次任取一个硬币把它抛向空中,你可以进行k次操作,问你k次操作之后硬币正面向上的最大数学期望。

思路:一共进行k次操作,每次我们都可以任取一个硬币把它抛一次,由于题目中说是让求最大的数学期望,那么我们抛的时候取正面向下的可以得到最大的数学期望,我们定义一个状态dp[i][j],表示抛i次其中有j个硬币正面向上的概率。

那么我们可以得到转移方程:

dp[i+1][j]+=dp[i][j]*0.5;

dp[i+1][j+1]+=dp[i][j]*0.5;

当j=n时

dp[i+1][n]+=dp[i][n]*0.5;

dp[i+1][n-1]+=dp[i][n]*0.5;

注意dp[0][0]=1;

#include <bits/stdc++.h>
using  namespace  std;
const int maxn=450;
double dp[maxn][maxn];
int main()
{
    int n,k;
    cin>>n>>k;
    for(int i=0; i<maxn; i++)
        for(int j=0; j<maxn; j++)
            dp[i][j]=0;
    dp[0][0]=1;
    for(int i=0; i<k; i++)
    {
        for(int j=0; j<n; j++)
        {
            dp[i+1][j]+=dp[i][j]*0.5;
            dp[i+1][j+1]+=dp[i][j]*0.5;
        }
        dp[i+1][n]+=dp[i][n]*0.5;
        dp[i+1][n-1]+=dp[i][n]*0.5;
    }
    double ans=0;
    for(int i=1; i<=n; i++)
        ans+=(dp[k][i]*i);
    printf("%.10f\n",ans);


}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值