Problem Description
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 <= 100). 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.
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 <= 100). 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
3 1 1 0.5 0.5 2 4 0.5 0.5 3 10 0.5 0.4
Sample Output
1 0.5000 2 1.18753 1.4965
#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> using namespace std; const int maxn=510; int T,n,m; double l,r,f[2][maxn][maxn]; int main() { cin>>T; while (T--) { cin>>m>>n>>l>>r; memset(f,0,sizeof(f)); f[0][200][200]=1; int v=1; for (int k=1;k<=n;k++) { memset(f[v],0,sizeof(f[v])); for (int i=200-k;i<=200+k;i++) for (int j=i;j<=200+k;j++) { f[v][i][j]+=f[v^1][i][j]*(1-l-r); f[v][i-1][j]+=f[v^1][i][j]*l; f[v][i+1][max(j,i+1)]+=f[v^1][i][j]*r; } v^=1; } double ans=0; for (int i=200-n;i<=200+n;i++) for (int j=200;j<=200+n;j++) ans+=(j-200)*f[v^1][i][j]; printf("%d %.4lf\n",m,ans); } return 0; }