Description
Input
Each test case contains two lines.
The First line of each test case is N (1 ≤ N ≤ 10) and p (0.25 ≤ p ≤ 0.75) seperated by a single blank, standing for the number of mines and the probability to walk one step.
The Second line of each test case is N integer standing for the place of N mines. Each integer is in the range of [1, 100000000].
Output
題意:
一條路上每個點標號爲1,2,3....,你一開始站在1往正方向出發, 有P的概率走到下一格,(1-P)的概率走到下一格的下一格,現在路上可能會有一些地雷,給出N個地雷的座標,問你不踩地雷走過這條路的概率。
範圍:
地雷數小於等於10,座標小於10憶
解法:
OXOOOOX
若O表示可以走的,X表示有地雷,那麼走過這段路可以分成三個部分
第一部分:座標1->座標3 這一階段達成的概率是1-p
第二部分:座標3->座標6
第三部分:座標6->座標8 這一階段達成的概率是1-p
可以發現,只要求出第二部分的概率即可,這一部分的概率可以DP
記F[i]爲走過長度爲i的沒有地雷的路程的概率,那麼公式爲f[i]=f[i-1]*p+f[i-2]*(1.0-p);
但是因爲I可能很大,需要考慮對它進一步分析,最後發現i趨於無窮時,F[I]是有極限的,實際過程中這個極限很快就會達到。
所以處理出大約前1000個F[I],大於1000的取F[1000],然後按階段處理即可。
代碼:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<iostream>
#include<stdlib.h>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<bitset>
#pragma comment(linker, "/STACK:1024000000,1024000000")
template <class T>
bool scanff(T &ret){ //Faster Input
char c; int sgn; T bit=0.1;
if(c=getchar(),c==EOF) return 0;
while(c!='-'&&c!='.'&&(c<'0'||c>'9')) c=getchar();
sgn=(c=='-')?-1:1;
ret=(c=='-')?0:(c-'0');
while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
if(c==' '||c=='\n'){ ret*=sgn; return 1; }
while(c=getchar(),c>='0'&&c<='9') ret+=(c-'0')*bit,bit/=10;
ret*=sgn;
return 1;
}
#define inf 1073741823
#define llinf 4611686018427387903LL
#define PI acos(-1.0)
#define lth (th<<1)
#define rth (th<<1|1)
#define rep(i,a,b) for(int i=int(a);i<=int(b);i++)
#define drep(i,a,b) for(int i=int(a);i>=int(b);i--)
#define gson(i,root) for(int i=ptx[root];~i;i=ed[i].next)
#define tdata int testnum;scanff(testnum);for(int cas=1;cas<=testnum;cas++)
#define mem(x,val) memset(x,val,sizeof(x))
#define mkp(a,b) make_pair(a,b)
#define findx(x) lower_bound(b+1,b+1+bn,x)-b
#define pb(x) push_back(x)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
int n,a[1111];
double p,f[1111];
int main(){
while(scanf("%d%lf",&n,&p)!=EOF){
mem(f,0);
f[1]=1.0;
rep(i,2,1000){
f[i]=f[i-1]*p+f[i-2]*(1.0-p);
}
double ans=1.0;
rep(i,1,n)scanff(a[i]);
sort(a+1,a+1+n);
int an=unique(a+1,a+1+n)-a-1;
a[0]=0;
rep(i,1,an){
int x=a[i];
int dis=min(a[i]-a[i-1]-1,1000);
ans=ans*f[dis]*(1.0-p);
}
printf("%.7f\n",ans);
}
return 0;
}