Aeroplane chess
Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 361 accepted submission (s): 255
Problem descriptionhzz loves aeroplane chess very much. the chess map contains N + 1 grids labeled from 0 to n. hzz starts at grid 0. for each step he throws a dice (a dice have six faces with equal probability to face up and the numbers on the faces are 1, 2, 4, 5, 6 ). when hzz is at grid I and the dice number is X, he will moves to grid I + X. hzz finishes the game when I + X is equal to or greater than N.
There are also m flight lines on the chess map. the I-th flight line can help hzz fly from grid XI to Yi (0 <xi <Yi <= N) without throwing the dice. if there is another flight line from Yi, hzz can take the flight line continuously. it is granted that there is no two or more flight lines start from the same grid.
Please help hzz calculate the expected dice throwing times to finish the game.
Inputthere are multiple test cases.
Each test case contains several lines.
The first line contains two integers n (1 ≤ n ≤ 100000) and M (0 ≤ m ≤ 1000 ).
Then M lines follow, each line contains two integers Xi, Yi (1 ≤ xi <Yi ≤ n ).
The input end with n = 0, m = 0.
Outputfor each test case in the input, you shocould output a line indicating the expected dice throwing times. Output shocould be rounded to 4 digits after decimal point.
Sample input2 0 8 3 2 4 5 7 8 0 0
Sample output1.1667 2.3441
Source2012 ACM/ICPC Asia Regional Jinhua online
Recommendzhoujiaqi2010
This is a simple question about probability DP during the competition. At that time, there was no probability of DP, and the probability of no DP was too weak ~~~~~~~
/* The probability DP is expected. Form a directed acyclic graph. You can follow the formula for recursion. DP [I] indicates the number of expected steps from point I to the target State. */ # Include <Stdio. h> # Include <Iostream> # Include <Algorithm> # Include < String . H> # Include <Vector> Using Namespace STD; Const Int Maxn = 100010 ; Double DP [maxn]; vector < Int >VEC [maxn]; Bool Used [maxn]; Int Main (){ Int N, m; Int U, V; While (Scanf ( " % D " , & N ,& M )){ If (N = 0 & M = 0 ) Break ; For ( Int I = 0 ; I <= N; I ++ ) VEC [I]. Clear (); memset (DP, 0 , Sizeof (DP )); While (M -- ) {Scanf ( " % D " , & U ,& V); VEC [v]. push_back (U);} memset (used, False , Sizeof (Used )); For ( Int I = 0 ; I <VEC [N]. Size (); I ++ ) {V = VEC [N] [I]; DP [v] = 0 ; Used [v] = True ;} For ( Int I = N- 1 ; I> = 0 ; I -- ){ If (Used [I] = False ){ For ( Int J = I + 1 ; J <= I + 6 ; J ++) DP [I] + = DP [J]/6 ; DP [I] + = 1 ; Used [I] = True ;} For ( Int J = 0 ; J <VEC [I]. Size (); j ++ ) {V = VEC [I] [J]; DP [v] = DP [I]; used [v] = True ;} Printf ( " %. 4lf \ n " , DP [ 0 ]);} Return 0 ;}