lightOJ 1317 Throwing Balls into the Baskets

时间:2022-05-16 01:51:59

lightOJ  1317  Throwing Balls into the Baskets(期望)  解题报告

题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88890#problem/A

题目:

Description

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 M 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 N 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 K 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-6 will 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个篮筐,一共打了k轮,每轮每个人可以投一个球,每个球投进的概率都是p,求k轮后,投中的球的期望是多少?

分析:

因为每个人投进的概率都是相同的,所以期望也是相同的。因此只需要求出第一轮的期望就可以了,总期望=k*第一轮的期望。

代码:

 #include<cstdio>
#include<iostream>
using namespace std; int t,n,m,k;
double p,ans;
int a[][]; void init()
{
a[][]=;
a[][]=;
for(int i=;i<;i++)
{
a[i][i]=;
a[i][]=;
for(int j=;j<i;j++)
a[i][j]=a[i-][j]+a[i-][j-];
}
} double count(int j)
{
double b=1.0;
for(int i=;i<j;i++)
b=b*p;//投中的期望
for(int i=;i<n-j;i++)
b=b*(1.0-p);//没投中的期望
return b*j*a[n][j];
} int main()
{
int c=;
scanf("%d",&t);
init();
while(t--)
{
scanf("%d%d%d%lf",&n,&m,&k,&p);
ans=0.0;//小数
for(int i=;i<=n;i++)
ans+=count(i);//第一轮的期望
printf("Case %d: %.7lf\n",c++,ans*k);
}
return ;
}