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

There are no more than 70 test cases. 
In each case , first Input a positive integer n(0<n<45), which means the layer of the maze, then Input five real number a, b, c, d, e. (0<=a,b,c,d,e<=1, a+b=1, c+d+e=1). 
The input is terminated with 0. This test case is not to be processed.
 

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 ;
}
                                 二,先算概率,后算期望
                                                                           SGU 495  Kids and Prizes

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

The first and only line of the input file contains the values of N and M (
).

Output

The
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求解例题的更多相关文章

  1. [转]概率DP总结 by kuangbin

    概率类题目一直比较弱,准备把kuangbin大师傅总结的这篇题刷一下! 我把下面的代码换成了自己的代码! 原文地址:http://www.cnblogs.com/kuangbin/archive/20 ...

  2. 概率dp入门

    概率DP主要用于求解期望.概率等题目. 转移方程有时候比较灵活. 一般求概率是正推,求期望是逆推.通过题目可以体会到这点. poj2096:Collecting Bugs #include <i ...

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

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

  4. HDU 4405 【概率dp】

    题意: 飞行棋,从0出发要求到n或者大于n的步数的期望.每一步可以投一下筛子,前进相应的步数,筛子是常见的6面筛子. 但是有些地方可以从a飞到大于a的b,并且保证每个a只能对应一个b,而且可以连续飞, ...

  5. [HDU 4089]Activation[概率DP]

    题意: 有n个人排队等着在官网上激活游戏.Tomato排在第m个. 对于队列中的第一个人.有以下情况: 1.激活失败,留在队列中等待下一次激活(概率为p1) 2.失去连接,出队列,然后排在队列的最后( ...

  6. HDU 4089 Activation(概率DP)(转)

    11年北京现场赛的题目.概率DP. 公式化简起来比较困难....而且就算结果做出来了,没有考虑特殊情况照样会WA到死的.... 去参加区域赛一定要考虑到各种情况.   像概率dp,公式推出来就很容易写 ...

  7. 动态规划——概率dp

    所谓概率dp,用动态规划的思想找到一个事件中可能发生的所有情况,然后找到符合要求的那些情况数,除以总数便可以得到符合要求的事件发生的概率.其核心思想还是通过dp来得到事件发生的所有情况,很类似在背包专 ...

  8. sdut2623--The number of steps(概率dp第一弹,求期望)

    The number of steps Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描写叙述 Mary stands in a st ...

  9. Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)

    Problem   Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...

随机推荐

  1. GitHub删除已有仓库

    之前都只是创建,还没试过删除,讲道理,如果第一次找删除按钮,还是有点小曲折的,特记录如下: 1.先找到你要删除的仓库 2.点进去,到具体项目地址,找到setting 3.点进去,一直往下翻,会看到红色 ...

  2. R语言中的回归诊断-- car包

    如何判断我们的线性回归模型是正确的? 1.回归诊断的基本方法opar<-par(no.readOnly=TRUE) fit <- lm(weight ~ height, data = wo ...

  3. TensorFlow+Keras 01 人工智能、机器学习、深度学习简介

    1 人工智能.机器学习.深度学习的关系 “人工智能” 一词最早是再20世纪50年代提出来的. “ 机器学习 ” 是通过算法,使用大量数据进行训练,训练完成后会产生模型 有监督的学习 supervise ...

  4. Atitit s2018 s4 doc list dvchomepc dvccompc.docx .docx \s2018 s4 doc compc dtS44 \s2018 s4 doc dvcCompc dtS420 \s2018 s4f doc homepc \s2018 s4 doc compc dtS44\(5 封私信 _ 44 条消息)WebSocket 有没有可能取代 AJAX

    Atitit s2018 s4 doc list dvchomepc dvccompc.docx .docx \s2018 s4 doc compc dtS44 \s2018 s4 doc dvcCo ...

  5. SATA主机协议的FPGA实现之准备工作

    SATA主机协议的FPGA实现之准备工作   从2月中旬准备开始,经过3个月的奋战,我的又一个项目--基于FPGA的固态硬盘读写控制电路,已经基本实现.由于实用资料的匮乏,以及项目本身颇具挑战性,这个 ...

  6. Variational RL for POMDP

    1.Le, Tuan Anh, et al. "Auto-encoding sequential monte carlo." arXiv preprint arXiv:1705.1 ...

  7. Centos7.0下Nexus私服搭建

    1.下载nexus wget https://sonatype-download.global.ssl.fastly.net/nexus/oss/nexus-2.11.2-03-bundle.tar. ...

  8. java生成zip压缩文件,解压缩文件

    1.生成zip public static void main(String[] args) { try { // testZip("c:\\temp.txt", "c: ...

  9. saltstack通过jinja模板,将变量值增加到配置文件中?通过引用变量值修改配置文件?

    需求描述: 在使用saltstack的时候,有的时候,需要根据不同的变量来增加配置,比如,bind,监听端口,这些都可以通过变量写入,并且在配置的时候引用,下面是一个例子,用来演示,如何使用jinja ...

  10. 关于pythoh面向过程开发人员三步转面向对象的补充,再加一步,四步走战略。转面向对象也可以有固定公式。

    前言: oop非常非常非常重要.搞不懂oop,就玩不了python,就算能写也一定是写代码时候喜欢靠猜瞎猫碰死老鼠写得心很虚.为什么这么说呢,我也是从面向过程编程到死走过来的,一路def到死,一看到有 ...