这题主要是个概率dp,然后加个矩阵快速幂优化
很容易想到dp[i] = dp[i-1]*p+dp[i-2]*(1-p);
然后那个范围是
108
所以直接dp会超时的
需要矩阵快速幂优化。
分段去做快速幂
p 1-p
1 0
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <bitset>
#include <map>
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define ROF(i,a,b) for(int i=a;i>=b;i--)
#define mem(i,a) memset(i,a,sizeof(i))
#define rson mid+1,r,rt<<1|1
#define lson l,mid,rt<<1
#define mp make_pair
#define pb push_back
#define ll long long
#define LL long long
using namespace std;
template <typename T>inline void read(T &_x_){
_x_=0;bool f=false;char ch=getchar();
while (ch<'0'||ch>'9') {if (ch=='-') f=!f;ch=getchar();}
while ('0'<=ch&&ch<='9') {_x_=_x_*10+ch-'0';ch=getchar();}
if(f) _x_=-_x_;
}
const double eps = 0.0000001;
const int maxn = 3e2+7;
const int mod = 1e9+7;
const ll inf = 1e15;
int n;
double p,ret=1.0;
int pos[15];
struct matrix{
double m[2][2];
}ans,bs;
matrix multi(matrix a,matrix b){
matrix c;
for(int i = 0; i < 2; i++){
for(int j = 0; j< 2; j++){
c.m[i][j] = 0;
for(int k = 0; k< 2; k++){
c.m[i][j] = (c.m[i][j]+a.m[i][k]*b.m[k][j]);
}
}
}
return c;
}
void fast_mod(int n){
bs.m[0][0]=p;bs.m[1][0]=1-p;
bs.m[0][1]=1;bs.m[1][1]=0;
ans.m[0][0]=ans.m[1][1]=1;
ans.m[0][1]=ans.m[1][0]=0;
while(n){
if(n&1)
ans=multi(ans,bs);
bs=multi(bs,bs);
n>>=1;
}
ret*=(1-ans.m[0][0]);
}
int main(){
while(~scanf("%d%lf",&n,&p)){
FOR(i,1,n) scanf("%d",&pos[i]);
sort(pos+1,pos+1+n);
ret=1.0;
for(int i=1;i<=n;i++){
fast_mod(pos[i]-pos[i-1]-1);
}
printf("%.7f\n",ret);
}
return 0;
}