暑假集训.8(概率Dp)

概率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

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值