Time limit:1000 ms |
|
Memory limit:65536 K |
Total submissions:5062 |
|
Accepted:1370 |
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
Meaning: Everyone starts from 1. The probability of P goes through 1 step. The probability of 1-P goes through 2 steps to find the probability of not stepping on thunder. Number of mines n <= 10, and the coordinate range is within 100000000.
Train of Thought: PeopleThe probability of going to the I position isDP [I] = DP [I-1] * P + dp [I-2] * (1-p), due to the large data range, can have a matrix power to quickly find the answer;
Constructor | P, 1 | I .e. | DP [n + 1], DP [N] | =| DP [N], DP [n-1] | * | P, 1 |
| 1-P, 0 | | 1-P, 0 |
Then we can treat each a [I] + 1 to a [I + 1] as a separate processing.
# Include <iostream> # include <algorithm> # include <cstdio> using namespace STD; const int maxn = 100000050; struct node {double mat [2] [2];}, b, C; double DP [maxn], p; int n, a [15]; void input () {for (INT I = 0; I <n; I ++) scanf ("% d", & A [I]); sort (A, A + n);. mat [0] [0] = P,. mat [0] [1] = 1.0;. mat [1] [0] = 1.0-p,. mat [1] [1] = 0.0;} node MUL (node P, node q) // matrix multiplication {node ans; For (INT I = 0; I <2; I ++) for (Int J = 0; j <2; j ++) {ans. mat [I] [J] = 0.0; For (int K = 0; k <2; k ++) ans. mat [I] [J] + = P. mat [I] [k] * q. mat [k] [J];} return ans;} node POW (node W, int num) // fast power {node ret = B, c = W; int coun = num; while (coun) {If (coun & 1) ret = MUL (Ret, c); coun> = 1; C = MUL (C, c);} return ret;} void solve () {int now = 1; double F1 = 0.0, f2 = 1.0; For (INT I = 0; I <N; I ++) {if (a [I]-Now> = 1) {node TMP = POW (a, a [I]-1-now ); f2 = (F2 * TMP. mat [0] [0] + F1 * TMP. mat [1] [0]) * (1.0-p); F1 = 0.0; now = [I] + 1;} else {printf ("0.0000000 \ n"); Return ;}} printf ("%. 7f \ n ", F2);} int main () {B. mat [0] [0] = 1.0, B. mat [0] [1] = 0.0; B. mat [2] [0] = 0.0, B. mat [1] [1] = 1.0; while (scanf ("% d % lf", & N, & P )! = EOF) {input (); solve ();} return 0 ;}
Poj 3744 scout yyf I (probability DP + rapid matrix power)