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

本文探讨了一个有奖竞猜问题,玩家需要在一系列问题中选择是否回答,回答正确奖金翻倍,错误则奖金清零。通过最优策略求解玩家的最大期望奖金,并详细解释了临界概率的计算方法。

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.5
1 0.3
2 0.6
24 0.25
0 0

Output for sample input

1.500
1.357
2.560
230.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;
		else
			ans = (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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值