Throwing Balls into the Baskets (概率 DP)

You probably have played the game "Throwing Balls into the Basket". It is a simple game. You have to throw a ball into a basket from a certain distance. One day we (the AIUB ACMMER) were playing the game. But it was slightly different from the main game. In our game we were N people trying to throw balls into identical Baskets. At each turn we all were selecting a basket and trying to throw a ball into it. After the game we saw exactly S balls were successful. Now you will be given the value of and M. For each player probability of throwing a ball into any basket successfully is P. Assume that there are infinitely many balls and the probability of choosing a basket by any player is 1/M. If multiple people choose a common basket and throw their ball, you can assume that their balls will not conflict, and the probability remains same for getting inside a basket. You have to find the expected number of balls entered into the baskets after turns.

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case starts with a line containing three integers N (1 ≤ N ≤ 16), M (1 ≤ M ≤ 100) and K (0 ≤ K ≤ 100) and a real number P (0  P ≤ 1)P contains at most three places after the decimal point.

Output

For each case, print the case number and the expected number of balls. Errors less than 10-6will be ignored.

Sample Input

2

1 1 1 0.5

1 1 2 0.5

Sample Output

Case 1: 0.5

Case 2: 1.000000

题意:

n个人 m个篮子 每一轮每个人可以选 m 个篮子中一个扔球  扔中的概率都是p 求k轮后所有篮子里面球数量的期望值

首先:

我们要知道每一次扔的时候,0个人扔进的概率,1个人扔进的概率…. 
由于最后不关心M个篮子里球的具体情况,所以M其实没有什么用 
只要区分扔不扔进去,至于扔到哪个篮子不管 、

有两种方法:

1、直接套公式


首先呢,这个题目是 n 个人投篮的独立实验

独立实验:在概率论中,把在同样条件下重复进行试验的数学模型称为独立试验序列概型

相关定理:

设在一次试验中,事件A发生的概率为p(0<p<1),则在n重独立试验中,事件A恰好发生 k 次的概率为:

(k=0,1,2...n)

推论:设在一次试验中,事件A首次发生的概率为p(0<p<1),则在伯努利试验序列中,事件A在第 k 次试验中才首次发生的概率为

  

(k=0,1,2...n )

二项分布

定义:在每次试验中只有两种可能的结果,发生或者不发生,而且两种结果发生与否互相对立,并且相互独立,与其它各次试验结果无关,事件发生与否的概率在每一次独立试验中都保持不变

一般地,在n次独立重复试验中,用ξ表示事件A发生的次数,如果事件发生的概率是p,则不发生的概率 q=1-p,N次独立重复试验中发生k次的概率是:

P(ξ=K)=    (k=0,1,2,3…n),

那么就说ξ服从二项分布,其中P称为成功概率,记作:ξ~B(n,p)。

(1)二项分布的期望:E(ξ)=np;

(2)二项分布的方差:D(ξ)=npq [3]  

几何分布

定义:

在第n次伯努利试验中,试验k次才得到第一次成功的机率,详细的说是:前k-1次皆失败,第k次成功的概率。

如果事件发生的概率是p,则不发生的概率q=1-p,则P(ξ=K) =  

具有这种分布列的随机变量,称为服从参数p的几何分布

1)几何分布的期望E(X)=  

(2)几何分布的方差D(X)=  


这个题目能用二项分布的原因是:

首先 n 个人投篮是独立发生的,产生的情况之有两种,投进、投不进,并且在这些事件中概率并不会发生变化

每一轮里每一个人投中的期望为p,n个人就为n*p,k轮之后的总期望为k*(n*p);

直接套用公式  N*K*P

Code:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<stack>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    double n,m,k;
    double p;
    int t;
    double ans=0;
    cin>>t;
    int flag=1;
    while(t--)
    {
        scanf("%lf %lf %lf %lf",&n,&m,&k,&p);
        ans=n*p*k;
        printf("Case %d:",flag++);
        printf(" %.6lf\n",ans);

    }
}

2、推递推关系


根据全期望公式

进行一轮球数量的期望值为      dp[1]*1+dp[2]*2+...+dp[n]*n     记为  ans

其中  dp[i] 表示 为 i个人扔中的概率 dp[i] = C(n, i)*p^i*(1-p)^(n-i)  ( 有 i 个人投中,就会有 n-i 个人不中,不中的概率是 1-p)

最终答案为  ans×k 


Code:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<stack>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
//typedef long long LL;
int c(int n,int m)  //  求阶层
{
    int ans=1;  // 打表的话可能会超
    for(int i=1;i<=m ;i++)
    {
        ans*=(n--);
        ans/=i;
    }
    return ans;
}
int main()
{
    double dp[102];
    double a[102],b[102];
    int t;
    int n,m,k;
    double p;
    int flag=1;
    cin>>t;
    while(t--)
    {
        scanf("%d %d %d %lf",&n,&m,&k,&p);
        a[0]=b[0]=dp[0]=1;
        for(int i=1;i<=n;i++) // 求 i^p,(n-i)^p 
        {
            a[i]=a[i-1]*p;  
            b[i]=b[i-1]*(1-p);
        }
        for(int i=1;i<=n;i++)  // 一轮中每个人的概率
            dp[i]=c(n,i)*a[i]*b[n-i];
        double ans=0;
        for(int i=1;i<=n;i++)
            ans+=(double)dp[i]*i; // 一轮中 期望值
        ans*=(double)k; // k 回合的期望值
        printf("Case %d: %.6lf\n",flag++,ans);
    }
}
 
  1.  

猜你喜欢

转载自blog.csdn.net/JKdd123456/article/details/81258621