A line segment, the starting point is 1, there are some points can not go, each time the probability of p to go to the right 1 steps, or the probability of (1-P) 2 steps to the right.
The probability of going through the whole line safely
First, if 1 can not walk the dog belt, if there are 2 consecutive points can not walk the dog belt
For all points can go, dp[i] represents the probability of walking to I, dp[i]=dp[i-1]*p+dp[i-1]* (p-2)
The matrix {{p,1-p},{1,0}} can be constructed, and the DP is optimized by the matrix fast Power
Segment by points that cannot be walked
1#include <cstdio>2#include <algorithm>3 using namespacestd;4 inta[ the];5 structmatrix{6 Doublea[2][2];7 voidClear () {a[0][0]=a[1][1]=1, a[0][1]=a[1][0]=0; }8Matrixoperator*(Matrix t) {9 Matrix sum;Ten for(intI=0;i<2; i++) One for(intj=0;j<2; j + +){ Asum.a[i][j]=0; - for(intk=0;k<2; k++) -sum.a[i][j]+=a[i][k]*T.a[k][j]; the } - returnsum; - } - }; + DoubleQP (Matrix BS,intx) { - Matrix sum; sum.clear (); + while(x) { A if(x&1) sum=sum*BS; atbs=bs*bs,x>>=1; - } - returnsum.a[0][0]; - } - intMain () { - intNDoublep; in while(SCANF ("%d", &n)! =EOF) { -scanf"%LF",&p); to for(intI=1; i<=n;i++) +scanf"%d",&a[i]); -Sort (A +1, A +1+n); the BOOLflag=a[1]==1?true:false; * for(intI=2; i<=n;i++) $ if(a[i]==a[i-1]+1) flag=true;Panax Notoginseng Doubleans; - if(a[1]==1) flag=true; the if(flag) ans=0; + Else{ A Matrix F; thef.a[0][0]=p,f.a[0][1]=1-p; +f.a[1][0]=1, f.a[1][1]=0; -ans=1;intlst=0; $ for(intI=1; i<=n;i++){ $Ans*= (1-p) *QP (f,a[i]-lst-2); -lst=A[i]; - } the } -printf"%.7f\n", ans);//Wuyi } the return 0; -}
View Code
[POJ 3774] Scout YYF I