Original question:
The player starts with a prize of $,
and is asked a sequence of n ques-
tions. 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 and his prize. The player wants to maximize his expected prize. Once Each question are asked, the player is able to assess the probability p that he'll be able to answer it. For each question, we assume this p is a random variable uniformly distributed over the range T. 1.
Input
Input is a number of lines, each with a Numbers:an integer 1≤n≤30, and a real 0≤t≤1. Input
is terminated by a line containing ' 0 0 '. This is should not being 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
Effect:
In a TV entertainment program, you start with 1 dollars. The host will ask you n questions, and every time you hear the question there are two choices: one is to give up answering the question, quit the game, take the bonus, and answer the question. If the answer is correct, the bonus doubles; If the answer is wrong, the game is over and you can't get a penny. If you answer all n questions correctly, you will take all of the 2 N dollars and become a 2 N-dollar Millionaire. Of course, there is a risk in answering the question. Every time you hear a question, you can immediately estimate the probability of the correct answer. As the moderator randomly asks questions, you can assume that the probability of the correct answer for each question is evenly distributed between T and 1. Enter the integer n and real number T (1≤n≤30,0≤t≤1), and your task is to find out the expected value of the bonus amount taken under the optimal strategy. The optimal strategy here is to make the bonus expectations as large as possible.
Code:
#include <bits/stdc++.h>
using namespace std;
Double powtwo[31],t;
int n;
int main ()
{
Ios::sync_with_stdio (false);
Powtwo[0]=1;
for (int i=1;i<=30;i++)
powtwo[i]=powtwo[i-1]*2;
Double ex;
while (cin>>n>>t,n+t)
{
if (ABS (1-T) <1e-9)
{
cout<<fixed<< Setprecision (3) <<PowTwo[n]<<endl;
Continue;
}
Ex=powtwo[n];
for (int i=n-1;i>=0;i--)
{
double p0=powtwo[i]/ex;
if (p0<=t)
ex= (1+t)/2*ex;
else
{
double p1= (p0-t)/(1-T);
ex=powtwo[i]*p1+ (1+p0)/2*ex* (1-P1);
}
}
Cout<<fixed<<setprecision (3) <<ex<<endl;
}
return 0;
}
Answer:
This problem will not do, by chance found in Rujia's purple book to explain the mathematical expectations of the example above is the problem. This question is really a good question ah, and not too good to think.
The following answers the purple book from Rujia.
Assuming you just started the game, if you give up, the bonus is 1; What is the expected bonus if you answer?
The 1th question is the probability p correlation, and and answer the problem behind the case of correlation. That
Select "Answer question 1th" after the expected bonus = p * Maximum expected bonus after 1 questions correctly
Note that the "maximum expected bonus after 1 answers" is irrelevant to this p, suggesting that we use the idea of recursion,
Use d[i] to indicate "the maximum expected bonus after I answer the question", plus "do not answer" when the situation, you can get: If the 1th question is correct
Probability is P, maximum value of expected bonus = max{2 0, p*d[1]}
This is deliberately written in 2 0, emphasizing that this is "right after 0 to give up" the final prize money received.
The above analysis can be generalized to the general situation, but one thing to note: So far, it has been assumed that P is known,
and P is actually not fixed, but is evenly distributed within the t~1. According to the definition of continuous probability, d[i] is conceptually equal to
Max{2^i, P*d[i+1]} points on the p=t~1. Don't be afraid of the word "integral", because while conceptually this is a product
, but the basic knowledge is still needed to implement the specific solution.
Because of the existence of the max function, two situations need to be discussed, namely pxd[i+1]<2 I and pxd[i+1]≥2 i.
Make P 0 =max{t, 2 i/d[i+1]} (add a max because according to the topic, p≥t), then:
P < P0, pxd[i+1]<2 I, so "no answer" is better, the expected bonus equals 2 I.
P≥p0, the "answer" is better, the expected bonus equals d[i] times the average of P (D[i] as a constant is "proposed", that is (1+P0)/2xd[i+1].(averaging is actually the expectation of a uniform distribution, the expected value of the winning probability)
In the first case, the actual range of P is [t,p0], so the probability is p1= (p0-t)/(1-T). According to the full expectations of the public
D[i] = 2 Ixp1 + (1+P0)/2xd[i+1]x (1-P1).
The boundary is d[n] = 2 N, the reverse hand is introduced d[0] is the answer.