poj3744 Scout YYF I[概率dp+矩阵优化]

Scout YYF I
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8598   Accepted: 2521

Description

YYF is a couragous scout. Now he is on a dangerous mission which is to penetrate into the enemy's base. After overcoming a series difficulties, YYF is now at the start of enemy's famous "mine road". This is a very long road, on which there are numbers of mines. At first, YYF is at step one. For each step after that, YYF will walk one step with a probability of p, or jump two step with a probality of 1-p. Here is the task, given the place of each mine, please calculate the probality that YYF can go through the "mine road" safely.

Input

The input contains many test cases ended with EOF.
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

For each test case, output the probabilty in a single line with the precision to 7 digits after the decimal point.

Sample Input

1 0.5
2
2 0.5
2 4

Sample Output

0.5000000
0.2500000

Source

题意:一条路上,有n个炸弹,给出每个炸弹的位置,一次走一步的概率是p,走两步的概率是1-p。求安全走完的概率。

//f[i]到达i点的概率
//f[i]=p*f[i-1]+(1-p)*f[i-2]
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int N=;
struct matrix{
double s[][];
matrix(){
memset(s,,sizeof s);
}
};
int n,num[N];double p;
matrix operator *(const matrix &a,const matrix &b){
matrix c;
for(int i=;i<;i++){
for(int j=;j<;j++){
for(int k=;k<;k++){
c.s[i][j]+=a.s[i][k]*b.s[k][j];
}
}
}
return c;
}
double fpow(matrix a,int p){
matrix res;
for(int i=;i<;i++) res.s[i][i]=;
for(;p;p>>=,a=a*a) if(p&) res=res*a;
return res.s[][];
}
int main(){
while(~scanf("%d%lf",&n,&p)){
for(int i=;i<=n;i++) scanf("%d",&num[i]);
sort(num+,num+n+);
matrix A;
A.s[][]=p;A.s[][]=1.0;
A.s[][]=1.0-p;A.s[][]=;
double ans=;
for(int i=;i<=n;i++){
ans*=(1.0-fpow(A,num[i]-num[i-]-));
}
printf("%.7f\n",ans);
}
return ;
}
上一篇:git将文件托管到github上遇到的问题


下一篇:POJ3744 Scout YYF I (矩阵优化的概率DP)