hdu4465 Candy(快速排列组合+概率)

Candy

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 679    Accepted Submission(s): 295
Special Judge


Problem Description
LazyChild is a lazy child who likes candy very much. Despite being very young, he has two large candy boxes, each contains n candies initially. Everyday he chooses one box and open it. He chooses the first box with probability p and the second box with probability (1 - p). For the chosen box, if there are still candies in it, he eats one of them; otherwise, he will be sad and then open the other box.
He has been eating one candy a day for several days. But one day, when opening a box, he finds no candy left. Before opening the other box, he wants to know the expected number of candies left in the other box. Can you help him?
 

Input
There are several test cases.
For each test case, there is a single line containing an integer n (1 ≤ n ≤ 2 × 10 5) and a real number p (0 ≤ p ≤ 1, with 6 digits after the decimal).
Input is terminated by EOF.
 

Output
For each test case, output one line “Case X: Y” where X is the test case number (starting from 1) and Y is a real number indicating the desired answer.
Any answer with an absolute error less than or equal to 10 -4 would be accepted.
 

Sample Input
  
  
10 0.400000 100 0.500000 124 0.432650 325 0.325100 532 0.487520 2276 0.720000
 

Sample Output
  
  
Case 1: 3.528175 Case 2: 10.326044 Case 3: 28.861945 Case 4: 167.965476 Case 5: 32.601816 Case 6: 1390.500000
 

有两个箱子,里面各放有糖n个,每天从两个之中任取一颗糖,突然有一天小孩取糖时发现一个盒子里空了,问另一个盒子里面现在有多少糖(求期望值)?

先说说我做这个题走过的的误区,我错误的以为题目里面说的那个the other box指的是取得概率为1-p那个,最后看了有一个解题报告的公式才知道算的是两个的期望值之和。这个题期望的公式好推,就是算排列组合的时候可能不知道问么算,取log()(这个是数学里面的ln可别以为是)饥渴,还原的时候用exp()。还有一点,第一个箱子为空时仍然要取一次糖,因为取不出来的时候才知道糖完了。

这个地方也要注意,为什么是C(n+k,k)而不是C(n+k+1,k)?k表示的是the other box取的糖的数目,我们在第一个箱子取了n+1次糖,可是那次取空时并没有取到糖果,所以我们算期望值时的概率还是要用C(n+k,k)


此处的c(m,n)是原作者写的一个快速排列组合函数

#include<stdio.h>
#include<math.h>
double p,logp,logq,lg[400002]={0},dsum[400002]={0};
//

double cac1(int n,int x)//计算 
{
	double sum=0;
	int i;
	sum+=(n+1)*logp;
	sum+=x*logq;
	sum+=dsum[n+x]-dsum[n]-dsum[x];
	return sum;
}
double cac2(int n,int x)//计算 
{
	double sum=0;
	int i;
	sum+=(n+1)*logq;
	sum+=x*logp;
	sum+=dsum[n+x]-dsum[n]-dsum[x];
	return sum;
}
int main()
{
	int i,j,n,m,x,C=1;
	double sum;
	for(i=2;i<=400000;i++)//打表
	{
		lg[i]=log(i*1.0);
		dsum[i]=dsum[i-1]+lg[i];
	}
	while(scanf("%d%lf",&n,&p)!=EOF)
	{
		logp=log(p);
		logq=log(1-p);
		sum=0;
		for(x=0;x<n;x++)
		{
		sum+=(exp(cac1(n,x))+exp(cac2(n,x)))*(n-x);
		}
		printf("Case %d: %.6f\n",C++,sum);
	}
	return 0;
} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值