ATM Mechine 概率DP

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. 
1≤K,W≤20001≤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));
    }
}
				
时间: 2024-10-07 07:45:46

ATM Mechine 概率DP的相关文章

HDU 5781 ATM Mechine(概率DP求期望)

传送门 ATM Mechine Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 376    Accepted Submission(s): 165 Problem Description Alice is going to take all her savings out of the ATM(Automatic Teller Mach

HDU-5781 ATM Mechine(概率DP)

题目大意:某个未知整数x等概率的分布在[0,k]中.每次你都可以从这个整数中减去一个任意整数y,如果x>=y,那么x=x-y,操作次数累计加1:否则,将会受到一次错误提示.当错误提示超过w次,将会对你的人生产生影响.现在,你的任务是将x逐步变为0,求最少操作次数的期望值.题目分析:概率DP求期望.定义状态dp(k,w)表示整数分布在[0,k],错误提示次数上限为w时的最少操作次数的期望.则dp(k,w)=min(p1*dp(k-y,w)+p2*(y-1,w-1))+1,其中p1.p2分别为k>

多校5 1001 HDU5781 ATM Mechine 记忆化搜索+概率

1 // 多校5 1001 HDU5781 ATM Mechine 2 // http://acm.hdu.edu.cn/search.php?field=problem&key=2016+Multi-University+Training+Contest+5&source=1&searchmode=source 3 // 记忆化搜索 4 // 每次二分,决策最优,所以最多查询11次 5 // dp[i][j] 当前能确定i元情况下,还能被警告j次 6 // 下次取k,实际剩余t

HDU 5236 Article(概率dp+贪心)

Problem Description As the term is going to end, DRD begins to write his final article. DRD uses the famous Macrohard's software, World, to write his article. Unfortunately this software is rather unstable, and it always crashes. DRD needs to write n

Codeforces 28C [概率DP]

/* 大连热身D题 题意: 有n个人,m个浴室每个浴室有ai个喷头,每个人等概率得选择一个浴室. 每个浴室的人都在喷头前边排队,而且每个浴室内保证大家都尽可能均匀得在喷头后边排队. 求所有浴室中最长队伍的期望. 思路: 概率dp dp[i][j][k]代表前i个浴室有j个人最长队伍是k的概率. 枚举第i个浴室的人数.然后转移的时候其实是一个二项分布. */ #include<bits/stdc++.h> using namespace std; int jilu[55]; double dp[

hdu 3076 ssworld VS DDD (概率dp)

///题意: /// A,B掷骰子,对于每一次点数大者胜,平为和,A先胜了m次A赢,B先胜了n次B赢. ///p1表示a赢,p2表示b赢,p=1-p1-p2表示平局 ///a赢得概率 比一次p1 两次p0*p1 三次 p0^2*p1,即A赢的概率为p1+p*p1+p^2*p1+...p^n*p1,n->无穷 ///即a_win=p1/(1-p);b_win=p2/(1-p); ///dp[i][j]表示a赢了j次,b赢了i次的概率 ///dp[i][j]=dp[i-1][j]*b_win+dp[

hdu 3853 概率DP 简单

http://acm.hdu.edu.cn/showproblem.php?pid=3853 题意:有R*C个格子,一个家伙要从(0,0)走到(R-1,C-1) 每次只有三次方向,分别是不动,向下,向右,告诉你这三个方向的概率,以及每走一步需要耗费两个能量,问你走到终点所需要耗费能量的数学期望: 回头再推次,思想跟以前的做过的类似 注意点:分母为0的处理 #include <cstdio> #include <cstring> #include <algorithm>

hdu4089(公式推导)概率dp

题意:有n人都是仙剑5的fans,现在要在官网上激活游戏,n个人排成一个队列(其中主角Tomato最初排名为m), 对于队列中的第一个人,在激活的时候有以下五种情况: 1.激活失败:留在队列中继续等待下一次激活(概率p1) 2.失去连接:激活失败,并且出队列然后排到队列的尾部(概率p2) 3.激活成功:出队列(概率p3) 4.服务器瘫:服务器停止服务了,所有人都无法激活了(概率p4) 求服务器瘫痪并且此时Tomato的排名<=k的概率. 解法:ans[i][j]表示i个人出于第j个位置要到目的状

poj3071(概率DP)

题意:淘汰赛制,2^n(n<=7)个队员.给出相互PK的输赢概率矩阵.问谁最有可能赢到最后. 解法:ans[i][j]表示第i个队员第j轮胜出的概率.赢到最后需要进行n场比赛.算出每个人赢到最后的ans[i][n].写出序号的二进制发现一个规律,两个队员i.j如果碰到,那么一定是在第get(i,j)场比赛碰到的.get(i,j)计算的是i和j二进制不同的最高位,这个规律也比较明显. 代码: /****************************************************