代码 学习

poj3682:数学期望,O(1)做法附推导过程

这几天一直在磨蹭这题..第一个答案很容易,但在第二个答案我无法算出来了,于是只好求助于Zayin.Zayin又求助于我们年级里面的一个研究生数学老师..而现在终于算出来了,我看了看,自己也推出来几次了,先看题:)

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

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

题意:
国王(一个自恋狂)迎来了他的生日,生日开始之时他每天都会抛一次硬币,正面向上的概率为P(= =b其实我很想吐槽怎么做到的),然后多天累计正面向上次数达到k次后,生日庆典结束。
生日庆典要钱的么。国王第一天要花掉1千个金币,第二天要花掉3千个金币,第三天要花掉5千个金币...第n天要花掉(2n-1)个金币。
求:期望举办生日庆典天数和期望花费金币数。

网上大多数都是DP做法,O(n),而且即使是O(1)做法也没给出过程..在此写个小小的推理过程...在此感谢Zayin和老师提供的思路。

第一个答案太简单,在这里我就不予推理了,在这里给出的是第二个答案的推导过程:

设P(t)为举办t天生日后结束的概率,则有:

P(t) = C(t - 1, k - 1) * (1 - P)^(t-k)*P^k;

又 sigma(t = 1,+∞) P(t) = 1

sigma(t = 1,+∞) C(t - 1, k - 1) * (1 - P)^(t-k)*P^k = 1

(p/(1 - p))^k * sigma(t = 1,+∞) C(t - 1, k - 1) * (1 - P)^t = 1

∴ sigma(t = 1,+∞) C(t - 1, k - 1) * (1 - P)^t = ((1 - p)/p)^k

得出:

E = sigma(t = 1,+∞) P * t^2 (说明:sigma(t = 1,n) 2 * t - 1 = n^2)

sigma(t = 1,+∞) C(t - 1, k - 1) * (1 - P)^(t-k) * P^k * t^2

(p/(1 - p))^k * sigma(t = 1,+∞) C(t - 1, k - 1) * (1 - P)^t * t^2

(p/(1 - p))^k * sigma(t = 1,+∞) C(t - 1, k - 1) * (1 - P)^t * t^2

(p/(1 - p))^k * sigma(t = 1,+∞) C(t - 1, k - 1) * (1 - P)^t * t^2

(p/(1 - p))^k * k * sigma(t = 1,+∞) C(t, k) * (1 - P)^t * t

在 sigma(t = 1,+∞) C(t, k) * (1 - P)^t * t 中,有:

sigma(t = 1,+∞) C(t, k) * (1 - P)^t * (t + 1) - sigma(t = 1,+∞) C(t, k) * (1 - P)^t

sigma(t = 1,+∞) C(t + 1, k) * (1 - P)^t - (1 /(1 - p)) * sigma(t = 1,+∞) C(t, k) * (1 - P)^(t + 1)

(k + 1) * sigma(t = 1,+∞) C(t + 1, k + 1) * (1 - P)^t - (1 /(1 - p)) * sigma(t = 1,+∞) C(t, k) * (1 - P)^(t + 1)

((k + 1)/(1 - p)^2) * sigma(t = 1,+∞) C(t + 1, k + 1) * (1 - P)^(t + 2) - (1 /(1 - p)) * sigma(t = 1,+∞) C(t, k) * (1 - P)^(t + 1)

代入化得:

((k + 1)/(1 - p)^2) * ((1 - p)/p)^(k + 2) - (1 /(1 - p)) * ((1 - p)/p)^(k + 1)

(k + 1) * ((1 - p)^k/p^(k + 2)) - ((1 - p)^k/p^(k + 1))

∴(p/(1 - p))^k * k * ((k + 1) * ((1 - p)^k/p^(k + 2)) - ((1 - p)^k/p^(k + 1)))

((k * (k + 1))/p^2) - k/p

(k^2 + k)/p^2 - k * p/p^2

(k^2 + k - k * p)/p^2

∴ E = (k^2 + k - k * p)/p^2
然后答案就是(k * (k + 1 - p) / (p * p))了。

以上过程如有纰漏或错误,请您务必在评论区提出。

附上AC代码:

/*Source Code

Problem: 3682		User: aclolicon
Memory: 180K		Time: 0MS
Language: C++		Result: Accepted
Source Code*/
#include
int main(){
	double k, p;
	while(scanf("%lf", &k) && k!=0){
		scanf("%lf", &p);
		printf("%.3lf %.3lfn", k/p, (k * (k + 1 - p) / (p * p)));
	}
	return 0;
}

啊啊,于是这题总算是搞定了。不过某种意义来说,我只是跟随大牛的脚步完成了推导过程,数学期望这方面还需要多加强化。

b-a
我还没有学会写个人说明!
查看“b-a”的所有文章 →

3 条评论

  1. 专业内容。就不假装也看懂了。

  2. 是在猿始森林吗?好像只有程序猿才能推算出来。

    • b-a

      不..算是基础了..

Leave a Reply

Your email address will not be published. Required fields are marked *

相关推荐