Aeroplane chess
Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 229 accepted submission (s): 158
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 is a common mathematical expectation. Recurrence from the back.
// 1006 # Include <stdio. h> # Include <Iostream> # Include <Map> # Include < Set ># Include <Algorithm> # Include < String . H> # Include <Stdlib. h> Using Namespace STD; Const Int Maxn = 100020 ; Struct Node { Int From ,; Int Next;} edge [maxn]; Int Head [maxn]; Int Tol; Void Add ( Int U, Int V) {edge [tol]. From = U; edge [tol]. = V; edge [tol]. Next = Head [u]; head [u] = Tol ++ ;} Double DP [maxn]; Bool Vis [maxn]; Int Main (){ // Freopen ("F. In", "r", stdin ); // Freopen ("F. Out", "W", stdout ); Int N, m; Int U, V; While (Scanf (" % D " , & N, & M )! = EOF ){ If (N = 0 & M = 0 ) Break ; Memset (DP, 0 , Sizeof (DP); memset (Head, - 1 , Sizeof (Head); memset (VIS, False , Sizeof (VIS); Tol = 0 ; While (M -- ) {Scanf ( " % D " , & U ,& V); add (v, U);} DP [N] =- 1 ; // Printf ("% d %. 4lf \ n", N, DP [N]); For ( Int I = N; I> = 0 ; I -- ){ If (! Vis [I]) {DP [I] + = 1 ; For ( Int J = I + 1 ; J <= I + 6 ; J ++ ) DP [I] + = (DP [J]/ 6.0 ); Vis [I] = True ;} For ( Int J = head [I]; J! =- 1 ; J = Edge [J]. Next ){ Int V = Edge [J]. To; DP [v] = DP [I]; vis [v] =True ;} // Printf ("% d %. 4lf \ n", I, DP [I]); } Printf ( " %. 4lf \ n " , DP [ 0 ]);} Return 0 ;}