GCJ--Millionaire (2008 APAC local onsites C)

Problem

You have been invited to the popular TV show "Would you like to be a millionaire?".Of course you would!

The rules of the show are simple:
  • Before the game starts, the host spins a wheel of fortune todetermine P, the probability of winning each bet.
  • You start out with some money: X dollars.
  • There are M rounds of betting. In each round, you can bet anypart of your current money, including none of it or all of it. The amount is not limited to whole dollars or whole cents.

    If you win the bet, your total amount of money increases by the amount you bet. Otherwise, your amount of money decreases by the amount you bet.

  • After all the rounds of betting are done, you get to keep yourwinnings (this time the amount is rounded down to whole dollars) only if you have accumulated $1000000 or more. Otherwise you get nothing.

Given M, P and X, determine your probability of winning at least $1000000 if you play optimally (i.e. you play so that you maximize yourchances of becoming a millionaire).

Input

The first line of input gives the number of cases, N.

Each of the following N lines has the format "M P X", where:

  • M is an integer, the number of rounds of betting.
  • P is a real number, the probability of winning each round.
  • X is an integer, the starting number of dollars.

Output

For each test case, output one line containing "Case #X: Y", where:

  • X is the test case number, beginning at 1.
  • Y is the probability of becoming a millionaire, between 0 and 1.

Answers with a relative or absolute error of at most 10-6 will be considered correct.

Limits

1 ≤ N ≤ 100
0 ≤ P ≤ 1.0, there will be at most 6 digits after the decimal point.
1 ≤ X ≤ 1000000

Small dataset

1 ≤ M ≤ 5

Large dataset

1 ≤ M ≤ 15


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define MAX_M 25
using namespace std;
typedef long long ll;
int M,X;
int T,temp;
double P;
double dp[2][(1<<MAX_M)+1];
void solve()
{
	int n=1<<M;
	double *prv =dp[0], *nxt=dp[1];
	memset(prv,0,sizeof(double)*(n+1));
	prv[n]=1.0;
	for(int r=0;r<M;r++)
	{
		for(int i=0;i<=n;i++)
		{
			int jub=min(i,n-i);
			double t=0.0;
			for(int j=0;j<=jub;j++)
			{
				t=max(t,P*prv[i+j]+(1-P)*prv[i-j]);
			}
			nxt[i]=t;
		}
		swap(prv,nxt);
	}
	int i=(ll) X*n/1000000;
	printf("Case #%d: %.6lf\n",temp-T,prv[i]);
}
int main()
{
	freopen("C-large-practice.in","r",stdin);
	freopen("output.out","w",stdout);
	cin>>T;
	temp=T;
	while(T--)
	{
		scanf("%d%lf%d",&M,&P,&X);
		solve();
	}
	return 0;
 } 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值