POJ3744 Scout YYF I (矩阵优化的概率DP)
Input
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
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)的更多相关文章
- 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 ...
- poj3744 Scout YYF I[概率dp+矩阵优化]
Scout YYF I Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8598 Accepted: 2521 Descr ...
- [Poj3744]Scout YYF I (概率dp + 矩阵乘法)
Scout YYF I Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9552 Accepted: 2793 Descr ...
- POJ3744 Scout YYF I 概率DP+矩阵快速幂
http://poj.org/problem?id=3744 题意:一条路,起点为1,有概率p走一步,概率1-p跳过一格(不走中间格的走两步),有n个点不能走,问到达终点(即最后一个坏点后)不踩坏点的 ...
- 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 ...
- POJ-3744 Scout YYF I (矩阵优化概率DP)
题目大意:有n颗地雷分布在一条直线上,有个人的起始位置在1,他每次前进1步的概率为p,前进两步的概率为1-p,问他不碰到地雷的概率. 题目分析:定义状态dp(i)表示到了dp(i)的概率,则状态转移方 ...
- 刷题总结—— Scout YYF I(poj3744 矩阵快速幂+概率dp)
题目: Description YYF is a couragous scout. Now he is on a dangerous mission which is to penetrate int ...
- poj 3744 Scout YYF I (矩阵快速幂 优化 概率dp)
题目链接 分析&&题意来自 : http://www.cnblogs.com/kuangbin/archive/2012/10/02/2710586.html 题意: 在一条不满地雷的 ...
- [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位置有地雷 ...
随机推荐
- 深度技术GHOST WIN7系统32,64位旗舰稳定版
系统来自系统妈:http://www.xitongma.com 系统概述 深度技术ghost win8 X86(32位)旗舰稳定版系统集成了SATA/RAID/SCSI驱动,支持P45. MCP78. ...
- 系统妈Win10系统64位和32位快速专业版
win10系统64位快速专业安装版 V2016年 系统妈:http://www.xitongma.com/ Ghost Win10 64位正式装机专业版2016 微软向Windows用户推送了win1 ...
- 51nod 1526 分配笔名
题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 320 难度:7级算法题. 班里有n个同学.老师为他们选了n个笔名.现在要把这些笔名分配给每一个同学,每一 ...
- Codeforces Round #318 (Div. 2) D Bear and Blocks (数学)
不难发现在一次操作以后,hi=min(hi-1,hi-1,hi+1),迭代这个式子得到k次操作以后hi=min(hi-j-(k-j),hi-k,hi+j-(k-j)),j = 1,2,3... 当k ...
- java B转换KB MB GB TB PB EB ZB
public static String readableFileSize(long size) { if (size <= 0) { return "0"; } final ...
- PAT (Basic Level) Practise (中文)-1030. 完美数列(25)
PAT (Basic Level) Practise (中文)-1030. 完美数列(25) http://www.patest.cn/contests/pat-b-practise/1030 给 ...
- JdbcTemplate类对sql的操作使用
<!--方式一: dbcp 数据源配置,在测试环境使用单连接 --> <bean id="dataSource" class="org.apache.c ...
- emoji等表情符号存mysql的方法
项目中需要存储用户信息(用户昵称有表情符号),自然就遇到了emoji等表情符号如何被mysql DB支持的问题 这里引用先行者博文:https://segmentfault.com/a/1190000 ...
- centOS下SVN安装和配置
1>SVN服务器端文件可见问题 在平时使用SVN时候,一直以为在客户提交文件,那么在服务器对应的版本库下面就会有相同文件.在自己搭建后,发现提交到服务器端文件完全看不见.... 这是由于SVN服 ...
- JS获取单选框checked的value方法
; var obj = document.getElementsByTagName("input"); document.getElementById('gender').oncl ...
