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 题意:一条长路有 N (1 ≤ N ≤ 10)颗地雷,一个人走一步的概率是 p ,走两步的概率是 (1-p) ,然后给出 N 颗地雷的位置 ,问这个人安全走过所有地雷的概率是多少 题解:对于一个位置x,设能走到的概率是 P(x) ,那么 P(x) = P(x-1)*p + P(x-2)*(1-p) 这个数x可能很大,所以需要矩阵快速幂
然后将整个的路看成由地雷分割的 N 段路
[0 -- x1]
[x1+1 -- x2]
[x2+1 -- x3]
... ...
所以,他能安全过去的概率就是 N 段都能过去的连乘
 #include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
#define MAXN 12 int n;
double p;
int bomb[MAXN]; double base[][];
double res[][]; //[ p(x) ] = [ p , 1-p ]^(x-1) * [ 1 ]
//[ p(x-1) ] [ 1 , 0 ] [ 0 ]
void quick_mi(int x)
{
double tp[][];
while (x)
{
if (x%==)
{
for (int i=;i<;i++)
for (int j=;j<;j++)
{
tp[i][j]=;
for (int k=;k<;k++)
tp[i][j]+=res[i][k]*base[k][j];
}
for (int i=;i<;i++)
for (int j=;j<;j++)
res[i][j]=tp[i][j];
}
for (int i=;i<;i++)
for (int j=;j<;j++)
{
tp[i][j]=;
for (int k=;k<;k++)
tp[i][j]+=base[i][k]*base[k][j];
}
for (int i=;i<;i++)
for (int j=;j<;j++)
base[i][j]=tp[i][j];
x/=;
}
} double Mi(int x)//处于位置1踩到位置 x 的概率
{
if (x==) return ;
base[][]=p,base[][]=1.0-p;
base[][]=,base[][]=;
res[][]=;res[][]=;
res[][]=;res[][]=;
quick_mi(x-);
return res[][];
} int main()
{
while (scanf("%d%lf",&n,&p)!=EOF)
{
for (int i=;i<n;i++)
scanf("%d",&bomb[i]);
sort(bomb,bomb+n); double xxx=Mi(bomb[]); //死了的概率
double ans = 1.0-xxx; //没死
for (int i=;i<n;i++)
{
xxx =Mi(bomb[i]-bomb[i-]); //化简后
ans *= (1.0-xxx);
}
printf("%.7lf\n",ans);
}
return ;
}

最新文章

  1. [Mongdb] 关于Replica Set复制集奇数成员限制的解释--待完善
  2. testMarkDown
  3. 前端人员一定要掌握的PS技巧
  4. 数据库性能调优——sql语句优化(转载及整理) —— 篇2
  5. ASP.NET Web API实现POST报文的构造与推送
  6. Ax 从一个form关闭另外一个form,AX全局变量
  7. Android LogCat 日志记录
  8. Learning WCF Chapter1 Generating a Service and Client Proxy
  9. python基础之语句结束
  10. js动态向页面中添加表格
  11. hdu 1233(还是畅通project)(prime算法,克鲁斯卡尔算法)(并查集,最小生成树)
  12. iOS截屏保存至相册
  13. scrapy架构初探
  14. 编程&amp;blog处女篇-用C#求100以内的质数
  15. the c programing language 学习过程4
  16. IdentityServer4 知多少
  17. Object Detection / Human Action Recognition 项目
  18. js设置document.domain实现跨域
  19. ASP.NET Core 集成测试中结合 WebApplicationFactory 使用 SQLite 内存数据库
  20. Java并发编程(二)-- 创建、运行线程

热门文章

  1. django开发环境部署之pip、virtualenv、virtualenvwrapper
  2. js获取上传图片的尺寸大小
  3. ES6里关于字符串的拓展
  4. nodejs - 根据用户地址不同 返回不同数据
  5. 页面加载后累加,自加1&amp;&amp;判断数字是否为两位数
  6. 简易选项卡&amp;&amp;简易JS年历
  7. [PIC32--IDE]使用MPLAB IDE调试
  8. Linux非阻塞IO(七)使用epoll重新实现客户端
  9. 《学习bash》笔记--进程处理
  10. 模式识别:利用MATLAB生成模式类