ATM Mechine
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.
(今天考试的原题,只是改了题面)
小沈阳在小品里说过:“人生最痛苦的事情是人死了,钱还没花了”。
于是小宋(80岁)决定要将所有的储蓄从ATM机中取出花光。 小宋忘记了她有多少存款(银行卡密码她是记得的2333),这个奇怪的ATM不支持查询存款余额功能。小宋知道她存款的唯一信息是存款上限是K元,这意味着小宋的存款x是0到K之间的随机整数(包括K)。
每次小宋都可以尝试从ATM中拿出一些钱。 如果她要取的y元钱不大于她的存款,ATM将立即给小宋y元。 但如果她的存款小于y,小宋将收到ATM的警告。
如果小宋被警告超过w次,那么她将被警方带走,作为小偷。
小宋希望取钱次数期望最小。
由于小宋聪明,她总是采取最好的策略。
请计算小宋将所有储蓄从自动取款机中取出期望次数最小值是多少,并不得被警方带走。
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
[解题报告]
取完不单单是指他存折里没钱了,小宋需要得到确定的信息告诉他取完了。
譬如小宋知道存折里最多1元,取一次就取完了。
或者当取1元的时候被警告了,也表示他存折里没钱了。
考虑dp[i][j]表示当前小宋直到存折里钱数上限为i,还可被警告j次,取完钱的期望。
(小宋知道自己最多0元钱)
代码如下:
#include<cstdio>
#define max(x,y) ((x>y)?x:y)
#define min(x,y) ((x<y)?x:y)
#define INF 0x3f3f3f3f
#define N 2010
double dp[N][15];
int k,w;
int main()
{
for(int i=1;i<N;i++) dp[i][0]=INF;
for(int i=0;i<15;i++) dp[0][i]=0;
for(int i=1;i<N;i++)
for(int j=1;j<15;j++)
{
double ans=INF;
for(int k=1;k<=i;k++)
ans=min(ans,(i-k+1.0)/(i+1)*dp[i-k][j]+k/(i+1.0)*dp[k-1][j-1]+1);
dp[i][j]=ans;
}
while(~scanf("%d%d",&k,&w))
printf("%.6f\n",dp[k][min(w,11)]);
return 0;
}