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 ;
}

Scout YYF I (概率+矩阵快速幂)的更多相关文章

  1. POJ 3744 Scout YYF I(矩阵快速幂优化+概率dp)

    http://poj.org/problem?id=3744 题意: 现在有个屌丝要穿越一个雷区,雷分布在一条直线上,但是分布的范围很大,现在这个屌丝从1出发,p的概率往前走1步,1-p的概率往前走2 ...

  2. POJ3744Scout YYF I(求概率 + 矩阵快速幂)

    Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6757   Accepted: 1960 Descr ...

  3. 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 Coin 概率+矩阵快速幂

    题目链接: https://nanti.jisuanke.com/t/17115 题意: 询问硬币K次,正面朝上次数为偶数. 思路: dp[i][0] = 下* dp[i-1][0] + 上*dp[i ...

  4. poj4474 Scout YYF I(概率dp+矩阵快速幂)

    Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4100   Accepted: 1051 Descr ...

  5. poj 3744 Scout (Another) YYF I - 概率与期望 - 动态规划 - 矩阵快速幂

      (Another) YYF is a couragous scout. Now he is on a dangerous mission which is to penetrate into th ...

  6. POJ 3744 Scout YYF I 概率dp+矩阵快速幂

    题目链接: http://poj.org/problem?id=3744 Scout YYF I Time Limit: 1000MSMemory Limit: 65536K 问题描述 YYF is ...

  7. 刷题总结—— Scout YYF I(poj3744 矩阵快速幂+概率dp)

    题目: Description YYF is a couragous scout. Now he is on a dangerous mission which is to penetrate int ...

  8. Scout YYF I POJ - 3744(概率dp + 矩阵快速幂)

    题意: 一条路上有n个地雷,你从1开始走,单位时间内有p的概率走一步,1-p的概率走两步,问安全通过这条路的概率 解析: 很容易想到 dp[i] = p * dp[i-1] + (1 - p) * d ...

  9. POJ 3744 【矩阵快速幂优化 概率DP】

    搞懂了什么是矩阵快速幂优化.... 这道题的重点不是DP. /* 题意: 小明要走某条路,按照个人兴致,向前走一步的概率是p,向前跳两步的概率是1-p,但是地上有地雷,给了地雷的x坐标,(一维),求小 ...

随机推荐

  1. C++ 获取URL图片、html文件,CInternetSession 【转】

    http://blog.csdn.net/gnixuyil/article/details/7688439 获取网络图片 CString URL="http://www.google.com ...

  2. iframe.contentWindow 属性:关于contentWindow和contentDocument区分

    定义和用法 contentDocument 属性能够以 HTML 对象来返回 iframe 中的文档,可以通过所有标准的 DOM 方法来处理被返回的对象. 语法:frameObject.content ...

  3. Solr 创建core 从MySql数据库中导入数据

    一.创建数据表和数据 在MySql数据中创建mysolrInfo表, 创建字段 id 主键,自动增加 pname :姓名 age :年龄 addtime :增加时间 增加几条数据 二.创建core 当 ...

  4. [转]js模块化编程之彻底弄懂CommonJS和AMD/CMD!

    原文: https://www.cnblogs.com/chenguangliang/p/5856701.html ------------------------------------------ ...

  5. SELinux的Docker安全性

    原文译自:http://opensource.com/business/14/7/docker-security-selinux 这篇文章基于我今年在DockerCon一个讲座,它将讨论我们当前听到的 ...

  6. 有关CGRectGetMinX,CGRectGetMidX,CGRectGetMaxX图解

    CGRect size = CGRectMake(20, 20, 400, 400); //矩形中最小x值 ,size.x CGRectGetMinX(size) = 20; //矩形中最小y值 ,s ...

  7. Android学习(一) 按钮的事件

    用户登录 1.内部匿名类方式实现 layout <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/an ...

  8. Php函数之end

    Php函数之end end()函数 (PHP 4, PHP 5, PHP 7) end - 将数组的内部指针指向最后一个单元 说明 mixed end ( array &$array ) en ...

  9. <转>创建支持eclipse的多模块maven项目

    如何使用eclipse创建Maven工程及其子模块 1,首先创建一个父类工程   子模块继承父类工程      并在父类工程的pom.xml文件中定义引入的jar及其版本号     子模块可以引用 2 ...

  10. C语言可以给字符数组赋值的方法

    分类: C 2012-04-06 10:23 4081人阅读 评论(0) 收藏 举报 语言c 学了这么多年的C语言,突然发现连字符串赋值都出错,真的很伤心. char a[10]; 怎么给这个数组赋值 ...