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. [ExtJS5学习笔记]第22 Extjs5正在使用beforeLabelTpl添加所需的配置选项标注星号标记

    本文地址:http://blog.csdn.net/sushengmiyan/article/details/39395753 官方样例:http://docs.sencha.com/extjs/5. ...

  2. android开源应用(主要是博客上带有分析的)收集 【持续更新】

    2014.5.24更新: (android高仿系列)今日头条    http://blog.csdn.net/vipzjyno1/article/details/26514543 CSDN Andro ...

  3. OpenGL缓冲区

    OpenGL缓冲区 颜色缓冲区 OpenGL时,先是在一个缓冲区中完毕渲染,然后再把渲染结果交换到屏幕上. 我们把这两个缓冲区称为前颜色缓冲区(屏幕)和后颜色缓冲区.在默认情况下,OpenGL命令是在 ...

  4. Cytoscape画图初探

    Cytoscape是一个做网络图的js插件.用起来非常方便,并且非常强大.这是它的站点:点击打开链接 使用它须要导入两个文件,一个是js文件,一个是css文件.官网上下载. 这里实现了一个功能.即从后 ...

  5. springMVC+angular+bootstrap+mysql的简易购物网站搭建

    springMVC+angular+bootstrap+mysql的简易购物网站搭建 介绍 前端的css框架用了bootstrap, 以及bootstrap的JS组件, 以及很好用的angular(a ...

  6. 持续集成并不能消除 Bug,而是让它们非常容易发现和改正(转)

    互联网软件的开发和发布,已经形成了一套标准流程,最重要的组成部分就是持续集成(Continuous integration,简称 CI). 本文简要介绍持续集成的概念和做法. 一.概念 持续集成指的是 ...

  7. Win7安装和配置Tigase 5.2server

    Win7安装和配置Tigaseserver 笔者:chszs,转载注明. 博客首页:http://blog.csdn.net/chszs 1.下载tigase-server-5.2.0-b3447.e ...

  8. HDU 4831 Scenic Popularity (段树)

    Scenic Popularity Problem Description 临近节日,度度熊们近期计划到室外游玩公园.公园内部包含了非常多的旅游景点区和歇息区,因为旅游景点非常热门,导致景点区和歇息区 ...

  9. 【BZOJ3450】【Tyvj1952】Easy 可能DP

    联系: #include <stdio.h> int main() { puts("转载请注明出处[辗转山河弋流歌 by 空灰冰魂]谢谢"); puts("网 ...

  10. linux复制文件命令scp

    linux大多数复制我们的递送工具使用,有着ftp,scp等一下. 当中scp命令很easy快捷, 本机到远程:scp (-r) 本地目录或者文件路径 远程ip:目录 远程到本机:scp (-r) 远 ...