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. SGU 107 数学题

    题意:求平方后末尾9个数是987654321的数个数. 之前做此题,竟然愚蠢到用计算器 在哪里算,还加笔算,SB啊!不知道先打印一下吗! #include<iostream> #inclu ...

  2. Java Enum枚举的用法(转)

    说明:Java的枚举比dotnet的枚举好用,至少支持的方式有很多. 用法一:常量 在JDK1.5 之前,我们定义常量都是: public static fianl.... .现在好了,有了枚举,可以 ...

  3. 【hibernate】报错:org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.DataException: could not execute statement

    报错如下: org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a ...

  4. js时间戳和时间格式之间的转换

    //时间戳转换成日期时间2014-8-8 下午11:40:20 function formatDate(ns){ return new Date(parseInt(ns) * 1000).toLoca ...

  5. flask使用debug模式时,存在错误时,会占用设备内存直至服务重启才释放;debug模式会开启一个守护进程(daemon process)

    函数调用顺序flask的app.py的run-->werkzeug的serving.py的run_simple-->调用werkzeug的debug的__init__.py里的类Debug ...

  6. 应用程序中的server错误,没有名称为“ServiceBehavior”的服务行为

    应用程序中的server错误,没有名称为"ServiceBehavior"的服务行为         今天在阅读"创建和使用Web服务"的相关内容,在浏览器中查 ...

  7. nginx配置初步

    nginx配置初步 1,切换至nginx目录,找到配置文件目录 cd /etc/nginx/conf.d 2,拷贝一份conf文件 sudo cp default.conf head.conf 3,进 ...

  8. 79.iOS 设备的UI规范和iOS各控件默认高度

    iOS设备的UI 规范 iPhone界面尺寸 iPhone图标尺寸 iPad的设计尺寸 iPad图标尺寸 iPhone设备尺寸分辨率比例 iPhone各设备 launch image iOS 各种控件 ...

  9. C#对二进制文件的特定位置进行读写小结

    虽然网络上关于“C#对二进制文件进行读写”的文章多如牛毛,但具体到自己要处理的问题时,难免让人产生“书到用时方恨少”和“纸上读来终觉浅”的感觉.我现在感觉要真正解决自己的问题,最终还是要靠自己下功夫. ...

  10. SolidEdge如何为零件指定不同的颜色 给零件着色 给装配体着色

    格式-零件画笔 可以给零件的一个表面,或者一个特征着色   如果要指定不同的色彩,可以在格式-样式-面样式中修改   如果是给装配体着色,则点击任意零件,在"无"的选项卡里面修改颜 ...