(POJ) 3682 - King Arthur's Birthday Celebration

King Arthur's Birthday Celebration

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3071 Accepted: 967

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,直到抛出k次正面为止结束,第一天抛硬币需花费1,第二天花费3,然后是5,7,9……以此类推,让我们求出抛硬币的天数的期望和花费的期望。

分析:
1.天数期望之间的递推关系——注意这个递推差值不是天为单位,递推间变化的是正面朝上的硬币数(期望不考虑天数具体值)。
设投出k个硬币正面朝上期望为E(k)天,那么该期望可以通过两种可能得到:A.投出了k-1个硬币正面朝上花费了E(k-1)天,再投出一个硬币正面朝上(概率为p,花费时间+1天);B.投出了k个硬币正面朝上花费了E(k)天,投出一个硬币反面朝上(概率为1-p,花费时间+1天)。分析的时候不能漏掉B情况,得到关系式:E(k)=p*(E(k-1)+1)+(1-p)*(E(k)+1),整理可得E(k)=E(k-1)+1/p,迭代或者数学归纳可知最终的E(k)=k/p。
P.S:换个角度理解,投硬币其实是一个完全独立的事件,正面朝上的概率是p,整体看来要出现k次正面朝上需要投硬币k/p次,每次投币花费一天,因此投出k个硬币正面朝上需要k/p天。
2.花费的期望:设投出k个硬币正面朝上花费期望为C(k),
整理关系式:C(k)=p* C(k-1) +(1-p)*C(k) + 2*E(k)-1
归纳可知最终:C(k)=(n*n+n)/(p*p)-n/p;


代码:
#include <cstdio>

using namespace std;

int main()
{
    double n,p;
    while(scanf("%d",&n)&&n)
    {
        scanf("%lf",&p);
        printf("%.3lf %.3lf\n",n/p,((n*n+n)/p*-n)/p);
    }
    return 0;
}


第二个关系式的推导:

设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))了。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值