概率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 ...
随机推荐
- grid - 网格项目层级
网格项目可以具有层级和堆栈,必要时可能通过z-index属性来指定. 1.在这个例子中,item1和item2的开始行都是1,item1列的开始是1,item2列的开始是2,并且它们都跨越两列.两个网 ...
- Jenkins安装卸载
下载安装去Jenkins官网下载Jenkins,Centos的话会下载到.rpm安装文件 安装.rpm文件使用命令rpm -ivh **.rpm 安装完成之后使用命令rpm -qc jenkins查看 ...
- R文本挖掘之jiebaR包
library(jiebaRD)library(jiebaR) ##调入分词的库cutter <- worker()mydata =read.csv(file.choose(),fileEnc ...
- Effective Java 第三版—— 86. 非常谨慎地实现SERIALIZABLE接口
Tips 书中的源代码地址:https://github.com/jbloch/effective-java-3e-source-code 注意,书中的有些代码里方法是基于Java 9 API中的,所 ...
- Django Rest Framework(认证、权限、限制访问频率)
阅读原文Django Rest Framework(认证.权限.限制访问频率) django_rest_framework doc django_redis cache doc
- HTTPS和SSL握手过程(转载)
https介绍 HTTPS = HTTP + 一组对称.非对称和基于证书的加密技术 HTTPS是最常见的HTTP安全版本.它得到了很广泛的应用,所有主要的商业浏览器和服务器都提供HTTPS.HTTPS ...
- 【温暖】文龙回AICODER给老马送锦旗了
又是一个愉快的周末,AICODER第一批老学员文龙小伙伴.已经工作两个月,而且就业薪资12000+,文龙从之前月薪不足4000,一下子翻了三倍多的工资. 几个月的实习,让文龙掌握了大前端全栈的技术,在 ...
- Maven知识总结(转)
原文地址:http://blog.csdn.net/caihaijiang/article/details/6664910 1.Maven内置变量说明: ${basedir} 项目根目录 ${proj ...
- Java中Access restriction:····的解决方法
http://blog.csdn.net/bit2012_2015/article/details/22798779 ————————————————————————————————————————— ...
- Activity的Launch mode详解,A B C D的singleTask模式
本文参考了此文http://hi.baidu.com/amauri3389/blog/item/a54475c2a4b2f040b219a86a.html 另附 android task与back s ...