poj3682 King Arthur's Birthday Celebration

King Arthur's Birthday Celebration
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 2212 Accepted: 664

Description

King Arthur is an narcissist who intends to spare no coins to celebrate his coming K-th birthday. The luxurious celebration will start on his birthday and King Arthur decides to let fate tell when to stop it. Every day he will toss a coin which has probability p that it comes up heads and 1-p up tails. The celebration will be on going until the coin has come up heads for K times. Moreover, the king also decides to spend 1 thousand coins on the first day's celebration, 3 thousand coins on the second day's, 5 thousand coins on the third day's ... The cost of next day will always be 2 thousand coins more than the previous one's. Can you tell the minister how many days the celebration is expected to last and how many coins the celebration is expected to cost?

Input

The input consists of several test cases.
For every case, there is a line with an integer K ( 0 < K ≤ 1000 ) and a real number p (0.1 ≤ p ≤ 1).
Input ends with a single zero.

Output

For each case, print two number -- the expected number of days and the expected number of coins (in thousand), with the fraction rounded to 3 decimal places.

Sample Input

1 1
1 0.5
0

Sample Output

1.000 1.000
2.000 6.000

Source

POJ Founder Monthly Contest – 2008.08.31, Soduku@POJ

又是个求期望的题,其实,今天发现期望题有些情况下可以通过递推求解,尤其是像本题类似几何分布的题。
本题是说每天扔一枚硬币,总计扔到k次正面停止,得到的金钱数第一天为1,第二天为3,第三天为5……
假设天数的期望为E(x),x可以由x位置(1-p)的概率到达,和x-1位置p的概率到达,所以就有E(x)=(1-p)E(x)+pE(x-1) +1,化简即可。第一问是多次的几何概型问题,几何概型就是一直做某件事,直到一次成功位置,几何概型的方差EX=1/p(证明可以通过数列求和得到,在此不赘述),则本题需要成功k次,则E(x)=x/p,完全满足几何概型的期望。
第二问是求硬币的期望。设为F(x),通过观察可以发现每天得到的金钱val和天数d关系有val=d*2-1,所以递推到下一天的金钱为d*2+1。所以有F(x)=p(F(x-1)+E(x-1)*2+1)+(1-p)(F(x)+E(x)*2+1),照样化简就可以了。
代码
#include <stdio.h>

double E[1005];
double F[1005];

int main()
{
	int i,j,n;
	double q,p;
	while(1)
	{
		scanf("%d",&n);
		if (n==0) break;
		scanf("%lf",&p);
		E[0]=0;
		F[0]=0;
		for (i=1;i<=n;i++)
		{
			E[i]=1/p+E[i-1];
			F[i]=F[i-1]+2*E[i-1]-2*E[i]+(1+2*E[i])/p;
		}
		printf("%.3lf %.3lf\n",E[n],F[n]);
	}
	return 0;
}


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值