Let f(n, a) be the maximum possible expected winnings if the player has to answer questions and the initial amount is .
If n = 0, the player keeps the initial amount, that is, f(0, a) = a.
The probability of answering a question correctly is , where t \le p \le 1. If the player answers the first question correctly, there remain n - 1 questions, and the prize doubles to . With probability 1 - p, the player gives an incorrect answer and loses everything. Therefore, the expected winnings after the first question is
If this value exceeds the current amount , it is advantageous to continue the game; otherwise, the player should stop. Thus, the expected winnings after making the decision to continue is
Since the probability is uniformly distributed over the interval , then
If t = 1, then the probability of a correct answer equals one, and the player should answer all questions, obtaining a final prize of 2^n.
Example
Let us consider the third test, where n = 2, t = 0.6. The initial amount a = 1.
Let us compute the value of f(1, 2) using f(0, 4):
Let us compute the value of f(2, 1) using f(1, 2):
Algorithm implementation
The function integral computes the value of the integral
for given real numbers and . When t = 1, the guessing probability equals one, so the value of the integral I(a, k) is set to max(a, k).
Below is the region whose area corresponds to the value of the integral \int_{t}^{1} max(a, kp)\,dp:
Let us find the intersection point of the lines y = a and y = kp:
Let temp = a / k. We consider the value of the integral \int_{t}^{1} max(a, kp)\,dp as the sum of two parts: \int_{t}^{temp} a\,dp + \int_{temp}^{1} kp\,dp. Depending on the position of the point relative to and , we compute the value of the integral I(a, k).
If t \le temp \le 1, then \int_{t}^{temp} a\,dp + \int_{temp}^{1} kp\,dp =
If temp \le t, then \int_{t}^{temp} a\,dp + \int_{temp}^{1} kp\,dp = \int_{t}^{1} kp\,dp = k \cdot (1 - t \cdot t) / 2.
If temp > 1, then \int_{t}^{temp} a\,dp + \int_{temp}^{1} kp\,dp = \int_{t}^{1} a\,dp = a \cdot (1 - t).
double integral(double a, double k)
{
double s = 0, temp = a / k;
if (t == 1) return max(a,k);
if (temp > 1) return a * (1 - t);
if (temp >= t) s = a * (temp - t);
else temp = t;
s += k * (1 – temp * temp) / 2;
return s / (1 - t);
}The function f recursively computes the value of f(n, a) = \begin{cases} I(a, f(n - 1, 2a)), n > 0 \\ a, n = 0 \end{cases}.
double f(int n, int a)
{
if (!n) return a;
double k = f(n - 1,2 * a);
return integral(a,k);
}The main part of the program. Read the input data, and print the value of f(n, 1).
while(scanf("%d %lf",&n,&t),n + t)
printf("%.3lf\n",f(n,1));