POJ3682 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?

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

数学问题 数学期望 递推

水道期望题,防止自己彻底忘掉期望是个啥……

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<cstdio>
 5 #include<cmath>
 6 using namespace std;
 7 const int mxn=10010;
 8 double f[mxn],g[mxn];
 9 double p;
10 int n;
11 int main(){
12     int i,j;
13     while(scanf("%d",&n)!=EOF && n){
14         scanf("%lf",&p);
15         for(i=1;i<=n;i++){
16             f[i]=1.0/p+f[i-1];
17             g[i]=(g[i-1]+2*(f[i-1]+1)-1)+(1-p)*(2*(f[i]+1)-1)/p;
18         }
19         printf("%.3f %.3f
",f[n],g[n]);
20     }
21     return 0;
22 }
原文地址:https://www.cnblogs.com/SilverNebula/p/6706860.html