5212: Coins I
时间限制: 1 Sec 内存限制: 128 MB
提交: 114 解决: 69
[提交] [状态] [讨论版] [命题人: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
概率dp
用当前dp推出下一个状态的概率,最后求加权和
代码相信都看的懂
#include<bits/stdc++.h>
#define ll long long
using namespace std;
double dp[233][233],p[233],c[233][233];
void init()
{
c[0][0] = p[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-1] + c[i-1][j];
}
for(int i=1;i<=100;i++)p[i] = p[i-1]/2;
}
int main()
{
init();
int t,n,m,K;
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++)
{
if(!dp[i][j])continue;
for(int k = 0;k<=K;k++)
{
if(n-j>=K)dp[i+1][j+k] += dp[i][j]*c[K][k]*p[K];
else dp[i+1][j-(K-(n-j))+k] += dp[i][j]*c[K][k]*p[K];
}
}
}
double ans = 0;
for(int i=1;i<=n;i++)ans+=dp[m][i]*i;
printf("%.3lf\n",ans);
}
return 0;
}