您的位置:首页 > 其它

hdu 5781 ATM Mechine 概率(期望)dp

2016-08-07 11:10 435 查看


ATM Mechine

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 1000    Accepted Submission(s): 502


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

 

Recommend

wange2014   |   We have carefully selected several similar problems for you:  5808 5807 5806 5805 5804 

 

Statistic | Submit | Discuss | Note

题意:一个人去取钱,但是他不知道自己存有多少钱。只知道钱数最多是m。他可以每次取出一定的钱数y,如果小于等于剩下的钱数,那么取出y元,否则报警一次。如果报警次数超过n,将会被警察带走。

现在这个人足够聪明,想要用最少的步数取出所有钱,每一步都是最优选择,问步数的期望。(1<=n,m<=2000)

解:比赛的时候,一看感觉O(n^3)时间复杂度太高,然后一直在想什么是最优选择,明明想对了,队友偏说什么二分,然后上去搞了半天,

然后就想不出来了...

因为尽量让取钱步数最少,所以使用的报警次数不会太多(就是说有太多机会是没有用的),打表可以验证:如果n>10,dp
[m]的结果和dp[10][m]的结果完全相同。

#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;

#define all(x) (x).begin(), (x).end()
#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)
#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)
#define mes(a,x,s)  memset(a,x,(s)*sizeof a[0])
#define mem(a,x)  memset(a,x,sizeof a)
typedef long long ll;
typedef pair<int, int> pii;
const int INF =0x3f3f3f3f;
const int maxn= 2000   ;
int n,m;
double dp[maxn+10][maxn+10];
bool cal[maxn+10][maxn+10];

double DP(int i,int j)
{
if(cal[i][j])  return dp[i][j];

if(!i||!j)
{
dp[i][j]=0;
cal[i][j]=1;
return dp[i][j];
}
if(j==1)//对于最大只有一元的情况,只用取1次。
{
dp[i][j]=1;
cal[i][j]=1;
return dp[i][j];
}

dp[i][j]=1;
double best=INF;
int ed=j;
if(i==1)  ed=1;//如果最多只能取一次,必须一次取一元,这样才能保证取出所有钱。
for(int num=1;num<=ed;num++)
{
double no=1.0*num/(j+1);
int ret=j-num;
double tmp=  no*DP(i-1,num-1)  +(1-no)*DP(i,ret);

if(tmp<best)
{
best=tmp;
}
}
dp[i][j]+=best;

cal[i][j]=1;
return dp[i][j];
}
int main()
{
std::ios::sync_with_stdio(false);
while(cin>>m>>n)
{
if(n>10)  n=10;
printf("%.6f\n",DP(n,m) );

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息