隐藏页面特效

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=20; struct matrix{ double s[2][2]; matrix(){ memset(s,0,sizeof s); } }; int n,num[N];double p; matrix operator *(const matrix &a,const matrix &b){ matrix c; for(int i=0;i<2;i++){ for(int j=0;j<2;j++){ for(int k=0;k<2;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=0;i<2;i++) res.s[i][i]=1; for(;p;p>>=1,a=a*a) if(p&1) res=res*a; return res.s[0][0]; } int main(){ while(~scanf("%d%lf",&n,&p)){ for(int i=1;i<=n;i++) scanf("%d",&num[i]); sort(num+1,num+n+1); matrix A; A.s[0][0]=p;A.s[0][1]=1.0; A.s[1][0]=1.0-p;A.s[1][1]=0; double ans=1; for(int i=1;i<=n;i++){ ans*=(1.0-fpow(A,num[i]-num[i-1]-1)); } printf("%.7f\n",ans); } return 0; }

 


__EOF__

本文作者shenben
本文链接https://www.cnblogs.com/shenben/p/6730684.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   神犇(shenben)  阅读(181)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示