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 2.0用户手册——总览(译)(转)

    2.1 特性 本章将介绍一系列Gradle的特性. 申明式构建和基于约定的构建 Gradle的核心是基于Groovy呈现了一种丰富的针对特定领域的语言,称之为Domain Specific Langu ...

  2. 每天收获一点点------Hadoop概述

    一.Hadoop来历 Hadoop的思想来源于Google在做搜索引擎的时候出现一个很大的问题就是这么多网页我如何才能以最快的速度来搜索到,由于这个问题Google发明了倒排索引算法,通过加入了Map ...

  3. FZU 2082 过路费(树链剖分)

    FZU 2082 过路费 题目链接 树链抛分改动边的模板题 代码: #include <cstdio> #include <cstring> #include <vect ...

  4. Python3.2官方文件翻译-工具列表和十进制浮点计算

    8.7 列表工具 许多数据结构需要通过内置列表类型来满足.但,有时候在不同的性能取舍需要选择一个实现. Array模块能提供一个像列表的array对象,它只能存储同类数据而且更加简洁. 接下来样例展示 ...

  5. SQLSERVER复制优化之一《减少包大小》

    原文:SQLSERVER复制优化之一<减少包大小> SQLSERVER复制优化之一<减少包大小> 自从搭了复制之后以为可以安枕无忧了,谁不知问题接踵而来 这次遇到的问题是丢包, ...

  6. vim note(3)

    Ctrl+w  Ctrl+v  will create a new window on the right side of the current window Ctrl+w  Ctrl+s   wi ...

  7. 一位OWin服务器新成员TinyFox

    一位OWin服务器新成员TinyFox TinyFox 是一款支持OWIN标准的WEB应用的高性能的HTTP服务器,是Jexus Web Server的"姊妹篇".TinyFox本 ...

  8. Sql使用视图(简单的视图)适合入门-level

    创建以便从库更好的数据呼叫和一个数据库,以(例如,该209东东库转移205图书馆) 例:209图书馆有不同的库名:数据库(AIS20140417092531电影库)供应商表(t_Supplier ). ...

  9. CSS3+HTML5特效3 - 纵向无缝滚动

    老惯例,先看例子. This is a test 1. This is a test 2. This is a test 3. This is a test 4. This is a test 5. ...

  10. 在遍历中使用 iterator/reverse_iterator 进行 Erase 的使用方法

    在遍历中使用 iterator/reverse_iterator 进行 Erase 的使用方法 罗朝辉 (http://blog.csdn.net/kesalin/) 本文遵循"署名-非商业 ...