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.
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
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
题意:爱丽丝存了钱在某个银行,银行有个ATM机,奇怪的是这个ATM并不能查询余额。所幸的是,爱丽丝知道这个银行的存储上限K。也就是爱丽丝在这个银行存的钱一定是在[0,K]内。现在爱丽丝想从ATM取钱。如果取的钱小于等于余额那么爱丽丝可以顺利取出,如果取的钱大于了余额,那么ATM机就会发出警告。如果警告次数超过了M次那么警察就会将爱丽丝当做小偷带走。爱丽丝是聪明的她一定有办法可以将自己的钱全部取出来且不会被当做小偷抓走,并且爱丽丝会尽可能用最少的取钱次数。问:爱丽丝将自己钱取完的期望次数是多少?
分析:题面可以看得出来是个概率DP的模型,dp[i][j]表示存款的范围长度为i+1并且可以警告j次的取钱次数期望。那么状态转移为:
dp[i][j] = min(dp[i][j] , dp[i-k][j]+dp[k-1][j-1]+1)然而时间复杂度为O(K^2M)但是仔细观察我们可以知道。爱丽丝是聪明的也就是采用二分的方式是出现警告最多的方式。也就是LogK <= 15. 所以最多只会出现15次警告。那么复杂度就降下来了。
代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
double dp[2010][16];
double dfs(int w,int c)
{
if(dp[w][c] != -1) return dp[w][c];
if(w == 0) return 0;
if( c == 0) return 1e9;
double ans = 1e9;
double k = 1.0/(1.0+w);
for(int i = 1 ; i <= w ; i ++ )
ans = min(ans,i*k*dfs(i-1,c-1)+(w+1-i)*k*dfs(w-i,c)+1);
return dp[w][c]=ans;
}
int main()
{
int i,j,k,w;
for (i=0;i<2010;i++)
for (j=0;j<16;j++) dp[i][j]=-1;
while (scanf("%d%d", &k, &w)!=EOF) {
w=min(w,15);printf("%.6f\n", dfs(k,w));
}
return 0;
}