Test instructions: On a mine-covered road, you are now starting at 1. There are mines at N points, 1<=n<=10. The coordinate range of the mine point: [1,100000000].
each advance to the probability of P before further, 1-p the probability of advancing 1-p step. Ask the probability of a smooth passage through this route. is not to go to a place where there is a mine. Set Dp[i] indicates 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];but because of the large range of coordinates, it is not possible to do so directly, and some of the points still exist in mines. N The coordinates of a mine point are x[1],x[2],x[3] " " "X[n".we divide the road into n sections:1~x[1];x[1]+1~x[2];x[2]+1~x[3];` X[n-1]+1~x[n].Transfer matrix:
Dp[i] | P, 1-p | DP[I-1]
=| |*
DP[I-1] | 1, 0 | Dp[i-2]so there is only one mine per paragraph. We just have to get the probability of passing each paragraph. Multiplying the principle of multiplication is the answer. for each paragraph, the probability of passing through the segment equals 1-the probability of stepping on the mine at the end of the segment. just like the first paragraph 1~x[1]. The passage is actually equivalent to reaching the x[1]+1 point. Then P[x[1]+1]=1-p[x[1]].but this premise is p[1]=1, that is, the probability of starting point equals 1. For the next paragraph we are the same hypothesis, so that is the answer to multiply. The method of calculating the probability of each section can be quickly obtained by matrix multiplication.
1 /*2 POJ 37443 4 C + + 0ms 184K5 */6#include <stdio.h>7#include <string.h>8#include <algorithm>9#include <iostream>Ten#include <math.h> One using namespacestd; A - structMatrix - { the Doublemat[2][2]; - }; - Matrix Mul (Matrix A,matrix B) - { + Matrix ret; - for(intI=0;i<2; i++) + for(intj=0;j<2; j + +) A { atret.mat[i][j]=0; - for(intk=0;k<2; k++) -ret.mat[i][j]+=a.mat[i][k]*B.mat[k][j]; - } - returnret; - } inMatrix Pow_m (Matrix A,intN) - { to Matrix ret; +memset (Ret.mat,0,sizeof(Ret.mat)); - for(intI=0;i<2; i++) ret.mat[i][i]=1; theMatrix temp=A; * while(n) $ {Panax Notoginseng if(n&1) ret=Mul (ret,temp); -temp=Mul (temp,temp); then>>=1; + } A returnret; the } + - intx[ -]; $ intMain () $ { - intN; - Doublep; the while(SCANF ("%D%LF", &n,&p)!=eof)//POJ on g++ to be changed to cin input - {Wuyi for(intI=0; i<n;i++) thescanf"%d",&x[i]); -Sort (x,x+n); Wu Doubleans=1; - Matrix tt; Abouttt.mat[0][0]=p; $tt.mat[0][1]=1-p; -tt.mat[1][0]=1; -tt.mat[1][1]=0; - Matrix temp; A +Temp=pow_m (tt,x[0]-1); theAns*= (1-temp.mat[0][0]); - $ for(intI=1; i<n;i++) the { the if(x[i]==x[i-1])Continue; theTemp=pow_m (tt,x[i]-x[i-1]-1); theAns*= (1-temp.mat[0][0]); - } inprintf"%.7lf\n", ans);//POJ on g++ to change to%.7f the } the return 0; About}
POJ 3744 probability dp+ matrix fast Power