概率DP求解例题
1,逆推状态:山东省赛2013年I题
Problem I: The number of steps
Description
Mary stands in a strange maze, the maze looks like a triangle(the first layer have one room,the second layer have two rooms,the third layer have three rooms …). Now she stands at the top point(the first layer), and the KEY of this maze is in the lowest layer’s leftmost room. Known that each room can only access to its left room and lower left and lower right rooms .If a room doesn’t have its left room, the probability of going to the lower left room and lower right room are a and b (a + b = 1 ). If a room only has it’s left room, the probability of going to the room is 1. If a room has its lower left, lower right rooms and its left room, the probability of going to each room are c, d, e (c + d + e = 1). Now , Mary wants to know how many steps she needs to reach the KEY. Dear friend, can you tell Mary the expected number of steps required to reach the KEY?
Input
Output
Please calculate the expected number of steps required to reach the KEY room, there are 2 digits after the decimal point.
Sample Input
3
0.3 0.7
0.1 0.3 0.6
0
Sample Output
3.41
思路:二维DP,d[i][j]表示在i行第j个的期望,
然后逆推,点有三种情况:
1)只能靠左走(最后一行):d[i][j] = 1;
2)能靠左走,能靠下一行的左,右走:d[i][j] = d[i][j-1]*c+d[i+1][j-1]*d + d[i+1][j]*e;
3)只能向下一层的左右走:d[i][j] = d[i+1[j-1]*a+d[i+1][j]*b;
#include <bits/stdc++.h>
#define repu(i,a,b) for(int i=a;i<b;i++)
using namespace std;
#define N 50 int main()
{
ios::sync_with_stdio(false);
int n;
while(cin>>n&&n)
{
double dp[N][N];
double a,b,c,d,e;
cin>>a>>b>>c>>d>>e;
memset(dp,0.00,sizeof(dp));
for(int i=n; i; i--)///µÚiÐÐ
{
for(int j = n+-i; j<=n; j++)///µÚj¸ö
{
if(i==n&&j==(n+-i))
continue;
else if(i==n)
dp[i][j] = j -;
else if(j==(n+-i))
dp[i][j] = dp[i+][j-]*a+dp[i+][j]*b+;
else
dp[i][j] = dp[i+][j-]*c+dp[i+][j]*d+dp[i][j-]*e+;
}
}
cout<<fixed<<setprecision()<<dp[][n]<<endl;
}
return ;
}
二,先算概率,后算期望
Description
ICPC
(International Cardboard Producing Company) is in the business of
producing cardboard boxes. Recently the company organized a contest for
kids for the best design of a cardboard box and selected M winners. There are N
prizes for the winners, each one carefully packed in a cardboard box
(made by the ICPC, of course). The awarding process will be as follows:
- All the boxes with prizes will be stored in a separate room.
- The winners will enter the room, one at a time.
- Each winner selects one of the boxes.
- The selected box is opened by a representative of the organizing committee.
- If the box contains a prize, the winner takes it.
- If
the box is empty (because the same box has already been selected by one
or more previous winners), the winner will instead get a certificate
printed on a sheet of excellent cardboard (made by ICPC, of course). - Whether there is a prize or not, the box is re-sealed and returned to the room.
The management of the company would like to know how many prizes will
be given by the above process. It is assumed that each winner picks a
box at random and that all boxes are equally likely to be picked.
Compute the mathematical expectation of the number of prizes given (the
certificates are not counted as prizes, of course).
Input

Output
first and only line of the output file should contain a single real
number: the expected number of prizes given out. The answer is accepted
as correct if either the absolute or the relative error is less than or
equal to 10 -9.
Sample Input
sample input |
sample output |
5 7 |
3.951424 |
sample input |
sample output |
4 3 |
2.3125 |
思路:一维DP,d[i]表示第i个人拿到奖品的概率;
从第二个人开始往后推,d[i] = d[i-1]*d[i-1] + (1-d[i-1])*(d[i-1]-1/n);
前一个人拿到奖品 + 前一个人没有拿到奖品
最后for循环累加d[i];d[i] 虽然是概率,但是在计算的时候已经把它那一步的期望算进去了,最后累加起来其实就是期望了。
/*
SGU 495
*/
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
const int MAXN=;
double dp[MAXN];//dp[i]表示第i个人得到礼物的概率 int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
dp[]=;
for(int i=;i<=m;i++)
{//第i个人得到礼物的概率:假如第i-1个人没有得到礼物,那么i得到礼物的概率和i-1一样。
//假如第i-1个人得到了礼物,那么i得到礼物的概率是i-1得到礼物概率减去1/n
dp[i]=(-dp[i-])*dp[i-]+dp[i-]*(dp[i-]-1.0/n);
}
double ans=;
for(int i=;i<=m;i++)ans+=dp[i];
printf("%.10lf\n",ans);
}
return ;
}
概率DP求解例题的更多相关文章
- [转]概率DP总结 by kuangbin
概率类题目一直比较弱,准备把kuangbin大师傅总结的这篇题刷一下! 我把下面的代码换成了自己的代码! 原文地址:http://www.cnblogs.com/kuangbin/archive/20 ...
- 概率dp入门
概率DP主要用于求解期望.概率等题目. 转移方程有时候比较灵活. 一般求概率是正推,求期望是逆推.通过题目可以体会到这点. poj2096:Collecting Bugs #include <i ...
- HDU 4405:Aeroplane chess(概率DP入门)
http://acm.split.hdu.edu.cn/showproblem.php?pid=4405 Aeroplane chess Problem Description Hzz loves ...
- HDU 4405 【概率dp】
题意: 飞行棋,从0出发要求到n或者大于n的步数的期望.每一步可以投一下筛子,前进相应的步数,筛子是常见的6面筛子. 但是有些地方可以从a飞到大于a的b,并且保证每个a只能对应一个b,而且可以连续飞, ...
- [HDU 4089]Activation[概率DP]
题意: 有n个人排队等着在官网上激活游戏.Tomato排在第m个. 对于队列中的第一个人.有以下情况: 1.激活失败,留在队列中等待下一次激活(概率为p1) 2.失去连接,出队列,然后排在队列的最后( ...
- HDU 4089 Activation(概率DP)(转)
11年北京现场赛的题目.概率DP. 公式化简起来比较困难....而且就算结果做出来了,没有考虑特殊情况照样会WA到死的.... 去参加区域赛一定要考虑到各种情况. 像概率dp,公式推出来就很容易写 ...
- 动态规划——概率dp
所谓概率dp,用动态规划的思想找到一个事件中可能发生的所有情况,然后找到符合要求的那些情况数,除以总数便可以得到符合要求的事件发生的概率.其核心思想还是通过dp来得到事件发生的所有情况,很类似在背包专 ...
- sdut2623--The number of steps(概率dp第一弹,求期望)
The number of steps Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描写叙述 Mary stands in a st ...
- Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)
Problem Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...
随机推荐
- JAVAWEB开发之JSTL标签库的使用、 自己定义EL函数、自己定义标签(带属性的、带标签体的)
JSTL JSTL简单介绍: JSTL的全称:JSP Standard Tag Library,JSP标准标签库 JSTL的作用: 提供给Java Web开发者一个标准通用的标签函数库 和E ...
- 每天一个linux命令(16):which
1.命令简介 which (which) 命令的作用是在PATH变量指定的路径中搜索某个系统命令的位置并且返回第一个搜索结果.也就是说使用which命令就可以看到某个系统命令是否存在以及执行的到底是哪 ...
- 自建k8s集群日志采集到阿里云日志服务
自建k8s集群 的master 节点安装 logtail 采集工具 wget http://logtail-release-cn-hangzhou.oss-cn-hangzhou.aliyuncs.c ...
- ubuntu系统默认源更改为阿里源
from:http://blog.csdn.net/minicto/article/details/62240020 ubuntu系统默认源更改为阿里源 ubuntu默认使用的国外的源,在更新的时候会 ...
- 关于python中pika模块的问题
工作中经常用到rabbitmq,而用的语言主要是python,所以也就经常会用到python中的pika模块,但是这个模块的使用,也给我带了很多问题,这里整理一下关于这个模块我在使用过程的改变历程已经 ...
- fiddler的编程文章
https://www.cnblogs.com/trevan/p/9487223.html https://www.cnblogs.com/rufus-hua/p/5275980.html https ...
- Linux 安装Zookeeper集群
1.解压,配置环境变量 export ZOOKEEPER_HOME=/usr/local/zkexport PATH=.:$HADOOP_HOME/bin:$ZOOKEEPER_HOME/bin:$J ...
- 【iCore4 双核心板_FPGA】例程十七:基于FIFO的ARM+FPGA数据存取实验
实验现象: 核心代码: int main(void) { /* USER CODE BEGIN 1 */ int i; int fsmc_read_data; ; ]; ]; char *p; /* ...
- iOS10.3 起,将支持应用内评分
iOS10.3 起,将支持应用内评分. (2017-01-25,现在最高版本iOS10.2.1,Xcode 稳定版本Xcode8.2.1,Xcode Beta版本 Xcode8.3Beta(BW109 ...
- ubuntu下core file文件生成及调试
1.简介:corefile 是Linux下程序崩溃时生成的文件,可以用来分析程序崩溃的原因,因为它内部包含了程序崩溃时的堆栈信息. 2.corefile的设置 默认情况下,程序崩溃是不会生成coref ...