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

Hint

题解:

数学期望E(X) = X1*p(X1) + X2*p(X2) + …… + Xn*p(Xn); 
比如打靶打中8环的概率为0.3 ,打中7环的概率为0.7,那么打中环数的期望就是 8*0.3 + 7*0.7; 
本题中我们用dp[i][j] 表示当前位置(i,j,表示房间的位置,最顶层的房间为(1,1),最低层最左边为(n,1))距离目的地还需要走的期望步数。那么目的地假设为dp[n][1] (根据建的坐标不一样,位置也不一样),那么dp[n][1]的值为0,因为已经到达目的地,不需要再走了。那么我们所求的就是dp[1][1] 开始的地方。所以解题的过程,就是一个逆推的过程。整个逆推过程完成,dp[1][1]内的值就是所求的期望步数。

代码:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define is_lower(c) (c >= 'a' && c <= 'z')
#define is_upper(c) (c >= 'A' && c <= 'Z')
#define is_alpha(c) (is_lower(c) || is_upper(c))
#define is_digit(c) (c >= '0' && c <= '9')
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define PI acos(-1)
#define IO                 \
  ios::sync_with_stdio(); \
  cin.tie();              \
  cout.tie();
#define For(i, a, b) for (int i = a; i <= b; i++)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
const ll inf = 0x3f3f3f3f;
;
const ll inf_ll = (ll)1e18;
const ll maxn = 100005LL;
const ll mod = 1000000007LL;
 + ;
double ans[N][N];
int main() {
  int n;
  while (cin >> n, n) {
    memset(ans, , sizeof(ans));
    double a, b, c, d, e;
    cin >> a >> b >> c >> d >> e;
    For(i, , n) { ans[n][i] = ans[n][i - ] + ; }
    ; i >= ; i--) {
      ans[i][] = (ans[i + ][] + ) * a + (ans[i + ][] + ) * b;
      ; j <= i; j++) {
        ans[i][j] = (ans[i][j - ] + ) * e + (ans[i + ][j] + ) * c +
                    (ans[i + ][j + ] + ) * d;
      }
    }
    printf(][]);
  }
}

The number of steps(概率dp)的更多相关文章

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

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

  2. 13年山东省赛 The number of steps(概率dp水题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud The number of steps Time Limit: 1 Sec  Me ...

  3. [2013山东ACM]省赛 The number of steps (可能DP,数学期望)

    The number of steps nid=24#time" style="padding-bottom:0px; margin:0px; padding-left:0px; ...

  4. SDUT 2623 The number of steps (概率)

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

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

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

  6. UVALive-8138 Number Generator 概率dp+优化

    题目链接:https://cn.vjudge.net/problem/UVALive-8138 题意 有一个随机数生成器,输出1-n的整数. 现在已经输出了k个数,问再取几个数才能使取出的所有数的个数 ...

  7. sdutoj 2623 The number of steps

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2623 The number of steps ...

  8. HDU 4050 wolf5x(动态规划-概率DP)

    wolf5x Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  9. 概率dp入门

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

随机推荐

  1. win7 C/C++,QT安装环境总结

    1. 安装VMware,但是不能用,发现是权限问题,解决方式:使用管理员运行模式即可: 2. 安装win7 ultimate x64,找了半天找不到密钥,只要用激活软件,目前来说系统可用 3. 安装 ...

  2. qemu的drive参数解释

    drive参数很简单,可以理解成是定义了一个实际的硬盘(或者是cd)与drive对应的是device-drive option[,option[,option[,...]]] Define a new ...

  3. [剑指Offer] 10.矩形覆盖

    题目描述 我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形.请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法? [思路]可归纳得出结论: f(n) = f(n-1) + f ...

  4. jdbc连接oracle语法

    public class LangDemo { public static void main(String[] args) throws Exception{ try { //加载驱动 Class. ...

  5. Boosting&Bagging

    Boosting&Bagging 集成学习方法不是单独的一个机器学习算法,而是通过构建多个机器学习算法来达到一个强学习器.集成学习可以用来进行分类,回归,特征选取和异常点检测等.随机森林算法就 ...

  6. LA4273 Post Offices

    题目戳这里. 村庄排序.状态\(f[j][i]\)表示考虑前\(i\)个村庄,造\(j\)个邮局且\(i\)造了邮局的最小代价.我们用\(Lb_i,Rb_i\)表示在第\(i\)个村庄造邮局,邮局最左 ...

  7. vector创建2维数组

    以前我要建立一个二维数组,总是使用 int N=5, M=6; vector<vector<int> > Matrix(N); for(int i =0; i< Matr ...

  8. 收藏一个漂亮的Flash焦点图切换

    网上闲逛的时候发现一个Flash焦点图效果,跟喜欢,然后就下载回来,收集在这里,以便以后方便取用.这个Flash使用方法也是相当简单的,如果你喜欢,也可以从这里查看源代码下载. Flash 焦点图效果 ...

  9. hdu 1520Anniversary party 树形dp入门

    There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The Un ...

  10. 用静态工厂的方法实例化bean

    //代码如下: package com.timo.domain; public class ClientService { //use static factory method create bea ...