2017乌鲁木齐ICPC: A. Coins(概率DP)

版权声明:本文为博主原创文章,你们可以随便转载 https://blog.csdn.net/Jaihk662/article/details/82666352

 

A. Coins

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 t (1 \le t \le 1000)t(1≤t≤1000) which is the total number of cases.

For each case, a line contains three space-separated integers nn, m (1 \le n, m \le 100)m(1≤n,m≤100) and k (1 \le k \le n)k(1≤k≤n).

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 33 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枚硬币并同时投掷(结果50%正面50%反面),求m轮投掷之后正面朝上的硬币数量期望(你当然想让这个期望尽可能大从而每次尽可能选择反面朝上的硬币进行投掷)

思路:

m和m和k范围都特别小,dp[x][y]表示x轮投币之后反面朝上的硬币数量期望

最后答案就是

\small \sum_{i=0}^ndp[m][i]*(n-i)

不用担心中间数据过大损失精度,精度要求其实不高,不要低估double的能力哦

#include<stdio.h>
#include<algorithm>
using namespace std;
double dp[105][105], C[105][105], er[105] = {1};
int main(void)
{
	double ans;
	int T, n, m, k, i, j, p;
	for(i=0;i<=102;i++)
		C[i][0] = 1;
	for(i=1;i<=102;i++)
	{
		er[i] = er[i-1]*2;
		for(j=1;j<=i;j++)
			C[i][j] = C[i-1][j]+C[i-1][j-1];
	}
	scanf("%d", &T);
	while(T--)
	{
		scanf("%d%d%d", &n, &m, &p);
		for(i=0;i<=m;i++)
		{
			for(j=0;j<=n;j++)
				dp[i][j] = 0;
		}
		dp[0][n] = 1;
		for(i=0;i<=m-1;i++)
		{
			for(j=0;j<=n;j++)
			{
				for(k=0;k<=p;k++)
				{
					if(j<=p)
						dp[i+1][k] += dp[i][j]*C[p][k]/er[p];
					else
						dp[i+1][j-p+k] += dp[i][j]*C[p][k]/er[p];
				}
			}
		}
		ans = 0;
		for(i=0;i<=n;i++)
			ans += (n-i)*dp[m][i];
		printf("%.3f\n", ans);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Jaihk662/article/details/82666352