ACM-ICPC 2017 Asia Urumqi:A. Coins 概率dp

问题 A: Coins I

时间限制: 1 Sec  内存限制: 128 MB
提交: 107  解决: 63
[提交] [状态] [讨论版] [命题人:admin]

题目描述

Alice and Bob are playing a simple game. They line up a row of n identical coins, all with the heads facing down onto the table and the tails upward.
For exactly m times they select any k 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.

 

输入

The input has several test cases and the first line contains the integer t (1 ≤ t ≤ 1000) which is the total number of cases.
For each case, a line contains three space-separated integers n, m (1 ≤ n, m ≤ 100) and k (1 ≤ k ≤ n).

 

输出

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.

 

样例输入

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

 

样例输出

0.500
1.250
3.479
3.000
5.500
5.000

 

[提交][状态]

题意:最开始有n枚朝下的硬币,每次投掷k个,投m次,求最佳策略下最终可能具有的朝上的硬币数量

最优策略即尽量投掷朝下的硬币,不够再投掷朝上的

概率dp  dp[i][j]表示投掷 i 次后,朝上硬币有 j 个的概率

在dp[i][j]的状态下转移,投掷的k枚硬币中朝上的硬币个数为 0~k 个,则 k 个中有 i 个朝上的概率为 C\binom{i}{k} \times 2^{-k}

先预处理组合数c[i][j],概率p[i]

代码:

#include <bits/stdc++.h>
using namespace std;
const  int maxn=200;
#define INF 0x3f3f3f;
const int mod=1e9+7;
typedef unsigned long long ll;
double dp[200][200],c[200][200];
double p[200];
void init()
{
    c[0][0]=1;
    for(int i=1; i<=100; i++){
        c[i][0]=1;
        for(int j=1; j<=i; j++)
            c[i][j] = c[i-1][j] + c[i-1][j-1];
    }
    p[0]=1;
    for(int i=1; i<=100; i++)
        p[i] = p[i-1] / 2;
}
int main()
{
    init();
    int t,n,m,d;
    cin>>t;
    while(t--){
        cin>>n>>m>>d;
        memset(dp,0,sizeof(dp));
        dp[0][0]=1;
        for(int i=0; i<=m; i++){
            for(int j=0; j<=n; j++){
                if(dp[i][j] == 0)  continue;
                for(int k=0; k<=d; k++){     //k为在(i,j)的状态下,投掷d枚硬币中有k枚朝上的概率
                    if(n - j >= d)           //朝下的硬币个数不小于d,n-j为当前朝下的硬币个数
                        dp[i+1][j+k] += dp[i][j] * c[d][k] * p[d];
                    else
                        dp[i+1][j-(d-(n-j))+k] += dp[i][j] * c[d][k] * p[d];
                }
            }
        }
        double ans = 0;
        for(int i=0; i<=n; i++)
            ans += dp[m][i] * i;   //计算期望,概率*个数
        printf("%.3lf\n",ans);
    }

    return 0;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值