暑假集训.8(概率Dp)

概率DP 

概率DP主要用于求解期望、概率等题目,其实和高中概率题的求法也有许多相似之处,能用高中数学知识解决的完全可以用高  中思路直接解决,除此以外,也可以用动态规划求解转移方程,还可以用迭代的方式,有时候就需要比较灵活地运用了。

因此,咱们做概率题的优先考虑顺序为:

1)高中数学直接解决

2)动态规划求解转移方程

3)迭代法

一般来说:求概率是正推,求期望是逆推,通过题目可以体会到这点。

 

例1:Discovering Gold ( LightOj : 1030 ) 

Description

You probably have played the game "Throwing Balls into the Basket". It is a simple game. You have to throw a ball into a basket from a certain distance. One day we (the AIUB ACMMER) were playing the game. But it was slightly different from the main game. In our game we were Npeople trying to throw balls into identical Baskets. At each turn we all were selecting a basket and trying to throw a ball into it. After the game we saw exactly S balls were successful. Now you will be given the value of and M. For each player probability of throwing a ball into any basket successfully is P. Assume that there are infinitely many balls and the probability of choosing a basket by any player is 1/M. If multiple people choose a common basket and throw their ball, you can assume that their balls will not conflict, and the probability remains same for getting inside a basket. You have to find the expected number of balls entered into the baskets after turns.

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case starts with a line containing three integers N (1 ≤ N ≤ 16), M (1 ≤ M ≤ 100) andK (0 ≤ K ≤ 100) and a real number P (0  P ≤ 1)P contains at most three places after the decimal point.

Output

For each case, print the case number and the expected number of balls. Errors less than 10-6will be ignored.

Sample Input

2

1 1 1 0.5

1 1 2 0.5

Sample Output

Case 1: 0.5

Case 2: 1.000000

 

题意:

给定n个格子以及每个格子上的金子数,你从第一个格子出发,每次掷1-6的骰子,根据掷出的值前进。若当前位置 + 掷出的值 > n,则重新掷骰子,直到正好到达第n个格子时游戏结束。求获得最大金子数的期望。

 

思路:

期望要由后向前推,所以设dp[ i ]为到达 i 个格子获得金子的期望,先初始化dp[ i ] = 第i个格子的金子数。则有dp[ i ] = (dp[ i+1 ] / 6 + ... + dp[ i+6 ] / 6 ) + dp[ i ]。dp[ i ]+=dp[ i+j ] / min ( n-i , 6 ) 的意思是,每次都把第 i 个格子当作目前阶段的起点,那么此时在这个格子的概率为1,期望为dp[ i ] * 1 =dp [ i ],然后再加上前面的min ( n-i , 6 )个可能到达这个格子的期望,这几个格子的概率是相等的,同时需要注意最后的时候到达这个点不足6个的情况,所以就是min ( n-i , 6 )个。直至推到最初起点1的位置,dp[ 1 ] 即为所求。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
 
using namespace std;
 
int main()
{
    double dp[1001];
    int i,j,T,z,n;
    scanf("%d",&T);
    for(z=1;z<=T;z++)
    {
        scanf("%d",&n);
        memset(dp,0,sizeof(dp));
        for(i=1;i<=n;i++)
            scanf("%lf",&dp[i]);
        for(i=n-1;i>=1;i--)
            for(j=1;j<=6;j++)
                dp[i]+=dp[i+j]/min(n-i,6);
        printf("Case %d: %.9lf\n",z,dp[1]);
    }
    return 0;
}

还有几个模板题

Light OJ 1317  LightOJ 1248 Light OJ 1317

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值