UVa 10900 So you want to be a 2n-aire?

来源:互联网 发布:郑钧 私奔知乎 编辑:程序博客网 时间:2024/05/22 06:09

The player starts with a prize of $1,and is asked a sequence of n questions.For each question, he may• quit and keep his prize.• answer the question. Ifwrong, he quits with nothing.If correct, the prize is doubled,and he continues withthe next question.After the last question, he quitswith his prize. The player wants tomaximize his expected prize.Once each question is asked, theplayer is able to assess the probabilityp that he will be able to answerit. For each question, we assume that p is a random variable uniformly distributed over the range t..1.

Input

Input is a number of lines, each with two numbers: an integer 1 ≤ n ≤ 30, and a real 0 ≤ t ≤ 1. Inputis terminated by a line containing ‘0 0’. This line should not be processed.

Output

For each input n and t, print the player’s expected prize, if he plays the best strategy. Output shouldbe rounded to three fractional digits.

Sample Input

1 0.5

1 0.3

2 0.6

24 0.25

0 0

Sample Output

1.500

1.357

2.560

230.138

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

概率+神奇的思路~

设d[i]为第i步的时候的期望钱数。

因为d[i]会受到i之后的d的影响,所以要倒推;而每次答对的概率只有一个范围,所以要先判断范围内哪些概率要答题,那些不需要答题,然后计算就可以了。

哪里用double哪里用int一定要分清!!!

(CSDN又不能给自己点赞了,差评。)


#include<cstdio>#include<iostream>using namespace std;int n,num[31];double p,d[31];int main(){num[0]=1;for(int i=1;i<=30;i++) num[i]=num[i-1]*2;while(scanf("%d%lf",&n,&p)!=EOF && n){d[n]=num[n];for(int i=n-1;i>=0;i--){double p0=max(p,num[i]/d[i+1]),p1=(p0-p)/(1-p);d[i]=p1*num[i];d[i]+=d[i+1]*(1+p0)/2*(1-p1);}printf("%.3lf\n",d[0]);}return 0;}


1 0
原创粉丝点击