So you want to be a 2n-aire?

Source: Internet
Author: User

Description

The gamer started with a $1 bonus and asked to answer n questions. For each problem, you can:

1) withdraw and keep the bonus
2) answer questions.
If an error occurs, exit and get nothing. If it is correct, double the bonus and continue to answer the next question.
After answering the last question, he received a bonus and quit. The gamer wants to maximize the bonus he expects.
Once a question is raised, the gamer can answer the question correctly with probability P. For each problem, assume that p is a random variable and the distribution range is T <= P <= 1.

Input

Input m rows. Each row has two numbers: integer N and real number t, 1 ≤ n ≤ 30, 0 ≤ T ≤ 1. N indicates the number of questions to be answered, and t indicates the lower limit of the probability that the player can answer the question correctly. The input ends with two zeros.

Output

The expected bonus for each input N, T, and output player when using the best strategy, retain three decimal places.

Sample Input

1 0.5
1 0.3
2 0.6
0 0

Sample output

1.500
1.357
2.560

From the very beginning, I had a problem with my thinking. Later I checked out the problem report from other people and finally found out that I was suitable for taking risks but not gambling ......

We may wish to set a [I] to indicate the expected results of the I-th question correctly. Obviously, what we need at the end is a [0], but let's put it first, let's first discuss whether we choose to answer questions or give up before we make the I + 1 question.

First, we can intuitively think that if we exit after I is done, we can get so much money as 2 ^ I. Assuming that the probability of correct question I + 1 is P, we naturally think of using P multiplied by "A value" to represent the expected result of the answer, if P is multiplied by the value greater than 2 ^ I, we will certainly choose to answer questions, because if so, the expected benefits will be greater than that of not answering questions. Now the question is, what is this "value? Possible maximum? Possible minimum? Or average (or expected )?

For a metaphor, the greatest value is the adventure, the minimum value is the coward, and the average value is the acmer who has received a good higher education. At the beginning, I became an adventure ......

Later, I thought it was true that the average value was only convincing in the statistics. Therefore, the so-called plays the best strategy in the question is to decide whether to answer questions or not to answer questions each time according to what we mentioned above. We only discuss P as a fixed value. If we discuss P as random, the average income of non-answer is obviously the same, because it has nothing to do with P, still 2 ^ I. If the answer is answered, the average returns should be (EP + 1)/2 * A [I + 1]. EP is the "watershed" we discussed earlier ", the expression is Ep = 2 ^ I/A [I + 1]. When P> EP, p * A [I + 1]> 2 ^ I, that is to say, if I answer this question correctly, I will be able to get a [I + 1] So much money. Then, the probability P of the correct answer is the expectation for the benefits of the I + 1 question, if this expectation is greater than 2 ^ I, the answer will be selected. Of course, the total benefit is calculated in the question. We can multiply the probability of the answer or not, that is, a [I] = (EP-T)/(1-T) * 2 ^ I + (t-EP)/(1-T) * (EP + 1)/2 * A [I + 1], this subvalue lists EP> T. For EP <= T, you can write an expression similar to that for EP <= T.

Now we can see that a [I + 1] is required for computing a [I]. What should we do? Calculate it backwards. So what is a [n? Obviously it is 2 ^ N, because the expectation of the gains after the n-th question is naturally the biggest gain.

 

#include<stdio.h>#include<string.h>#include<math.h>#define MX 35int N;double T, q[MX];int main(){    q[0] = 1;    for(int i = 1; i <= 30; i ++)         q[i] = 2 * q[i - 1];    while(1)    {          scanf("%d%lf", &N, &T);           if(!N)                 break;            if(fabs(1 - T) < 1e-9)                printf("%.3lf\n", q[N]);          else          {                int i, j, k;                double eq, f = 1, quit;                f = q[N];                for(i = N - 1; i >= 0; i --)                 {                     quit = q[i];                     eq = quit / f;                    if(eq <= T)                    f = (T + 1) / 2 * f;               else                       f = (eq - T) / (1 - T) * quit + (1 - eq) / (1 - T) * (eq + 1) / 2 * f;    }    printf("%.3lf\n", f);          }    }    return 0;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.