[poj3744]Scout YYF I(概率dp+矩阵快速幂)
2017-08-29 17:31
375 查看
题意:在一维空间上存在一些雷,求安全通过的概率。其中人有$p$的概率前进一步,$1-p$的概率前进两步。
解题关键:若不考虑雷,则有转移方程:$dp[i] = p*dp[i - 1] + (1 - p)*dp[i - 2]$
由于雷的数量很少,所以可以以雷为界,将区域分开,在每个区域中,通过该段的概率等于1-踩到该段终点的地雷的概率。然后用矩阵快速幂优化一下即可
#include<cstdio> #include<cstring> #include<algorithm> #include<cstdlib> #include<cmath> #include<iostream> using namespace std; typedef long long ll; struct mat{ double m[2][2]; }ss; ll a[100]; mat mul(mat &A,mat &B){ mat C={0}; for(int i=0;i<2;i++){ for(int k=0;k<2;k++){ for(int j=0;j<2;j++){ C.m[i][j]+=A.m[i][k]*B.m[k][j]; } } } return C; } mat pow(mat A,ll n){ mat B={0}; B.m[0][0]=B.m[1][1]=1; while(n>0){ if(n&1) B=mul(B,A); A=mul(A,A); n>>=1; } return B; } int main(){ ll n; double p; while(scanf("%lld%lf",&n,&p)!=EOF){ for(int i=1;i<=n;i++) scanf("%lld",a+i); double ans=1.0; sort(a+1,a+n+1);//0位置看做有雷 for(int i=0;i<n;i++){ mat ss={p,1-p,1,0}; mat C=pow(ss,a[i+1]-a[i]-1); //ans*=(1-C.m[1][1]-p*C.m[1][0]); ans*=(1-C.m[0][0]); } printf("%.7f\n",ans); } return 0; }
相关文章推荐
- poj3744——Scout YYF I(概率DP+矩阵快速幂)
- poj3744 Scout YYF I 概率dp+矩阵快速幂
- POJ3744:Scout YYF I(概率dp & 矩阵优化)
- POJ 3744 Scout YYF(概率DP+矩阵快速幂)
- poj4474 Scout YYF I(概率dp+矩阵快速幂)
- poj3744 Scout YYF I 【概率dp+矩阵优化】
- poj 3744 Scout YYF I(概率dp+矩阵快速幂)
- POJ3744——概率DP 矩阵快速幂优化——Scout YYF I
- poj3744--Scout YYF I(概率dp第五弹:矩阵优化)
- poj 3744 Scout YYF I(概率dp+矩阵快速幂)
- poj 3744 Scout YYF I(概率dp+矩阵快速幂)
- [poj3744] Scout YYF I【概率dp 数学期望】
- poj4474 Scout YYF I(概率dp+矩阵快速幂)
- poj4474 Scout YYF I(概率dp+矩阵快速幂)
- POJ 3744 Scout YYF I 概率dp+矩阵快速幂
- POJ 3744 Scout YYF I 概率DP
- POJ3744----Scout YYF I
- POJ 3744 Scout YYF I 概率DP matrix 一开始考虑错误TLE了
- POJ 3744 Scout YYF I(概率+矩阵)
- 【POJ3744】Scout YYF I