码迷,mamicode.com
首页 > 其他好文 > 详细

ATM Mechine 概率DP

时间:2017-08-19 10:54:45      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:from   eof   not   warning   data-   home   form   port   put   

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.

InputThe input contains multiple test cases. 
Each test case contains two numbers K and W. 
1K,W20001≤K,W≤2000OutputFor 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

求期望逆推,求概率顺推
分情况讨论,如果取钱成功。。如果取钱不成功。。。
注意钱在给定的范围内是等可能分布的
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<memory>
#include<bitset>
#include<string>
#include<functional>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;

#define MAXN 2009 

#define INF 0x3f3f3f3f

/*
概率DP 分为猜中和猜不中两部分计算
dp[i][j] 目前已知金钱范围为0-i 
    当前还有j次猜的机会
dp[i][j] =min( k从1到i 
1 + 取钱成功:(i-k+1)/(i) * dp[i-k][j] + k/(i)*dp[k][j - 1])
dp[1][x] = 1
dp[x][0] = INF
*/
double dp[MAXN][15];
double E(int v, int k)
{
    if (v == 0)
        return dp[v][k] = 0;
    else if (k == 0)
        return INF;
    else if (dp[v][k] > 0)
        return dp[v][k];
    else
    {
        dp[v][k] = INF;
        for (int i = 1; i <= v; i++)
        {
            dp[v][k] = min(dp[v][k], (double)( v - i +1)/( v + 1) * E(v - i, k) + (double)(i) / ( v + 1) *E(i - 1, k - 1) + 1.0);
        }
        return dp[v][k];
    }
}
int main()
{
    int k, w;
    while (~scanf("%d%d", &k, &w))
    {
    //    memset(dp, 0, sizeof(dp));
        w = min(w, 15);
        printf("%.6lf\n", E(k, w));
    }
}

 



ATM Mechine 概率DP

标签:from   eof   not   warning   data-   home   form   port   put   

原文地址:http://www.cnblogs.com/joeylee97/p/7395509.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!