King Arthur's Birthday Celebration (POJ-3682)期望DP(二项分布)

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

题意:亚瑟王掷一枚硬币,概率p正面向上,概率1-p反面朝上,现在亚瑟王要掷k次正面朝上,第i次掷硬币时花费2∗i−1。问:期望要掷多少枚硬币才能达到k次正面朝上,以及达到k次正面朝上时的花费。
思路:Ei表示已经掷到i次正面朝上了,还要再掷多少次才能到达k次正面朝上。
           Ei=p*Ei+1+(1−p)*Ei+1
           很容易解出期望掷k/p次达到目的。
           Fi表示已经掷到i次正面朝上了,还要再花费多少才能到达k次正面朝上。
           Fi=p*Fi+1+(1−p)*Fi+2∗Ei−1
           +2∗Ei−1是因为从i次正面朝上的时候掷下一次的时候是在第Ei次掷的。
           解出花费为(n*n+n)p*p−n/p

AC代码:

#include <stdio.h>
#include <string>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include<iomanip>
const int maxn=1010;
const int inf=0x3f3f3f3f;
using namespace std;
int main()
{
    int n;
    double p;
    while(cin>>n)
    {
        if(n==0)
            break;
        cin>>p;
        cout<<fixed<<setprecision(3)<<n/p<<" "<<fixed<<setprecision(3)<<((n*n+n)/p-n)/p<<endl;
    }
    return 0;
}


 

发布了212 篇原创文章 · 获赞 18 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_43846139/article/details/104059179