[ACM] HDU 4576 robot (probability DP, rolling array)

Source: Internet
Author: User

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


Solution:

A circle is surrounded by grids numbered 1-N. At first, the robot stood in the grid numbered 1 and had M commands. Each Command robot took a grid of W, which can be clockwise or counterclockwise, q: What is the probability of a robot standing in the grid [L, R] After M commands.

The idea is roughly to calculate the probability of robots on each grid after M commands are run in brute force mode, so P [l] + P [L + 1] + P [L + 2] + ............ P [R] is desired

Use DP [I] [J] to indicate the probability that the robot will be at the J position after the I-th command.

Because the number of commands M, 0 ≤ m ≤ 1,000,000, it is impossible to open such a large array, because the probability of the robot location after the I command is only related to the location of the robot after the I-1 command, so you can use the rolling array, DP [2] [J. The rolling array requires that the current status only has a relationship with the previous status and has nothing to do with the previous status. Therefore, the stored data space can be overwritten and reused to reduce the space complexity, however, the time complexity remains unchanged.

Rolling array details: http://blog.csdn.net/niushuai666/article/details/6677982

Code:

# Include <iostream> # include <stdio. h> # include <iomanip> # include <string. h> using namespace STD; double DP [2] [210]; int main () {int N, M, L, R, cm; while (scanf ("% d", & N, & M, & L, & R )! = EOF) {If (n = 0 & M = 0 & l = 0 & R = 0) break; int D = 0; for (INT I = 1; I <n; I ++) DP [d] [I] = 0; DP [0] [0] = 1; while (M --) {scanf ("% d", & cm); For (INT I = 0; I <n; I ++) DP [d ^ 1] [I] = 0; // The probability of each position in the new State is 0 for (INT I = 0; I <n; I ++) {If (DP [d] [I] = 0) // if this clause is added, the continue will not time out. DP [d ^ 1] [(I + cm) % N] + = 0.5 * DP [d] [I]; DP [d ^ 1] [(I-CM + n) % N] + = 0.5 * DP [d] [I];} d ^ = 1; // ^ 1 is to add 1 or subtract 1} double ans = 0; For (INT I = L-1; I <r; I ++) ans + = DP [d] [I]; cout <setiosflags (IOs: fixed) <setprecision (4) <ans <Endl;} return 0 ;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.