Consider the classic random walk: at each step, you have a 1/2 chance of taking a step to the left and a 1/2 chance of taking a step to the right. Your expected position after a period of time is zero; that is, the average over many such random walks is that you end up where you started. A more interesting question is what is the expected rightmost position you will attain during the walk.
Input
The first line of input contains a single integer P, (1 ≤ P ≤ 15), which is the number of data sets that
follow Each data set should be processed identically and independently.
Each data set consists of a single line of input consisting of four space-separated values. The first
value is an integer K, which is the data set number. Next is an integer n, which is the number of steps to take (1 ≤ n ≤ 1000). The final two are double precision floating-point values L and R which are the probabilities of taking a step left or right respectively at each step (0 ≤ L ≤ 1, 0 ≤ R ≤ 1, 0 ≤ L+R ≤ 1).
Note: the probably of not taking a step would be 1 − L − R.
Output
For each data set there is a single line of output. It contains the data set number, followed by a single
space which is then followed by the expected (average) rightmost position you will obtain during the
walk, as a double precision floating point value to four decimal places.
Sample Input
4
1 1 0.5 0.5
2 4 0.5 0.5
3 10 0.5 0.4
4 1000 0.5 0.4
Sample Output
1 0.5000
2 1.1875
3 1.4965
4 3.9995
题意
站在原点的你面临n次选择,向左走或向右走或原点不动。已知三种走法的概率,问到达最右距离的期望。
分析
期望: 在概率论和统计学中,一个离散性随机变量的期望值(或数学期望、或均值,亦简称期望)是试验中每次可能的结果乘以其结果概率的总和。
具体到该题,设X=“到达的最右距离”, X = {0,1,2,3…n};
期望:
该题属于概率DP类型题,设
DP[i][j][k], i 代表这次走了i步, j 代表的是当前走到了下标为j的位置, k代表从第一步开始到第i步
这期间到过的最右(大)下标处。
分两种情况:
- 当前位置是从开始到现在走的最右(大)下标处,即 j == k, 则有:
dp[i][j][k] = dp[pre][j][k] * m + dp[i-1][j-1][k] * r + dp[i-1][j-1][k-1] * r, m = 1.0 - l - r - 当前位置不是从开始到现在走的最右(大)下标处, 即 j != k,则有:
dp[i][j][k] = dp[i-1][j][k] * m + dp[i-1][j-1][k] * r + dp[i-1][j+1][k] * l, m = 1.0 - l - r
代码
- 该题数据量有点大, 要改一下DP数组,由于第i步的状态只取决与第i-1步的状态,故可以用
dp[2][N][N] 就行。 - DP数组记得初始化。
#include <iostream>
#include <string.h>
using namespace std;
double dp[2][2010][2010];
const int mid = 1001;
int main()
{
int T;
scanf("%d",&T);
while(T--){
int i,j,k,now,pre,z,n;
double m,l,r;
scanf("%d%d%lf%lf",&z,&n,&l,&r);
m = 1.0 - l - r;
memset(dp,0,sizeof(dp));
dp[0][mid][mid]=1.0;
for(i=1;i<=n;i++){
now = i%2;
pre = (i+1) % 2;
for(j=mid-i;j<=mid+i;j++){
for(k=max(j,mid);k<=mid+i;k++){
if(j == k){
dp[now][j][k] = dp[pre][j][k] * m + dp[pre][j-1][k] * r + dp[pre][j-1][k-1] * r;
}
else{
dp[now][j][k] = dp[pre][j][k] * m + dp[pre][j-1][k] * r + dp[pre][j+1][k] * l;
}
}
}
}
double ans=0;
for(j=mid-n;j<=mid+n;j++){
for(k = mid; k<=mid+n; k++){
ans += dp[now][j][k]*(k-mid);
}
}
printf("%d %.4lf\n",z,ans);
}
return 0;
}