Scout YYF I
| Time Limit: 1000MS |
|
Memory Limit: 65536K |
| Total Submissions: 5565 |
|
Accepted: 1553 |
Description YYF is a couragous scout. Now he's on a dangerous mission which are to penetrate into the enemy ' s base. After overcoming a series difficulties, YYF are now at the start of enemy ' s famous "Mine Road". This was a very long road, on which there is numbers of mines. At first, YYF are at step one. For each step after, Yyf'll walk one step with a probability ofP, or jump to step with a probality of 1-P. Here's the task, given the place's mine, please calculate the probality that YYF can go through the "Mine Road" SAF Ely.Input The input contains many test cases ended withEOF. Each test case contains the lines. The first line of all test case isN(1≤N≤10) andP(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 all test case was n integer standing for the place of N mines. Each integer was in the range of [1, 100000000].Output For each test case, the output of the probabilty in A, a, and 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, Simonthis is titled Probability DP1. Finding probabilities:there are n mines, segmented processing, to find the probability of each step of the mine, P, then the probability of not stepping on the 1-p, and then multiply separately. the probability of a mine in each step is recursive: DN = p * dn-1 + (1-p) * DN-2;2. Recursive processing:because the data is too large, the direct recursive solution is easy to tle, so the recursive conversion to the matrix form (method http://www.cnblogs.com/sunus/p/4404273.html), and then the matrix fast power processing. Note: final [dn; dn-1] = [P, 1-p; 1, 0]^ (n-1) * [D1; d0];D1 = p;D0 = 1;therefore, [dn; dn-1] = [P, 1-p; 1, 0]^ (n-1) * [P; 1];therefore, DN = ([P, 1-p; 1, 0]^n) [0][0];#include <cstdio>#include<cstring>#include<algorithm>#defineN 55#defineRepu (I, A, b) for (int i = A; I < b; i++)using namespacestd;#defineMAXN 2#defineLL DoublellBase[MAXN] [MAXN] = {{1.0,0.0}, {0.0,1.0}};intMa[n];structmatrix{ll M[MAXN][MAXN];//two-dimensional array storage matrixMatrix (ll Num[maxn][maxn]) { for(inti =0; i < MAXN; i++) for(intj =0; J < Maxn; J + +) M[i][j]=Num[i][j]; } //initialization of an arrayMatrix () {}}; Matrixoperator*(Matrix M1, matrix m2) {intI, j, K; Matrix temp; for(i =0; i < MAXN; i++) { for(j =0; J < Maxn; J + +) {Temp.m[i][j]=0; for(k =0; K < MAXN; k++) Temp.m[i][j]+ = (m1.m[i][k] * m2.m[k][j]);//% MoD;//temp.m[i][j]%= mod;//Note that each step is modeled } } returntemp;} Matrix Quickpow (Matrix M,intN) {Matrix Tempans (Base);//initialize to the unit matrix while(n) {if(N &1) Tempans= Tempans * M;//It's overloaded *n = n >>1; M= M *M; } //fast Power (similar to integer) returnTempans;}intMain () {intN; ll P; while(~SCANF ("%d", &N)) {scanf ("%LF", &p); Matrix M, C; m.m[0][0] =p; m.m[0][1] =1.0-p; m.m[1][0] =1.0; m.m[1][1] =0.0; intLast , rear; DoubleP =1.0; ma[0] =0; Repu (i,1, n +1) scanf ("%d", &Ma[i]); Sort (MA, MA+ N +1); Repu (i,1, n +1) {C= Quickpow (M, Ma[i]-ma[i-1] -1); P*= (1.0-c.m[0][0]); } if(ma[1] ==1) P =0.0; printf ("%.7lf\n", P); } return 0;}View Code |