poj-3682 King Arthur's Birthday Celebration
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?
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.
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.
1 1 1 0.5 0Sample Output
1.000 1.000 2.000 6.000
OJ-ID:
poj-3682
author:
Caution_X
date of submission:
20191031
tags:
math
description modelling:
有一个人每天抛一次硬币,直到抛出了K次正面向上才会停止,第i天的金钱花费是2*i-1,现在输入
K,问花费金钱的数学期望
major steps to solve it:
设E[i],F[i],E[i]表示抛出i次正面向上时的期望天数,F[i]表示第i天的花费金钱数学期望
(1)E[i]=1/p+W[i-1]
(2)F[i] = p*(F[i]-1 + 2 * E[i] -1)/*第i天正好得到正面向上*/ + (1-p)*(F[i] + 2 * (E[i]+1) -1) /*第i天没有得到正面向上*/
AC code:
#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("%.3f %.3f\n",E[n],F[n]); } return 0; }