Scout YYF I
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 一位童子兵要穿过一条路,路上有些地方放着地雷。这位童子兵非常好玩,走路一蹦一跳的。
每次他在 i 位置有 p 的概率走一步到 i+1 ,或者 (1-p) 的概率跳一步到 i+2。童子兵初始在1位置,求他安全通过这条道路的概率。
以所在位置为状态,dp[i] 表示在位置 i 的安全的概率。
dp[i] = p * dp[i-1] + (1 - p) * dp[i-2]; // i 位置没有地雷
但是题目数据的范围是 10^8 这样dp的话会 TLE。
想想可以用矩阵快速幂优化。简单推出矩阵是
|p 1-p| * |dp[i]   | = |dp[i+1]|
|1 0    |   |dp[i-1]|    |dp[i]     |
而这时地雷位置是不满足这个矩阵的,因此我们得对地雷位置进行特判。而两个地雷中间的位置可以用快速幂优化。
假设 k 位置放有地雷,,我们可以得到 dp[k+1] = dp[k-1] * (1 - p);
对于炸弹位置为 a[i] 和 a[i+1] 之间的数,知道 dp[a[i]+1] 后可以推出
(视0位置有颗地雷,有地雷的位置的dp值为0)
于是我们可以对两个前后两个地雷之间用快速幂优化,并最终得到答案dp[max(a[i])+1];
转自:http://blog.csdn.net/xuelanghu407/article/details/47172759
 #include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std; int n;double p;
int x[]; struct Node{double mat[][];};
Node mul(Node a,Node b)
{
Node res;
for(int i=;i<;i++)
for(int j=;j<;j++)
{
res.mat[i][j]=;
for(int k=;k<;k++) res.mat[i][j]+=a.mat[i][k]*b.mat[k][j];
}
return res;
}
Node pow_M(Node a,int n)
{
Node res;
memset(res.mat,,sizeof(res.mat));
for(int i=;i<;i++)
res.mat[i][i]=;
Node temp=a;
while(n)
{
if(n&)res=mul(res,temp);
temp=mul(temp,temp);
n>>=;
}
return res;
}
int main()
{
while(~scanf("%d%lf",&n,&p))
{
for(int i=;i<n;i++)
scanf("%d",&x[i]);
sort(x,x+n);
double ans=;
Node tt;
tt.mat[][]=p;
tt.mat[][]=-p;
tt.mat[][]=;
tt.mat[][]=;
Node temp; temp=pow_M(tt,x[]-);
ans*=(-temp.mat[][]); for(int i=;i<n;i++)
{
if(x[i]==x[i-])continue;
temp=pow_M(tt,x[i]-x[i-]-);
ans*=(-temp.mat[][]);
}
printf("%.7f\n",ans);
}
}
 

POJ3744 Scout YYF I (矩阵优化的概率DP)的更多相关文章

  1. Scout YYF I POJ - 3744(概率dp)

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

  2. poj3744 Scout YYF I[概率dp+矩阵优化]

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

  3. [Poj3744]Scout YYF I (概率dp + 矩阵乘法)

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

  4. POJ3744 Scout YYF I 概率DP+矩阵快速幂

    http://poj.org/problem?id=3744 题意:一条路,起点为1,有概率p走一步,概率1-p跳过一格(不走中间格的走两步),有n个点不能走,问到达终点(即最后一个坏点后)不踩坏点的 ...

  5. POJ-3744 Scout YYF I 概率DP

    题目链接:http://poj.org/problem?id=3744 简单的概率DP,分段处理,遇到mine特殊处理.f[i]=f[i-1]*p+f[i-2]*(1-p),i!=w+1,w为mine ...

  6. POJ-3744 Scout YYF I (矩阵优化概率DP)

    题目大意:有n颗地雷分布在一条直线上,有个人的起始位置在1,他每次前进1步的概率为p,前进两步的概率为1-p,问他不碰到地雷的概率. 题目分析:定义状态dp(i)表示到了dp(i)的概率,则状态转移方 ...

  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. poj 3744 Scout YYF I (矩阵快速幂 优化 概率dp)

    题目链接 分析&&题意来自 : http://www.cnblogs.com/kuangbin/archive/2012/10/02/2710586.html 题意: 在一条不满地雷的 ...

  9. [poj3744] Scout YYF I【概率dp 数学期望】

    传送门:http://poj.org/problem?id=3744 令f(i)表示到i,安全的概率.则f(i) = f(i - 1) * p + f(i - 2) * (1 - p),若i位置有地雷 ...

随机推荐

  1. C语言中的二级指针(双指针)

    原创作品,转载请标明出处http://blog.csdn.net/yming0221/article/details/7220688 C语言更多查看 C语言使用注意事项(一) C语言使用注意事项(二) ...

  2. Android(java)学习笔记148:网易新闻RSS客户端应用编写逻辑过程

    1.我们的项目需求是编写一个新闻RSS浏览器,RSS(Really Simple Syndication)是一种描述和同步网站内容的格式,是使用最广泛的XML应用.RSS目前广泛用于网上新闻频道,bl ...

  3. 1.JOIN和UNION区别

    1.JOIN和UNION区别join 是两张表做交连后里面条件相同的部分记录产生一个记录集,union是产生的两个记录集(字段要一样的)并在一起,成为一个新的记录集 . JOIN用于按照ON条件联接两 ...

  4. Python学习记录1

    交互式解释器 模块 python序列 索引提取 序列运算 空列表 成员资格 长度最大值最小值函数 列表 list和join函数 交互式解释器 ' >>> '为提示符. 语句是用来告诉 ...

  5. [LUOGU] 1364 医院设置

    设有一棵二叉树,如图: [我是图] 其中,圈中的数字表示结点中居民的人口.圈边上数字表示结点编号,现在要求在某个结点上建立一个医院,使所有居民所走的路程之和为最小,同时约定,相邻接点之间的距离为1.如 ...

  6. PHP 把字符转换为 HTML 实体 - htmlentities() 函数

    定义和用法 htmlentities() 函数把字符转换为 HTML 实体. 语法 htmlentities(string,quotestyle,character-set) 参数 描述 string ...

  7. Centos6.9 搭建rsync服务端与客户端 案例:全网备份项目

    rsync的企业工作场景说明 1)定时备份 1.1生产场景集群架构服务器备份方案项目 借助cron+rsync把所有客户服务器数据同步到备份服务器 2)实时复制 本地数据传输模式(local-only ...

  8. RN原生方法setNativeProps

    https://facebook.github.io/react-native/docs/direct-manipulation.html setNativeProps可以直接修改底层native组件 ...

  9. 2018年,最经典的26个JavaScript面试题和答案!

    根据 Stack Overflow 的 2018 年度调查,JavaScript 连续六年成为最常用的编程语言.所以我们必须面对这样的现实,JavaScript 已经成为全栈开发技能的基石,在全栈开发 ...

  10. $(addprefix PREFIX,NAMES…)

    addprefix 是makefile中的函数,是添加前缀的函数例如:$(addprefix src/,foo bar) 返回值为“src/foo src/bar”.所以上面的意思是为dirver_d ...