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. Docker实战系列一:初识Docker for Windows

    windows下安装Docker官网教程Install Docker for Windows Docker配置官网教程Get started with Docker for Windows

  2. 【版本控制】VisualSVN Server更改SVN版本库存放路径的方法

    最近也玩起了SVN软件版本管理,在本机上安装了VisualSVN Server+TortoiseSVN,感觉还不错吧.但是,版本库存在哪里呢?在安装VisualSVN Server时,已经默认设置了, ...

  3. background 背景图铺满界面

    background <body background="/image/1.png" style=" background-repeat:no-repeat ; b ...

  4. P1118 [USACO06FEB]数字三角形`Backward Digit Su`…

    题目描述 FJ and his cows enjoy playing a mental game. They write down the numbers from 11 to N(1 \le N \ ...

  5. Hibernate高级应用

    数据模型与领域模型的关系 领域模型是一个分析模型,它帮助需求分析人员.用户认识现实业务的工具,描述的是业务中设计的试题及其相互之间的关系,它是需求分析的产物.领域模型是需求分析人员与用户交流的有力工具 ...

  6. 2017 Multi-University Training Contest - Team 2 TrickGCD(组合数学)

    题目大意: 给你一个序列An,然后求有多少个序列Bn 满足Bi<=Ai,且这个序列的gcd不为1 题解: 考虑这样做 枚举一个因子k,然后求出有多少个序列的gcd包含这个因子k 然后把结果容斥一 ...

  7. BZOJ1103 [POI2007]大都市meg 【树剖】

    1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 3038  Solved: 1593 [Submit][S ...

  8. OpenJudge百炼-2747-数字方格-C语言-枚举

    描述:如上图,有3个方格,每个方格里面都有一个整数a1,a2,a3.已知0 <= a1, a2, a3 <= n,而且a1 + a2是2的倍数,a2 + a3是3的倍数, a1 + a2 ...

  9. Reasons to use innodb_file_per_table

    When working with InnoDB, you have two ways for managing the tablespace storage: Throw everything in ...

  10. ubuntu安装GraphicsMagick

    一. sudo apt-get install graphicsmagick 二. http://www.cnblogs.com/cocowool/archive/2010/08/16/1800954 ...