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 to determine 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 any part 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 your winnings (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 your chances 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

Sample

In the first case, the only way to reach $1000000 is to bet everything in the single round.

In the second case, you can play so that you can still reach $1000000 even if you lose a bet. Here's one way to do it:

You have $600000 on the first round. Bet $150000.

If you lose the first round, you have $450000 left. Bet $100000.

If you lose the first round and win the second round, you have $550000 left. Bet $450000.

If you win the first round, you have $750000 left. Bet $250000.

If you win the first round and lose the second round, you have $500000 left. Bet $500000.

题意:

最开始你有x元钱,要进行M轮赌博。每一轮赢的概率为P,你可以选择赌与不赌,如果赌也可以将所持的任意一部分钱作为赌注(可以是整数,也可以是小数)。如果赢了,赌注将翻倍;输了赌注则没了。在M轮赌博结束后,如果你持有的钱在100万元以上,就可以把这些钱带回家。问:当你采取最优策略时,获得100万元以上的钱并带回家的概率是多少。

分析:

由于每一轮的赌注是任意的,不一定为整数,因而有无限种可能,所以即便想穷竭搜索也无从着手。但如果能化连续为离散,那么可能便也是有限的了。具体如下:假设前M-1轮的赌博后,还持有x'元。对于最后一轮,考虑的情况有3种。如果x' >= 100万,则没有必要再赌了即最后一轮赢的概率为0;如果50<= x' < 100万,只要参与赌博并且赌注 >= 50万则有赢的概率为P;如果x' < 50万,那么无论是否参与最后一轮的赌博,压的赌注是多少赢的概率必为0。我们不妨看一下倒数第二轮与最后一轮的关系,设在倒数第二轮时持有的钱为x。如果x >= 100万,赢的概率为1;如果x < 25万,即便最后两轮赌博都赢了总钱数必小于100万,所以赢的概率为0;否则,只要选择参与至少一轮赌博并且赌注至少25万则有赢得概率。假设倒数第二轮的赌注为y(y = 0 或 y >= 25万),则最后一轮持有的钱x' = (x + y)或x' = (x - y)。而倒数第二轮考虑的情况具体可以分为5种。综上,当参与M轮赌博时所需考虑的情况总共有2^m + 1种,可以通过dp解决。定义一个二维dp数组,dp[i][j] := 参与第i轮赌博,持有的钱所在模块为j并且采取最优策略时赢的概率。初始化:dp[n][1 << m] = 1,状态转移方程dp[i][j] = max(P * dp[i + 1][j + k] + (1 - P) * dp[i + 1][j - k] / 0 <= k <= min(j, n - j) )。时间复杂度O(m*2^2m)。

代码:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h> using namespace std;
typedef long long int LL;
int M , X ;
double P;
double dp[2][(1 << 15) + 1];
void solve()
{
int n = 1 << M;
double *pre = dp[0] , *nxt = dp[1];
memset(pre , 0 , sizeof(double) * (n + 1));
///memset(pre , 0 , sizeof(pre)); 这样初始化是不行的,因为pre为一个double型的指针,不是整个数组。
pre[n] = 1.0;///因:模块n对应的资金>= 100万
for(int r = 0 ; r < M; r++)///枚举第几轮
{
for(int i = 0 ; i <= n ; i++)///枚举当前是哪种状态
{
int step = min(i , n - i);///如果step大于n / 2 , 等会儿转移的时候可能会超过n
double t = 0.0;
for(int j = 0 ; j <= step ; j++)///枚举当前的所有可能走法
{
t = max(t , P * pre[i + j] + (1 - P) * pre[i - j]);///求出期望的最大值
}
nxt[i] = t;
}
swap(pre , nxt);///交换两个数组的值进行滚动
}
int i = (LL)X * n / 1000000;///找到X对应的是第几块
// for(int i = 0 ; i <= n ; i++)cout << '*' << pre[i] << endl;
printf("%.6lf\n" , pre[i]);
}
int main()
{
cin >> M >> P >> X;
solve();
return 0;
}

2008 APAC local onsites C Millionaire (动态规划,离散化思想)的更多相关文章

  1. GCJ 2008 APAC local onsites C Millionaire

    时间复杂度很大.dp[i][j]表示第i轮 j这种状态的概率. #include<cstdio> #include<cstring> #include<cmath> ...

  2. GCJ2008 APAC local onsites C Millionaire

    自己Blog的第一篇文章,嗯... 接触这道题,是从<挑战程序设计竞赛>这本书看来的,其实头一遍读题解,并没有懂.当然现在已经理解了,想想当初可能是因为考虑两轮的那张概率图的问题.于是决定 ...

  3. Code Jam 2008 APAC local onsites Problem C. Millionaire —— 概率DP

    题意: 你有X元钱,进行M轮赌博游戏.每一轮可以将所持的任意一部分钱作为赌注(赌注为0元表示这一轮不押),赌注可以是小数的,不是一定要整数.每一轮 赢的概率为P,赢了赌注翻倍,输了赌注就没了.如果你最 ...

  4. UVA 221 城市化地图(离散化思想)

    题意: 给出若干个栋楼俯视图的坐标和面积,求从俯视图的南面(可以视为正视图)看过去到底能看到多少栋楼. 输入第一个n说明有n栋楼,然后输入5个实数(注意是实数),分别是楼的左下角坐标(x,y), 然后 ...

  5. HDU5124:lines(线段树+离散化)或(离散化思想)

    http://acm.hdu.edu.cn/showproblem.php?pid=5124 Problem Description John has several lines. The lines ...

  6. NOIP 2008 传纸条(洛谷P1006,动态规划递推,滚动数组)

    题目链接:P1006 传纸条 PS:伤心,又想不出来,看了大神的题解 AC代码: #include<bits/stdc++.h> #define ll long long using na ...

  7. USACO 2008 Mar Silver 3.River Crossing 动态规划水题

    Code: #include<cstring> #include<algorithm> #include<cstdio> using namespace std; ...

  8. 此坑待填 离散化思想和凸包 UVA - 10173 Smallest Bounding Rectangle

    Smallest Bounding Rectangle Given the Cartesian coordinates of n(>0)2-dimensional points, write a ...

  9. 算法:贪心、回溯(su)、分治、动态规划,思想简要

    贪心算法: 只做出当前看来最好的选择,而不从整体考虑最优,他所作出的是局部最优解.使用该算法的前提是必须具备无后效性,即某个状态以前的选择不会影响以后的状态的选择,只与当前状态有关. 回溯算法: 本质 ...

随机推荐

  1. .net平台借助第三方推送服务在推送Android,IOS消息(极光推送_V3版本)最新

    最近刚从极光推送官网上看到V2版本要停用,不得不有重新写V3版本的.这里用到了 HTTP Basic Authentication http://www.cnblogs.com/pingming/p/ ...

  2. 整理下本周工作中遇到的疑问;uid/euid/suid;docker镜像管理

    1.系统中的父子进程关系,以及docker是如何处理的这种父子进程关系,线上问题发现,子进程长时间得不到退出. 2.调用system系统调用发生了啥事情,发现大量的页表拷贝. 3.通过shell命令通 ...

  3. 【bzoj4641】基因改造 特殊匹配条件的KMP

    题目描述 如果两个长度相等的字符串,如果存在一种字符的一一映射,使得第一个字符串的所有字符经过映射后与第二个字符串相同,那么就称它们“匹配”.现在给出两个串,求第一个字符串所有长度等于第二个字符串的长 ...

  4. 【题解】SCOI2006萌萌哒

    看到这题,首先想到\(n^{2}\)的暴力,就是用并查集暴力合并两个相等的点.但由于这样会导致反复地访问同一个操作,显然是不能够的.于是我们可以联想这题的特殊性质,就是互相连变的点都是一段一段的区间. ...

  5. POJ3461:Oulipo——题解

    http://poj.org/problem?id=3461 KMP板子,好久以前学过了,直接把板子粘上去即可. #include<cstdio> #include<cstring& ...

  6. BZOJ5321 & 洛谷4064 & LOJ2274:[JXOI2017]加法——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=5321 https://www.luogu.org/problemnew/show/P4064 ht ...

  7. BZOJ3534:[SDOI2014]重建——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=3534 https://www.luogu.org/problemnew/show/P3317 T国 ...

  8. BZOJ3673 & BZOJ3674 & 洛谷3402:可持久化并查集——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=3673 https://www.lydsy.com/JudgeOnline/problem.php? ...

  9. [学习笔记]FFT——快速傅里叶变换

    大力推荐博客: 傅里叶变换(FFT)学习笔记 一.多项式乘法: 我们要明白的是: FFT利用分治,处理多项式乘法,达到O(nlogn)的复杂度.(虽然常数大) FFT=DFT+IDFT DFT: 本质 ...

  10. ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) A map B贪心 C思路前缀

    A. A Serial Killer time limit per test 2 seconds memory limit per test 256 megabytes input standard ...