Hdu 4576: Robot

Source: Internet
Author: User

RobotTime Limit: 8000/4000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others) Total Submission (s): 1600 Accepted Submission (s ): 599 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 wi Ll 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 c Ases. 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 I Nput, you shoshould output a line with the expected possibility. output shoshould be round to 4 digits after decimal points. sample Input3 1 215 2 4 4120 0 0 0 Sample Output0.50000.2500 Source2013ACM-ICPC Hangzhou Division national invitational Competition Algorithm: simulation of the subject meaning: Robot round circle (1-n) walking, there are m commands, select the direction randomly and use the I command to walk the mi distance. Calculate the probability within the range of l to r. Train of Thought: simulation, no timeout. Construct two arrays. temp [] records the previous state. cnt [] records the current state (its initialization is 0) and moves temp [], which is not 0, then add it to the cnt [] at the destination location. Finally, the output is ans = temp [l] +... + temp [r]; Code:

# Include <stdio. h> # include <string. h >#define N 201 double cnt [N], temp [N]; int main () {int n, m, l, r, I, j, sum1, w, sum2, pos; double ans; while (scanf ("% d", & n, & m, & l, & r )! = EOF & (n + m + l + r) {memset (cnt, 0, sizeof (cnt); memset (temp, 0, sizeof (temp )); temp [1] = 1; for (I = 1; I <= m; ++ I) {scanf ("% d", & w ); // m w for (j = 1; j <= n; ++ j) {if (temp [j] = 0) continue; // clockwise pos = j + w % n; // determine the position if (pos> n) {pos-= n ;} cnt [pos] + = 0.5 * temp [j]; // counter-clockwise pos = j-w % n; if (pos <= 0) {pos ++ = n ;} cnt [pos] + = 0.5 * temp [j];} for (j = 1; j <= n; ++ j) {temp [j] = cnt [j]; cnt [j] = 0 ;}} sum1 = sum2 = 0; ans = 0; for (I = l; I <= r; ++ I) {ans + = temp [I];} printf ("%. 4lf \ n ", ans);} return 0 ;}

 


Related Article

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.