题目链接: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:
设Xi代表分数为i的牌在b次操作中是否被选到,Xi=1为选到,Xi=0为未选到
那么期望EX=1*X1+2*X2+3*X3+…+x*Xx
Xi在b次中被选到的概率是1-(1-1/x)^b
那么E(Xi)= 1-(1-1/x)^b
那么EX=1*E(X1)+2*E(X2)+3*E(X3)+…+x*E(Xx)=(1+x)*x/2*(1-(1-1/x)^b)
代码如下:
#include <cstdio> #include <cstring> #include <cmath> int main() { int t; int x, b; int cas = 0; scanf("%d",&t); while(t--) { scanf("%d%d",&x,&b); double p = 1-pow(1-1.0/x,b); double n = x*(x+1.0)/2; double ans = n*p; printf("Case #%d: %.3lf\n",++cas,ans); } }
2:
dp思想,先预处理出dp[i][j]: i 个数 j 轮!
因为每个数字出现的总次数是相同的!
我们只要找出数字出现的总的有效次数!再乘上所有数的和,就可以求出所有数字的有效和,最后再除以总的组合数即可!
其实写两个案例我们就可以发现,每个数字的有效次数是相邻的i的j次方差!
因为每次都有(i-1)^j次方的组合里可以不含i个数字其中的一个!
例如:x = 3, b = 3时
我们有组合
(1,1,1)、(1,1,2)、(1,2,1)、(1,2,2)
(2,2,2)、(2,1,1)、(2,1,2)、(2,2,1)
这8个组合里不含数字 3
同理还有 8 个组合不含数字 1, 8 个不含数字 2 !
那么1,2,3出现的有效次数就是总的组合 num = x^b - (x-1)^b!
sn = x*(x+1)/2;
sum = sn*num;
代码如下:
#include <cstdio> #include <cstring> #define maxn 100010 double dp[maxn][6]; //dp[i][j]: i 个数 j 轮 void init() { for(int i = 1; i <= 5; i++) { dp[1][i] = 1; } for(int i = 2; i <= 100000; i++) { dp[i][1] = dp[i-1][1]+i; double cnt1, cnt2; for(int j = 2; j <= 5; j++) { cnt1 = cnt2 = 1; for(int k = 1; k <= j; k++) { cnt1 *= i; cnt2 *= i-1; } double cishu = (double)cnt1-cnt2;//次方差 double sum = (double)i*(1+i)/2.0; dp[i][j] = cishu*sum; } } } int main() { int x, b; int t; int cas = 0; init(); scanf("%d",&t); while(t--) { scanf("%d%d",&x,&b); double tol = 1; for(int i = 1; i <= b; i++)//x^b { tol*=x; } double ans = dp[x][b]/tol; printf("Case #%d: %.3f\n",++cas,ans); } return 0; } /* 99 2 3 3 3 1 3 3 1 3 2 100000 5 */