Robot
Problem descriptionmichael Has A telecontrol robot. One day he put the robot on a loop with N cells. The cells are numbered from 1 to n clockwise.
At first the robot is in cell 1. then Michael uses a remote control to send M commands to the robot. A command will make the robot walk some distance. unfortunately the direction part on the remote control is broken, so for every command the robot will chose a direction (clockwise or anticlockwise) randomly with equal possibility, and then walk W cells forward.
Michael wants to know the possibility of the robot stopping in the cell that cell number> = L and <= R after M commands.
Inputthere are multiple test cases.
Each test case contains several lines.
The first line contains four integers: above mentioned n (1 ≤ n ≤ 200), m (0 ≤ m ≤ 1,000,000), L, R (1 ≤ L ≤ r ≤ n ).
Then M lines follow, each representing a command. A command is a integer W (1 ≤ W ≤ 100) representing the cell length the robot will walk for this command.
The input end with n = 0, m = 0, L = 0, r = 0. You shoshould not process this test case.
Outputfor each test case in the input, you should output a line with the expected possibility. Output shocould be round to 4 digits after decimal points.
Sample Input
3 1 1 215 2 4 4120 0 0 0
Sample output
0.50000.2500
Source2013ACM-ICPC Hangzhou Division national invitational Competition
Question:
4 digits in the first row: N grids and M operations on a disc. L and R represent intervals. In the next m rows, each row has 1 digit W, at the beginning, the robot moved the W grid clockwise or counter-clockwise in the grid 1 and asked about the probability of the final stop in the range [L, R.
Solution:
I met this question when I went to Hangzhou last year. At that time, I had a hard time and felt like I was so weak !!
Now it's easy to look back at this question.
The I-W lattice can only be obtained from the I-w lattice and the I + W lattice, with each half of the probability. So pay attention to the border and use the rolling idea.
Solution code:
#include <iostream>#include <cstdio>using namespace std;const int maxn=210;double p[maxn],q[maxn];int n,m,l,r;void solve(){ double ans=0; for(int i=0;i<=n;i++) q[i]=p[i]=0; p[0]=1.0; for(int t=0;t<m;t++){ int w; scanf("%d",&w); for(int i=0;i<n;i++){ if(p[i]>0){ q[(i+w)%n]+=p[i]*0.5; q[((i-w)%n+n)%n ]+=p[i]*0.5; } } for(int i=0;i<n;i++){ p[i]=q[i]; q[i]=0; } } for(int i=l-1;i<r;i++) ans+=p[i]; printf("%.4lf\n",ans);}int main(){ while(scanf("%d%d%d%d",&n,&m,&l,&r)!=EOF && (n||m||l||r) ){ solve(); } return 0;}