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. gitlab配置通过smtp发送邮件(QQ exmail腾讯企业为例)

    gitlab配置通过smtp发送邮件(QQ exmail腾讯企业为例) 首先祭出官网文档链接:https://docs.gitlab.com/omnibus/settings/smtp.html 其实 ...

  2. K-means算法原理

    聚类的基本思想 俗话说"物以类聚,人以群分" 聚类(Clustering)是一种无监督学习(unsupervised learning),简单地说就是把相似的对象归到同一簇中.簇内 ...

  3. Oracle&SQLServer中实现跨库查询

    一.在SQLServer中连接另一个SQLServer库数据 在SQL中,要想在本地库中查询另一个数据库中的数据表时,可以创建一个链接服务器: EXEC master.dbo.sp_addlinked ...

  4. Linux 系统 TCP优化

    这里主要是对<High performance Browser Networking>一书中关于TCP的描述的整理,本书与2013年出版,在书出版后,内核做了一些升级,有可能某些项不再适用 ...

  5. Spring Boot 2.x 学习专栏

    Spring Boot 2.0 入门指南 Spring Boot 2.0 返回JSP页面实战 Spring Boot 2.0 热部署指南 Spring Boot 2.0 整合FreeMarker模板引 ...

  6. vs code 设置问题

    现已取消 .vue 文件与 HTML 的默认关联,需要手动配置.vue 文件里不能使用div + Tab 键快速生成 html 代码   "emmet.syntaxProfiles" ...

  7. 自定义命令杀死 java 进程 alias kjava

    alias kjava='ps -ef|grep ProcessName |awk "{print $2}"|xargs kill -9' 上面脚本放在杀JAVA进程中,会出现一些 ...

  8. 【iCore4 双核心板_ARM】例程三十五:HTTP_IAP_ARM实验——更新升级STM32

    实验现象: 核心代码: int main(void) { led.initialize(); //LED³õʼ»¯ key.initialize(); if(ARM_KEY_STATE == KEY ...

  9. 【iCore4 双核心板_ARM】例程三十三:SD_IAP_ARM实验——更新升级STM32

    实验现象及操作说明: 1.本例程共有两个代码包,APP和IAP,IAP程序功能实现将APP程序升级至STM32中. 2.直接上电或烧写程序将执行升级的APP应用程序. 3.按下按键上电或写程序将进行升 ...

  10. 欢迎访问我的最新个人技术博客http://zhangxuefei.site

    博客已经搬家,欢迎访问我的最新个人技术博客:http://zhangxuefei.site