There is a robot on the 2D plane. Robot initially standing on the position (0, 0). Robot can make a 4 different moves:

  1. Up (from (x, y) to (x, y + 1)) with probability U.
  2. Right (from (x, y) to (x + 1, y)) with probability R.
  3. Down (from (x, y) to (x, y - 1)) with probability D.
  4. Left (from (x, y) to (x - 1, y)) with probability L.

After moving N times Robot gets points.

  • Let x1 be the smallest coordinate in X-axis, that Robot reached in some moment.
  • Let x2 be the largest coordinate in X-axis, that Robot reached in some moment.
  • Let y1 be the smallest coordinate in Y-axis, that Robot reached in some moment.
  • Let y2 be the largest coordinate in Y-axis, that Robot reached in some moment.

Points achieved by Robot equals to x2 - x1 + y2 - y1.

Given N, U, R, D, L. Calculate expected value of points that Robot achieved after N moves.

Input

First line: One interger N (1 ≤ N ≤ 200).

Second line: Four real numbers U, R, D, L (U + R + D + L = 1, 0 ≤ U, R, D, L ≤ 1) with maximum of 6 numbers after dot.

Output

One number: expected value of points achieved by Robot. The answer will be considered correct if its relative or absolute error does not exceed 10-6.

Example 1

Input:
2
0.100000 0.200000 0.300000 0.400000
Output:
1.780000

Example 2

Input:
3
0.25 0.25 0.25 0.25
Output:
2.375000

题意:二维平面上一个机器人,给出机器人走的步数,已知走上下左右的概率;机器人走到最右边是Xmax,最左边是Xmin,上下同理。问机器人Xmax-Xmin+Ymax-Ymin的期望。

思路:万万没想到,4个方向是分开求,开始一直在想怎么整体求。。。。分开求的话就不难想了,分别求出X方向的最大最小,Y方向的最大最小,搜索一下。。。自己看代码。。。但是注意搜索会超时,注意记忆化。。。

(总之,是个不错的题!

#include<bits/stdc++.h>
using namespace std;
double u,d,l,r,ans;
double dp1[][][],dp2[][][],dp3[][][],dp4[][][];
int vis1[][][],vis2[][][],vis3[][][],vis4[][][];
double maxx(int x,int R,int step)
{
if(vis1[x][R][step]) return dp1[x][R][step];
if(step==) return R;
double res=;
res+=maxx(x,R,step-)*(u+d);
res+=maxx(x+,max(R,x+),step-)*r;
res+=maxx(x-,R,step-)*l;
vis1[x][R][step]=; dp1[x][R][step]=res;
return res;
}
double minx(int x,int L,int step)
{
if(vis2[x][L][step]) return dp2[x][L][step];
if(step==) return L;
double res=;
res+=minx(x,L,step-)*(u+d);
res+=minx(x+,L,step-)*r;
res+=minx(x-,min(L,x-),step-)*l;
vis2[x][L][step]=; dp2[x][L][step]=res;
return res;
}
double maxy(int y,int U,int step)
{
if(vis3[y][U][step]) return dp3[y][U][step];
if(step==) return U;
double res=;
res+=maxy(y,U,step-)*(l+r);
res+=maxy(y+,max(U,y+),step-)*u;
res+=maxy(y-,U,step-)*d;
vis3[y][U][step]=; dp3[y][U][step]=res;
return res;
}
double miny(int y,int D,int step)
{
if(vis4[y][D][step]) return dp4[y][D][step];
if(step==) return D;
double res=;
res+=miny(y,D,step-)*(l+r);
res+=miny(y+,D,step-)*u;
res+=miny(y-,min(D,y-),step-)*d;
vis4[y][D][step]=; dp4[y][D][step]=res;
return res;
}
int main()
{
int N;
cin>>N>>u>>r>>d>>l;
ans+=maxx(,,N);
ans-=minx(,,N);
ans+=maxy(,,N);
ans-=miny(,,N);
printf("%.7lf\n",ans);
return ;
}

SPOJ:Robot(数学期望)的更多相关文章

  1. SPOJ FAVDICE 数学期望

    题目大意: 一个有n面的色子抛掷多少次能使所有面都能被抛到过,求期望值 总面数为n,当已经抛到过 i 个不同面时,我们抛出下一个不同面的概率为 (n-i)/n,那么抛的次数为 n/(n-i) 将所有抛 ...

  2. 【整理】简单的数学期望和概率DP

    数学期望 P=Σ每一种状态*对应的概率. 因为不可能枚举完所有的状态,有时也不可能枚举完,比如抛硬币,有可能一直是正面,etc.在没有接触数学期望时看到数学期望的题可能会觉得很阔怕(因为我高中就是这么 ...

  3. 动态规划之经典数学期望和概率DP

    起因:在一场训练赛上.有这么一题没做出来. 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6829 题目大意:有三个人,他们分别有\(X,Y,Z\)块钱 ...

  4. [BZOJ 3143][HNOI2013]游走(数学期望)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3143 分析: 易得如果知道了每条边经过的数学期望,那就可以贪心着按每条边的期望的大小赋 ...

  5. Codeforces Round #259 (Div. 2) C - Little Pony and Expected Maximum (数学期望)

    题目链接 题意 : 一个m面的骰子,掷n次,问得到最大值的期望. 思路 : 数学期望,离散时的公式是E(X) = X1*p(X1) + X2*p(X2) + …… + Xn*p(Xn) p(xi)的是 ...

  6. 数学期望和概率DP题目泛做(为了对应AD的课件)

    题1: Uva 1636 Headshot 题目大意: 给出一个000111序列,注意实际上是环状的.问是0出现的概率大,还是当前是0,下一个还是0的概率大. 问题比较简单,注意比较大小: A/C & ...

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

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

  8. 【BZOJ2134】单位错选(数学期望,动态规划)

    [BZOJ2134]单位错选(数学期望,动态规划) 题面 BZOJ 题解 单独考虑相邻的两道题目的概率就好了 没了呀.. #include<iostream> #include<cs ...

  9. 【BZOJ1415】【NOI2005】聪聪和可可(动态规划,数学期望)

    [BZOJ1415][NOI2005]聪聪和可可(动态规划,数学期望) 题面 BZOJ 题解 先预处理出当可可在某个点,聪聪在某个点时 聪聪会往哪里走 然后记忆化搜索一下就好了 #include< ...

  10. 【Luogu1291】百事世界杯之旅(动态规划,数学期望)

    [Luogu1291]百事世界杯之旅(动态规划,数学期望) 题面 洛谷 题解 设\(f[i]\)表示已经集齐了\(i\)个名字的期望 现在有两种方法: 先说我自己的: \[f[i]=f[i-1]+1+ ...

随机推荐

  1. Heavy Transportation(最短路)

    poj 1797 ——Heavy Transportation 思路: 这道题我们可以采用类似于求最短路径的方法,用一种新的“松弛操作”去取代原本的方法. 我们可以记录d[u]为运送货物到点j时最大可 ...

  2. Codeforces Round #321 (Div. 2) E

    终于补好了. 题目链接: http://codeforces.com/contest/580/problem/E E. Kefa and Watch time limit per test 1 sec ...

  3. 【Todo】Spark运行架构

    接上一篇:http://www.cnblogs.com/charlesblc/p/6108105.html 上一篇文章中主要参考的是 Link 这个系列下一篇讲的是Idea,没有细看,又看了再下一篇: ...

  4. 【转】fedora和ubuntu开启ssh

    fedora和ubuntu开启ssh 1小时前 ubuntu开启SSH服务 SSH分客户端openssh-client和openssh-server如果你只是想登陆别的机器的SSH只需要安装opens ...

  5. Codeforces Round #266 (Div. 2) C. Number of Ways

    You've got array a[1], a[2], ..., a[n], consisting of n integers. Count the number of ways to split ...

  6. 【J2EE】十三个规范:愿天下苍生,人人如猿。

    学习了J2ee后对java这个立足标准化的行为感到深深的佩服. 收买什么都不如收买人心,培养啥子都不如培养 习惯.没错,java就是在培养行业习惯,以一纸规范屹立不倒.毕竟技术什么的层出不穷,再新再前 ...

  7. Android安全机制介绍

    Android的安全机制包含下面几个方面:      • 进程沙箱隔离机制.      • 应用程序签名机制.      • 权限声明机制.      • 訪问控制机制.      • 进程通信机制. ...

  8. webpack2 详解

    1.安装 npm install webpack -g npm install webpack -save-dev 2.编辑配置文件 // 引入 path var path=require('path ...

  9. 亲测linux上安装svn

    方法一: 1.wget http://subversion.tigris.org/downloads/subversion-1.6.1.tar.gz2.wget http://subversion.t ...

  10. python socket初探

    先看一段代码 import socket import sys import re def getServerContent(url): host_ip = socket.gethostbyname( ...