Problem Description
Alice is going to take all her savings out of the ATM(Automatic Teller Machine). Alice forget how many deposit she has, and this strange ATM doesn’t support query deposit. The only information Alice knows about her deposit is the upper bound is K RMB(that means Alice’s deposit x is a random integer between 0 and K (inclusively)).
Every time Alice can try to take some money y out of the ATM. if her deposit is not small than y, ATM will give Alice y RMB immediately. But if her deposit is small than y, Alice will receive a warning from the ATM.
If Alice has been warning more then W times, she will be taken away by the police as a thief.
Alice hopes to operate as few times as possible.
As Alice is clever enough, she always take the best strategy.
Please calculate the expectation times that Alice takes all her savings out of the ATM and goes home, and not be taken away by the police.
Input
The input contains multiple test cases.
Each test case contains two numbers K and W.
1≤K,W≤2000
Output
For each test case output the answer, rounded to 6 decimal places.
Sample Input
1 1
4 2
20 3
Sample Output
1.000000
2.400000
4.523810
Author
ZSTU
Source
2016 Multi-University Training Contest 5
题目意思:Alice忘记了自己银行里存了多少钱,只记得在[0,k]之间。每次取钱如果余额足够就出钱,否则警告一次,警告超过w次就会把你抓起来,在不想被警察抓起来的前提下,Alice采取最优策略,求期望取钱多少次能知道自己存了多少钱。
官方给出的题解
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
#define LL long long
#define INF 0x3f3f3f3f
const double inf=1e12;
double f[2016][15]; //最多的钱,剩余可用的次数
double bank(int m,int w){
if(m==0) return 0;
if(w==0) return inf;
if(f[m][w]>0) return f[m][w];
double ans=inf;
for(int i=1;i<=m;i++){ //枚举实际的钱数
ans=min(ans,bank(i-1,w-1)*i/(m+1)+bank(m-i,w)*(m-i+1)/(m+1)+1);
//为什么加1.。考虑的很久,菊苣已解释貌似就知道了。
//试了一次之后,产生了两种步数期望,还要加上我试的这一次。
//orz巨啊。。。
}
return f[m][w]=ans;
}
int main(){
int k,w;
fill(f[0],f[0]+2016 * 15,0.0);
while(scanf("%d %d",&k,&w)!=EOF){
w=min(w,15);//为什么15呢?因为二分查找2^15>2000
printf("%.6f\n",bank(k,w));
}
return 0;
}