HDU 5159 Card (概率DP):http://acm.hdu.edu.cn/showproblem.php?pid=5159
题面描述:
2 2 3 3 3
Case #1: 2.625 Case #2: 4.222HintFor the first case, all possible combinations BieBie can pick are (1, 1, 1),(1,1,2),(1,2,1),(1,2,2),(2,1,1),(2,1,2),(2,2,1),(2,2,2) For (1,1,1),there is only one kind number i.e. 1, so the sum of different score is 1. However, for (1,2,1), there are two kind numbers i.e. 1 and 2, so the sum of different score is 1+2=3. So the sums of different score to corresponding combination are 1,3,3,3,3,3,3,2 So the expectation is (1+3+3+3+3+3+3+2)/8=2.625
题目大意:
从1-x中选择b个数,计算其数字不同的所有数字之和的概率。
题目分析:
要求和的期望,根据期望的基本定理:和的期望=各部分期望的和。
即:E(sum)=E(1)+E(2)+...+E(x);
考虑每一个数的贡献,b次中一个数一次都不出现的概率为(x-1)^b,而总的排列次数是x^b,所以每个数都有贡献的概率为x^b-(x-1)^b,则推广到全部,公式即为:(x^b-(x-1)^b)*(1+2+3+...+x)/(x-1)^b=(1-((x-1)/x)^b)*(1+x)*x/2.
代码实现:
#include <iostream> #include <cstdio> #include <math.h> using namespace std; int main() { int t,x,b; int casenum=0; scanf("%d",&t); while(t--) { scanf("%d%d",&x,&b); double a=1.0*(x-1)/x; a=pow(a,b*1.0); double ans=1-a; double sum=1.0*(x+1)*x/2; ans=sum*ans;; printf("Case #%d: %.3lf\n",++casenum,ans); } return 0; }