题目分析
占坑
代码
#include <cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
using namespace std;
const int maxr=100000000+5;
const int maxn=12;
struct mat
{
double e[2][2];
mat(){e[0][0]=e[1][1]=1,e[0][1]=e[1][0]=0;}
mat& operator*(mat& x)
{
mat ans= *this;
for(int i=0;i<=1;i++)
for(int j=0;j<=1;j++)
ans.e[i][j]=e[i][0]*x.e[0][j]+e[i][1]*x.e[1][j];
return ans;
}
};
double qpow(mat a,int p)
{
mat ans;
for(;p;p>>=1)
{
if(p&1) ans = ans*a;
a=a*a;
}
return ans.e[0][0];
}
int n;
double p;
long long a[maxn];
mat M;
int main()
{
while(cin>>n>>p)
{
M.e[0][0]=p,M.e[0][1]=1-p,M.e[1][0]=1,M.e[1][1]=0;
a[0]=0;
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
sort(a+1,a+n+1);
n=unique(a+1,a+n+1)-a-1;
double ans=1;
for(int i=1;i<=n;i++)
ans=ans*(1-qpow(M,a[i]-a[i-1]-1));
printf("%.7f\n",ans);
}
return 0;
}

本文介绍了一种使用矩阵快速幂的方法来解决特定的概率计算问题。通过定义特定的矩阵运算及快速幂函数,可以高效地计算出经过一系列状态转移后的最终概率。文章提供了完整的代码实现,并解释了关键步骤。
2152

被折叠的 条评论
为什么被折叠?



