GCJ--Millionaire (2008 APAC local onsites C)

来源:互联网 发布:阿里云 网站端口 编辑:程序博客网 时间:2024/06/05 19:52

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 25using 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