Scout yyf I
Time limit:1000 ms |
|
Memory limit:65536 K |
Total submissions:3723 |
|
Accepted:928 |
Description
Yyf is a couragous scout. now he is on a dangerous mission which is to penetrate into the enemy's base. after overcoming a series difficulties, yyf is now at the start of enemy's famous "Mine Road ". this is a very long road, on which there are numbers of mines. at first, yyf is at step one. for each step after that, yyf will walk one step with a probability
P , Or jump two step with a probality of 1-
P . Here is the task, given the place of each mine, Please calculate the probality that yyf can go through the "Mine Road" safely.
Input
The input contains into test cases ended
EOF .
Each test case contains two lines.
The first line of each test case is
N (1 ≤
N ≤ 10) and
P (Less than or equal to 0.25
P ≤ 0.75) seperated by a single blank, standing for the number of mines and the probability to walk one step.
The second line of each test case is n integer standing for the place of N mines. Each integer is in the range of [1, 100000000].
Output
For each test case, output the probabilty in a single line with the precision to 7 digits after the decimal point.
Sample Input
1 0.522 0.52 4
Sample output
0.50000000.2500000
Source
Poj monthly contest-2009.08.23, Simon Question: on the road to being dissatisfied with mines, you are now starting at one place. Mines are deployed at N points, 1 <= n <= 10. Coordinate range of a mine point: [1,]. The probability of each forward P goes one step forward, and the probability of 1-P goes one-P step forward. Ask the probability of passing this path. That is, do not go to a place with mines. If DP [I] is set to indicate the probability of reaching the I point, the initial value DP [1] = 1. It is easy to think of the transfer equation: DP [I] = p * DP [I-1] + (1-p) * DP [I-2]; However, due to the large range of coordinates, it is impossible to directly do so, and some points in it still have mines. The coordinates of N mine points are X [1], X [2], X [3] ''' X [N]. We divide the road into N segments: 1 ~ X [1]; X [1] + 1 ~ X [2]; X [2] + 1 ~ X [3]; ` ` ` X [N-1] + 1 ~ X [N]. In this way, each segment has only one mine. We only need to obtain the probability of passing through each segment. Multiplication is the answer. For each segment, the probability of passing through the segment is equal to 1-the probability of stepping on the end of the segment. For example, the first section 1 ~ X [1]. The passing of this section is actually equivalent to reaching X [1] + 1. Then P [x [1] + 1] = 1-P [x [1]. However, the premise is P [1] = 1, that is, the probability of the starting point is equal to 1. We also share the same assumptions for the following sections. In this way, the answer is multiplied. The probability of each section can be quickly obtained through matrix multiplication. Code:
/* Poj 3744c ++ 0 Ms 184 K */ # Include <Stdio. h> # Include < String . H> # Include <Algorithm> # Include <Iostream> # Include <Math. h> Using Namespace STD; Struct Matrix { Double Mat [ 2 ] [ 2 ] ;}; Matrix MUL (matrix A, matrix B) {matrix ret; For ( Int I = 0 ; I < 2 ; I ++ ) For (Int J = 0 ; J < 2 ; J ++ ) {Ret. Mat [I] [J] = 0 ; For ( Int K = 0 ; K < 2 ; K ++ ) Ret. Mat [I] [J] + = A. Mat [I] [k] * B. Mat [k] [J];} Return RET;} matrix pow_m (matrix, Int N) {matrix ret; memset (Ret. mat, 0 , Sizeof (Ret. Mat )); For ( Int I = 0 ; I < 2 ; I ++) ret. Mat [I] [I] = 1 ; Matrix temp = A; While (N ){ If (N & 1 ) Ret = Mul (Ret, temp); temp = Mul (temp, temp); n >>= 1 ;} Return RET ;} Int X [ 30 ]; Int Main (){ Int N; Double P; While (Scanf ( " % D % lf " , & N, & P )! = EOF) // Change g ++ on poj to CIN input. { For ( Int I = 0 ; I <n; I ++ ) Scanf ( " % D " ,& X [I]); sort (x, x + N ); Double Ans = 1 ; Matrix tt; TT. Mat [ 0 ] [ 0 ] = P; TT. Mat [ 0 ] [ 1 ] = 1 - P; TT. Mat [ 1 ] [ 0 ] = 1 ; TT. Mat [ 1 ] [ 1 ] = 0 ; Matrix temp; temp = Pow_m (TT, X [ 0 ]- 1 ); Ans * = ( 1 -Temp. Mat [ 0 ] [ 0 ]); For ( Int I = 1 ; I <n; I ++ ){ If (X [I] = x [I- 1 ]) Continue ; Temp = Pow_m (TT, X [I]-X [I- 1 ]- 1 ); Ans * = ( 1 -Temp. Mat [ 0 ] [ 0 ]);} Printf ( " %. 7lf \ n " , ANS ); // Change g ++ on poj to %. 7f } Return 0 ;}