HDU 5159 Card (概率求期望)

B - Card
Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit Status

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 {{ m{S}}_{ m{j}}} . 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.62

题意:x张牌,每张牌的值从1到x。依次取b张,注意不是一次性取,即取12和取21是两种情况。问所取的牌中值不同的牌的总和的期望为多少。输入一组数据t,然后t行,每行一个x一个b。Hint给出了第一个样例的解释,看了就能懂。
题解:首先x张牌依次取b张,因为每一次取都有x种情况,所以一共会出现x^b种情况,那么如果x-1张牌取b张牌就会出现(x-1)^b种情况,所以一张牌会有x^b-(x-1)^b种情况,就是指有这么多种情况包含这张牌,而且对于每张牌都是这样的。那么这张牌出现的概率为(x^b-(x-1)^b)/x^b。由概率求期望的公式可知,这张牌的期望为这张牌的值乘以这张牌出现的概率。那么总期望就为所有牌的值的和乘以牌出现的概率,因为每张牌出现的概率是相同的,所有牌的值的和为x*(x+1)/2,将每张牌的概率的公式化简可得1-((1-1/x)^b),相乘即可,得出最终公式(1-pow(1-1.0/x,b))*x*(x+1)/2。
#include <iostream>
#include <math.h>
#include <stdio.h>
using namespace std;
int main()
{
    int t,cas=1;
    double x,b;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lf%lf",&x,&b);
        printf("Case #%d: %.3lf
",cas++,(1-pow(1-1.0/x,b))*x*(x+1)/2);
    }
    return 0;
}


原文地址:https://www.cnblogs.com/Ritchie/p/5461404.html