概率DP主要用于求解期望、概率等题目。

转移方程有时候比较灵活。

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

poj2096:Collecting Bugs

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <math.h>
typedef long long ll;
using namespace std;
#define mod 1000000007
double dp[][];
int n,s;
int main()
{
while(scanf("%d%d",&n,&s)!=EOF)
{
dp[n][s]=;
for(int i=n;i>=;i--)
{
for(int j=s;j>=;j--)
{
if(i==n&&j==s) continue;
dp[i][j]=(n*s+i*(s-j)*dp[i][j+]+(n-i)*j*dp[i+][j]+(n-i)*(s-j)*dp[i+][j+])/(n*s-i*j);
}
}
printf("%.4f\n",dp[][]);
}
return ;
}
//http://www.cnblogs.com/jackge/archive/2013/05/21/3091757.html

sdut2626题目: The number of steps

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <math.h>
typedef long long ll;
using namespace std;
#define mod 1000000007
int n;
double dp[][];
double a,b,c,d,e;
int main()
{
while(scanf("%d",&n)!=EOF&&n!=)
{
scanf("%lf%lf",&a,&b);
scanf("%lf%lf%lf",&c,&d,&e);
memset(dp,,sizeof(dp));
dp[n][]=;
for(int i=;i<=n;i++)
{
dp[n][i]=dp[n][i-]+;
}
for(int i=n-;i>=;i--)
{
for(int j=;j<=i;j++)
{
if(j==)
{
if(i==n) continue;
dp[i][j]=dp[i+][j]*a+dp[i+][j+]*b+1.0;
}
else
{
dp[i][j]=dp[i][j-]*e+dp[i+][j]*c+dp[i+][j+]*d+1.0;
}
}
}
printf("%.2lf\n",dp[][]);
}
return ;
}

HDU4405:

题目大意:

 
跳棋有0~n个格子,每个格子X可以摇一次色子,色子有六面p(1=<p<=6),概率相等,可以走到X+p的位置,有些格子不需要摇色子就可以直接飞过去。问从0出发到达n或超过n摇色子的次数的期望。
 
解题思路:
 
dp[i]表示从i出发到达最终位置的次数期望。
 
转移方程当i需要摇色子时,dp[i]=Σ(1+dp[i+j])(1<=j<=6);否则dp[i]=dp[jump[i]] 表示从i能够跳得到的最大位置。
 
预处理后面的6个位置,直接转移就行。
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <queue>
#include <vector>
#include <algorithm>
#include <queue>
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std;
int n,m;
int path[];
double dp[];
int main()
{
int xx,yy;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==&&m==) break;
memset(path,-,sizeof(path));
for(int i=;i<=m;i++)
{
scanf("%d%d",&xx,&yy);
path[xx]=yy;
}
for(int i=n;i>=;i--)
{
if(path[i]!=-)
{
int j=path[i];
if(path[j]!=-)
path[i]=path[j];
else path[i]=j;
}
}
for(int i=;i<;i++)
dp[n+i]=;
for(int i=n-;i>=;i--)
{
if(path[i]!=-)
dp[i]=dp[path[i]];
else
{
double tt=;
for(int j=;j<=;j++)
{
tt+=dp[i+j]*(1.0/6.0);
}
dp[i]=+tt;
}
}
printf("%.4f\n",dp[]);
}
return ;
}

概率dp入门的更多相关文章

  1. HDU 3853 LOOPS 概率DP入门

    LOOPS Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others)Total Sub ...

  2. HDU 4405:Aeroplane chess(概率DP入门)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=4405 Aeroplane chess Problem Description   Hzz loves ...

  3. poj 2096 Collecting Bugs 概率dp 入门经典 难度:1

    Collecting Bugs Time Limit: 10000MS   Memory Limit: 64000K Total Submissions: 2745   Accepted: 1345 ...

  4. 概率DP入门学习QAQ

    emmmm博客很多都烂尾了...但是没空写..先写一下正在学的东西好了 概率DP这东西每次考到都不会..听题解也是一脸懵逼..所以决定学习一下这个东东..毕竟NOIP考过...比什么平衡树实在多了QA ...

  5. 洛谷P2719 搞笑世界杯 题解 概率DP入门

    作者:zifeiy 标签:概率DP 题目链接:https://www.luogu.org/problem/P2719 我们设 f[n][m] 用于表示还剩下n张A类票m张B类票时最后两张票相同的概率, ...

  6. POJ 2096-Collecting Bugs(概率dp入门)

    题意: 有n种bug和s种系统bug,每天发现一种bug(可能已经发现过了)所有种bug被发现的概率相同,求所有bug被发现的期望天数. 分析: dp[i][j]发现i种bug,j种系统bug期望天数 ...

  7. HDU 3853-loop(概率dp入门)

    题意: r*c个方格,从(1,1)开始在每个方格可释放魔法(消耗能量2)以知,释放魔法后可能在原地.可能到达相邻的下面格子或右面格子,给出三者的概率 求要到达(R,C)格子,要消耗能量的期望值. 分析 ...

  8. hdu4405概率dp入门

    Aeroplane chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  9. hdu3853之概率dp入门

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/xingyeyongheng/article/details/25205693 LOOPS Time ...

随机推荐

  1. HTML5怎样在网页中使用摄像头功能

    怎样在网页中使用摄像头,html5越来越多的使用到实际工作中,那么他怎样调用摄像头呢进行视频聊天,视频监控等活动呢,今天为大家抛砖引玉,教大家怎样实现怎样利用html5实现摄像头视频监控功能.废话不多 ...

  2. 李洪强 - C语言8-Scanf函数

    C语言的scanf函数 一.变量的内存分析 (一)字节与地址 ①. 内存以字节为单位 每个字节都有自己的内存地址,根据地址就可以找到该字节.整个内存相当于一整个酒店,而酒店以房间为单位,在这里每个房间 ...

  3. [转帖] VS集成Qt环境搭建

    http://blog.sina.com.cn/s/blog_a6fb6cc90101gynd.html 用了这么久的Qt,IDE一直都是VS与Creator并用(实际开发以VS为主),至于哪个更好这 ...

  4. [转帖] CvMat,Mat和IplImage之间的转化和拷贝

    原文地址: http://blog.csdn.net/holybin/article/details/17711013 在OpenCV中Mat.CvMat和IplImage类型都可以代表和显示图像. ...

  5. Brief introduction to Scala and Breeze for statistical computing

    Brief introduction to Scala and Breeze for statistical computing 时间 2013-12-31 03:17:19  Darren Wilk ...

  6. Hibernate配置文件学习心得

    Hibernate配置文件在工程中十分重要,名称为Hibernate.cfg.xml;如下图: 在代码模式下图: 第一句由于没怎么改动过,所以至今不知道有什么作用: <property name ...

  7. Vertex Fetch Texture (VTF)

    http://www.opengl.org/wiki/Vertex_Texture_Fetch Vertex Texture Fetch     This article contains inacc ...

  8. Apache Spark源码走读之6 -- 存储子系统分析

    欢迎转载,转载请注明出处,徽沪一郎. 楔子 Spark计算速度远胜于Hadoop的原因之一就在于中间结果是缓存在内存而不是直接写入到disk,本文尝试分析Spark中存储子系统的构成,并以数据写入和数 ...

  9. 常用的PHP数据库操作方法(MYSQL版)

    常用的PHP数据库操作方法(MYSQL版) 作者: 字体:[增加 减小] 类型:转载 时间:2011-06-08   最近一直在折腾自己的网站首页,写的大部分PHP脚本都要用到和MYSQL数据库相关的 ...

  10. access violation at address General protection fault

    https://en.wikipedia.org/wiki/General_protection_fault In memory errors, the faulting program access ...