HDU 5159 Card(概率期望)

传送门

Card

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 791    Accepted Submission(s): 315
Special Judge


Problem Description
There are x cards on the desk, they are numbered from 1 to x. The score of the card which is numbered i(1<=i<=x) is i. Every round BieBie picks one card out of the x cards,then puts it back. He does the same operation for b rounds. Assume that the score of the j-th card he picks is  Sj  . You are expected to calculate the expectation of the sum of the different score he picks.
 

Input
Multi test cases,the first line of the input is a number T which indicates the number of test cases. 
In the next T lines, every line contain x,b separated by exactly one space.

[Technique specification]
All numbers are integers.
1<=T<=500000
1<=x<=100000
1<=b<=5
 

Output
Each case occupies one line. The output format is Case #id: ans, here id is the data number which starts from 1,ans is the expectation, accurate to 3 decimal places.
See the sample for more details.
 

Sample Input
       
       
       
       
2 2 3 3 3
 

Sample Output
       
       
       
       
Case #1: 2.625 Case #2: 4.222
Hint
For 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
 

Source
BestCoder Round #26
 
题目大意:

桌子上有x张牌,每张牌从1到x编号,编号为i(1<=i<=x)的牌上面标记着分数i , 每次从这x张牌中随机抽出一张牌,然后放回,执行b次操作,记第j次取出的牌上面分数是 Sj , 问b次操作后不同种类分数之和的期望是多少。


样例解释:

对于第一组数据所有牌型的组合为(1, 1, 1),(1,1,2),(1,2,1),(1,2,2),(2,1,1),(2,1,2),(2,2,1),(2,2,2)
对于(1,1,1)这个组合,他只有一种数字,所以不同数字之和为1
而对于(1,2,1)有两种不同的数字,即1和2,所以不同数字之和是3。
所以对应组合的不同数字之和为1,3,3,3,3,3,3,2
所以期望为(1+3+3+3+3+3+3+2)/8=2.625

解题思路:

我们先考虑一下每一个数出现的概率,首先我们计算有这 x 个数出现b次的总数肯定就是 x^b,那么x-1个数出现b次的总数就是 (x-1)^b:所以一个数出现的总数就是x^b-(x-1)^b,那么概率就是(x^b-(x-1)^b)/x^b,期望等于什么呢,期望等于的应该是每个数出现的概率乘以每个数的值,又因为期望具有线性关系:即E(ax1+bx2+...)=aE(x1)+bE(x2)+...

所以我们要求的期望就等于(x^b-(x-1)^b)/x^b*(1+2+3+...+x)化简得:

(1-((x-1)/x)^b)*(x+1)*x/2(最终公式)可以求了:

My Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;

int main()
{
    int T;
    double x, b;
    scanf("%d",&T);
    for(int cas=1; cas<=T; cas++)
    {
        scanf("%lf%lf",&x,&b);
        double sum = x*(x+1)/2;
        double tmp = 1-pow(1-1.0/x,b);
        ///cout<<tmp<<endl;
        printf("Case #%d: %.3lf\n",cas,sum*tmp);
    }
    return 0;
}



你可能感兴趣的:(HDU 5159 Card(概率期望))