[poj3744]Scout YYF I(概率dp+矩阵快速幂)

题意:在一维空间上存在一些雷,求安全通过的概率。其中人有$p$的概率前进一步,$1-p$的概率前进两步。

解题关键:若不考虑雷,则有转移方程:$dp[i] = p*dp[i - 1] + (1 - p)*dp[i - 2]$

由于雷的数量很少,所以可以以雷为界,将区域分开,在每个区域中,通过该段的概率等于1-踩到该段终点的地雷的概率。然后用矩阵快速幂优化一下即可

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<cstdlib>
 5 #include<cmath>
 6 #include<iostream>
 7 using namespace std;
 8 typedef long long ll;
 9 struct mat{
10     double m[2][2];
11 }ss;
12 ll a[100];
13 mat mul(mat &A,mat &B){
14     mat C={0};
15     for(int i=0;i<2;i++){
16         for(int k=0;k<2;k++){
17             for(int j=0;j<2;j++){
18                 C.m[i][j]+=A.m[i][k]*B.m[k][j];
19             }
20         }
21     }
22     return C;
23 }
24 mat pow(mat A,ll n){
25     mat B={0};
26     B.m[0][0]=B.m[1][1]=1;
27     while(n>0){
28         if(n&1) B=mul(B,A);
29         A=mul(A,A);
30         n>>=1;
31     }
32     return B;
33 }
34 
35 int main(){
36     ll n;
37     double p;
38     while(scanf("%lld%lf",&n,&p)!=EOF){
39         for(int i=1;i<=n;i++)    scanf("%lld",a+i);
40         double ans=1.0;
41         sort(a+1,a+n+1);//0位置看做有雷 
42         for(int i=0;i<n;i++){
43             mat ss={p,1-p,1,0};
44             mat C=pow(ss,a[i+1]-a[i]-1);
45             //ans*=(1-C.m[1][1]-p*C.m[1][0]);
46             ans*=(1-C.m[0][0]);
47         }
48         printf("%.7f
",ans);
49     }
50     return 0;
51 }
原文地址:https://www.cnblogs.com/elpsycongroo/p/7449395.html