Scout YYF I
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4410   Accepted: 1151

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
/*分析:对于n个地雷s[1],s[2],s[3],s[4]...s[n]
如果s都是不递减的。 如果dp[i]表示从1到达i这个位置的概率
则:
dp[s[1]-1]为1~s[1]-1的概率//s[1]不能到达
dp[s[2]-1]为1~s[2]-1也是1->s[1]-1->s[1]+1->s[2]-1的概率
因为最多仅仅能跳两格
所以dp[s[i]+1]一定是从dp[s[i]-1]到达
然后从dp[s[i]+1]到达dp[s[i+1]-1];//这部分就能够用矩阵高速幂
另外依据公式dp[i]=p*dp[i-1]+(1-p)*dp[i-2]也可知从s[i]+1 => s[i+1]-1用矩阵高速幂求
构造初始矩阵:
p 1-p * dp[i] = dp[i+1]
1 0 dp[i-1] dp[i]
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <iomanip>
#define INF 99999999
typedef long long LL;
using namespace std; const int MAX=10+10;
const int N=2;
int n,s[MAX];
double array[N][N],sum[N][N],p; void InitMatrix(){
array[0][0]=p;
array[0][1]=1-p;
array[1][0]=1;
array[1][1]=0;
for(int i=0;i<N;++i){
for(int j=0;j<N;++j)sum[i][j]=(i == j);
}
} void MatrixMult(double a[N][N],double b[N][N]){
double c[N][N]={0};
for(int i=0;i<N;++i){
for(int j=0;j<N;++j){
for(int k=0;k<N;++k){
c[i][j]+=a[i][k]*b[k][j];
}
}
}
for(int i=0;i<N;++i)for(int j=0;j<N;++j)a[i][j]=c[i][j];
} double Matrix(int k){
if(k<0)return 0;//表示s[i-1]~s[i]之间无位置
InitMatrix();//初始化矩阵
while(k){//有k+1个位置,到达第k+1个位置所以是k次
if(k&1)MatrixMult(sum,array);
MatrixMult(array,array);
k>>=1;
}
return sum[0][0];//sum[0][0]*dp[1]+sum[0][1]*dp[0]
} int main(){
while(~scanf("%d%lf",&n,&p)){
for(int i=1;i<=n;++i)scanf("%d",&s[i]);
sort(s+1,s+1+n);
double ans=Matrix(s[1]-2);//1~s[1]-1的概率
for(int i=2;i<=n;++i){
if(s[i] == s[i-1])continue;
double temp=Matrix(s[i]-s[i-1]-2);//s[i-1]~s[i]之间有s[i]-s[i-1]-1个位置,须要走s[i]-s[i-1]-2次到达最后一个位置
ans=ans*(1-p)*temp;//从s[i-1]-1的位置跳两格到s[i-1]+1再到s[i]-1
}
printf("%.7f\n",ans*(1-p));//在s[n]-1位置还须要跳两格才安全了
}
return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

poj3744高速功率矩阵+可能性DP的更多相关文章

  1. [AC自己主动机+可能性dp] hdu 3689 Infinite monkey theorem

    意甲冠军: 给n快报,和m频率. 然后进入n字母出现的概率 然后给目标字符串str 然后问m概率倍的目标字符串是敲数量. 思维: AC自己主动机+可能性dp简单的问题. 首先建立trie图,然后就是状 ...

  2. 矩阵优化dp

    链接:https://www.luogu.org/problemnew/show/P1939 题解: 矩阵优化dp模板题 搞清楚矩阵是怎么乘的构造一下矩阵就很简单了 代码: #include < ...

  3. bzoj 3120 矩阵优化DP

    我的第一道需要程序建矩阵的矩阵优化DP. 题目可以将不同的p分开处理. 对于p==0 || p==1 直接是0或1 对于p>1,就要DP了.这里以p==3为例: 设dp[i][s1][s2][r ...

  4. HDU - 2294: Pendant(矩阵优化DP&前缀和)

    On Saint Valentine's Day, Alex imagined to present a special pendant to his girl friend made by K ki ...

  5. [六省联考2017]组合数问题 (矩阵优化$dp$)

    题目链接 Solution 矩阵优化 \(dp\). 题中给出的式子的意思就是: 求 nk 个物品中选出 mod k 为 r 的个数的物品的方案数. 考虑朴素 \(dp\) ,定义状态 \(f[i][ ...

  6. Codevs 1305 Freda的道路(矩阵乘法 DP优化)

    1305 Freda的道路 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description Freda要到Rainbow的城堡去玩了.我们可以认 ...

  7. 洛谷P3502 [POI2010]CHO-Hamsters感想及题解(图论+字符串+矩阵加速$dp\&Floyd$)

    洛谷P3502 [POI2010]CHO-Hamsters感想及题解(图论+字符串+矩阵加速\(dp\&Floyd\)) 标签:题解 阅读体验:https://zybuluo.com/Junl ...

  8. [BZOJ 4818/LuoguP3702][SDOI2017] 序列计数 (矩阵加速DP)

    题面: 传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=4818 Solution 看到这道题,我们不妨先考虑一下20分怎么搞 想到暴力,本蒟 ...

  9. poj 3744 Scout YYF I (可能性DP+矩阵高速功率)

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5062   Accepted: 1370 Description YYF i ...

随机推荐

  1. gradle--java入门(转)

    7.3.3 项目之间的依赖性 您可以添加项目之间的依赖性在相同的构建,所以,例如,这个一个项目的JAR文件是用来编译另一个项目,在api构建文件我们将添加一个依赖JAR产生的共享项目.由于这种依赖性, ...

  2. extjs每一个组件要设置唯一的ID

    extjs每一个组件要设置唯一的ID,否则会造成各种错误 EXTJS基本上是靠ID来识别组件的,假如你在panel1中有个ID:"keyword"的textfield,而panel ...

  3. ANDROID PAD版本号 PHONE版本号 源代码有什么 差别?

    ANDROID PAD版本号 PHONE版本号 源代码有什么 差别? 直接把frameworks/base/core/res/res/values/config.xml里面的<bool name ...

  4. 为什么 Linux Mint 比 Ubuntu好?

    Linux Mint由Linux Mint Team团队于2006年开始发行,是一份基于Debian和Ubuntu的Linux发行版.其目标是提供一种更完整的即刻可用体验,这包括提供浏览器插件.多媒体 ...

  5. 2g-3g

  6. JTextField限制输入长度的完美解决方案(转)

    关于JTextField限制输入字符长度的问题,因为没提供现成的api,所以我们得自己动手,来实现这个功能,网上也有很多这样的资料,大多是在JTextField的Document的insertStri ...

  7. Linux系统下启动MySQL报错:Neither host &#39;localhost.localdomain&#39; nor &#39;localhost&#39; could be looked up with

    Linux系统下启动MySQL报错:Neither host 'localhost.localdomain' nor 'localhost' could be looked up with 摘要 Li ...

  8. Android IPC通信和AIDL技术应用

    首先我们了解一下 IPC和AIDL IPC:进程间通信 AIDL:Android Interface Definition Language,即Android接口定义语言. 为什么使用: Androi ...

  9. Javascript学习7 - 脚本化浏览器窗口

    原文:Javascript学习7 - 脚本化浏览器窗口 本节讨论了文档对象模型.客户端Javascript下Window中的各项属性,包括计时器.Location对象.Histroy对象.窗口.浏览器 ...

  10. 采用oracle官方文件(11G)——初步Concept

    采用oracle官方文件(11G)示例 这里是oracle官方文档界面,想了解oracle,阅读官方文档是唯一的方法,大致了解官方文档的使用,对官方文档有一个更直观的认识.文档可通过文章关联的链接查看 ...