码迷,mamicode.com
首页 > 其他好文 > 详细

POJ3682King Arthur's Birthday Celebration(数学期望||概率DP)

时间:2017-12-17 15:03:02      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:i++   ase   oat   action   one   lan   expected   tar   pen   

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块钱,第二天3块钱,第三天5块,直到他抛到硬币向上的数量为K。

求天数期望和钱期望。

思路:

天数期望dp很好求,公式一推,代码一敲。钱期望money没想出来,我开始想难道是用第x天结束的期望乘第x天的钱,累加,直到x天的期望乘钱小于0.0001。但是参考了下别人的公式,反正自己是没想出来。

天数:dp[i]=dp[i]*(1-p)+dp[i-1]*p+1,化简:dp[i]=dp[i-1]+1/p;

money:money[i] = p(money[i-1]+ 2 *(dp[i-1]+1)-1) + (1-p)(money[i] + 2 * (dp[i]+1)-1)。化简:money[i]=money[i-1]+2*dp[i-1]-2*dp[i]+(1+2*dp[i])/p;

问题:

巴斯卡分布?二项分布???给数学跪了

 http://blog.csdn.net/nmfloat/article/details/50650489

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=1010;
double dp[maxn],money[maxn];
int main()
{
    int n;double p;
    while(~scanf("%d",&n)&&n){
        scanf("%lf",&p);
        for(int i=1;i<=n;i++) {
            dp[i]=dp[i-1]+1/p;
            money[i]=money[i-1]+2*dp[i-1]-2*dp[i]+(1+2*dp[i])/p;
        }
        printf("%.3lf %.3lf\n",dp[n],money[n]);
    }return 0;
}

 

POJ3682King Arthur's Birthday Celebration(数学期望||概率DP)

标签:i++   ase   oat   action   one   lan   expected   tar   pen   

原文地址:http://www.cnblogs.com/hua-dong/p/8051914.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!