HDU 1145So you want...(概率题目 dfs+思维)

So you want to be a 2n-aire?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 201    Accepted Submission(s): 146


Problem Description
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. If wrong, he quits with nothing. If correct, the prize is doubled, and he continues with the next question. 
After the last question, he quits with his prize. The player wants to maximize his expected prize. 
Once each question is asked, the player is able to assess the probability p that he will be able to answer it. 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. Input is 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 should be 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
 

                    题目大意: 像百万富翁的闯关游戏原理一样。但是百万富翁里面好像如果你不答的话就必须拿上当前现金走人,而这个题可以不走,keep the prize。每次你可以选择不答。也可以选择答题,答对,现金翻倍,错误则没钱。答对每个题目的概率的范围是p~1.给你n,p让你求每次都是最好的选择,然后可以得到的现金期望。

           解题思路:开始看了题目,不知所云。懂了意思之后也不知道如何下手。看了下题解,表示题解看了半天才懂。主要用到一个原理,每次把下次可以得到的最大现金期望算出来,然后算下百分比。看下自己是选择不答还是选择答题。
HDU 1145So you want...(概率题目 dfs+思维)_第1张图片
           
            题目地址:So you want to be a 2n-aire?

AC代码:
#include
#include
using namespace std;
double t,res;
double tmp,mo;
int n;

double dfs(int k,int money)
{
    if(k==n)
    {
        mo=0.5,tmp=2*money;
        if(t>mo) return (1+t)/2*tmp;
        return (1-mo)/(1-t)*(1+mo)/2*tmp+(mo-t)/(1-t)*money;
    }
    tmp=dfs(k+1,money*2);

    mo=money/tmp;  //是为了找出平衡点
    if(t>mo) return (1+t)/2*tmp;
    return (1-mo)/(1-t)*(1+mo)/2*tmp+(mo-t)/(1-t)*money;
}

int main()
{
    while((scanf("%d%lf",&n,&t))!=EOF)
    {
        if(n==0&&t==0) break;
        res=dfs(1,1);
        printf("%.3lf\n",res);
    }
    return 0;
}


你可能感兴趣的:(概率)