UVA 10900 - So you want to be a 2n-aire?(概率)

来源:互联网 发布:刘胜军 知乎 编辑:程序博客网 时间:2024/05/22 08:05

Problem A: So you want to be a 2n-aire?

The player starts with a prize of $1, and is asked a sequence of nquestions. 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 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.

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.51 0.32 0.624 0.250 0

Output for sample input

1.5001.3572.560230.138

题意:有奖竞猜,n题,猜对奖金翻倍,猜错奖金归0。为最大奖金期望。

思路:思路想得很接近了,可是一直以为临界概率是0.5,然后顺推跑出来答案是错的,后面看题解才发现临界概率要由p[i + 1]去推算,要逆推才行

代码:

#include <stdio.h>#include <string.h>#include <math.h>const int MAXN = 35;double max(double a, double b) {return a - b >= 1e-9 ? a : b;}int n;double t, mi[MAXN];double solve() {if (fabs(1 - t) < 1e-9) return mi[n];double ans = mi[n];for (int i = n - 1; i >= 0; i --) {double f = mi[i] / ans;if (f <= t)ans = (1 + t) / 2 * ans;elseans = (f - t) / (1 - t) * mi[i] + (1 - f) / (1 - t) * (1 + f) / 2 * ans;}return ans;}int main() {mi[0] = 1.0;for (int ii = 1; ii <= 31; ii ++)mi[ii] = mi[ii - 1] * 2;while (~scanf("%d%lf", &n, &t) && n || t) {printf("%.3lf\n", solve());}return 0;}