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(][]);
  }
}

最新文章

  1. Windows Phone 三、样式和资源
  2. 链表c++实现一
  3. centos6 安装python2.7
  4. EF事务
  5. 解决mvc部署在IIS上以后出现404错误
  6. Codeforces Round #346 (Div. 2)
  7. DataRow.RowState 属性
  8. Mysql打开日志信息
  9. sql2005下载和安装
  10. MyEclipse server窗口 Could not create the view: An unexpected exception was thrown 错误解决
  11. DataSet、DataTable、DataRow、DataColumn区别及使用实例
  12. Gdiplus 贴图(助记) -------------------拖动整个对话框
  13. IOS safari浏览器登陆时Cookie无法保存的问题
  14. pandas的基本功能(一)
  15. 再谈Retina下1px的解决方案
  16. idea格式化代码无效Ctrl+Alt+L
  17. ELK-logstash-6.3.2部署
  18. unity3d的Animation 动画播放器的基本API
  19. XAMPP+composer+laravel+thinkphp5那些深情的往事
  20. jar依赖

热门文章

  1. 用户代理UA
  2. Redis使用手册
  3. MySQL 数据库性能优化之缓存参数优化
  4. DES 加密解密
  5. mapreduce出现大量task被KILLED_UNCLEAN的3个原因
  6. Maven 标准目录结构
  7. Eclipse开发环境配置,打磨Eclipse,安装插件(适用3.4,3.5,3.6,3.7)
  8. 《vue.js实战》练习---数字输入框组件
  9. SQL优化之一
  10. [bzoj1208][HNOI2004]宠物收养所——splay