poj 3744 Scout YYF I (可能性DP+矩阵高速功率)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 5062 | Accepted: 1370 |
Description
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
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
题意:人人从1開始走,p的概率走1步,1-p的概率走2步,求不踩雷的概率。地雷数N<=10,坐标范围在100000000内。
思路:人走到第i位置的概率为dp[i]=dp[i-1]*p+dp[i-2]*(1-p),因为数据范围较大,可有矩阵高速幂高速求出答案;
构造矩阵 | P , 1 | 即 | dp[n+1] , dp[n] | =
| dp[n] , dp[n-1] | * | P , 1 |
| 1-P , 0 | |
1-P , 0 |
然后将每一个a[i]+1到a[i+1]看做一段单独处理即可。
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn=100000050; struct node
{
double mat[2][2];
}A,B,C; double dp[maxn],p;
int n,a[15]; void input()
{
for(int i=0;i<n;i++) scanf("%d",&a[i]);
sort(a,a+n);
A.mat[0][0]=p,A.mat[0][1]=1.0;
A.mat[1][0]=1.0-p,A.mat[1][1]=0.0;
} node mul(node p,node q) // 矩阵相乘
{
node ans;
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
{
ans.mat[i][j]=0.0;
for(int k=0;k<2;k++)
ans.mat[i][j]+=p.mat[i][k]*q.mat[k][j];
}
return ans;
} node pow(node w,int num) // 高速幂
{
node ret=B,c=w;
int coun=num;
while(coun)
{
if(coun & 1) ret=mul(ret,c);
coun>>=1;
c=mul(c,c);
}
return ret;
} void solve()
{
int now=1;
double f1=0.0,f2=1.0;
for(int i=0;i<n;i++)
{
if(a[i]-now>=1)
{
node tmp=pow(A,a[i]-1-now);
f2=(f2*tmp.mat[0][0]+f1*tmp.mat[1][0])*(1.0-p);
f1=0.0;
now=a[i]+1;
}
else
{
printf("0.0000000\n");
return ;
}
}
printf("%.7f\n",f2);
} int main()
{
B.mat[0][0]=1.0,B.mat[0][1]=0.0;
B.mat[1][0]=0.0,B.mat[1][1]=1.0;
while(scanf("%d %lf",&n,&p)!=EOF)
{
input();
solve();
}
return 0;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
poj 3744 Scout YYF I (可能性DP+矩阵高速功率)的更多相关文章
- POJ 3744 Scout YYF I 概率dp+矩阵快速幂
题目链接: http://poj.org/problem?id=3744 Scout YYF I Time Limit: 1000MSMemory Limit: 65536K 问题描述 YYF is ...
- poj 3744 Scout YYF 1 (概率DP+矩阵快速幂)
F - Scout YYF I Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- poj 3744 Scout YYF I(概率dp,矩阵优化)
Scout YYF I Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5020 Accepted: 1355 Descr ...
- poj 3744 Scout YYF I(递推求期望)
poj 3744 Scout YYF I(递推求期望) 题链 题意:给出n个坑,一个人可能以p的概率一步一步地走,或者以1-p的概率跳过前面一步,问这个人安全通过的概率 解法: 递推式: 对于每个坑, ...
- POJ 3744 Scout YYF I
分段的概率DP+矩阵快速幂 Scout YYF I Time Limit: 1000MS Memory Limit: 65536K Total Sub ...
- poj3744 Scout YYF I[概率dp+矩阵优化]
Scout YYF I Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8598 Accepted: 2521 Descr ...
- POJ 3744 Scout YYF I(矩阵快速幂优化+概率dp)
http://poj.org/problem?id=3744 题意: 现在有个屌丝要穿越一个雷区,雷分布在一条直线上,但是分布的范围很大,现在这个屌丝从1出发,p的概率往前走1步,1-p的概率往前走2 ...
- poj 3744 Scout YYF I (矩阵快速幂 优化 概率dp)
题目链接 分析&&题意来自 : http://www.cnblogs.com/kuangbin/archive/2012/10/02/2710586.html 题意: 在一条不满地雷的 ...
- POJ 3744 Scout YYF I (概率dp+矩阵快速幂)
题意: 一条路上,给出n地雷的位置,人起始位置在1,向前走一步的概率p,走两步的概率1-p,踩到地雷就死了,求安全通过这条路的概率. 分析: 如果不考虑地雷的情况,dp[i],表示到达i位置的概率,d ...
随机推荐
- IntelliJ IDEA15开发时设置中java complier 的问题
Error:java: Compilation failed: internal java compiler error set中java complier 设置的问题 ,项目中有人用jdk1.6 ...
- SQL Server 版本号汇总
通过SSMS连接Sql servr,查看实例的版本就能知道当前SQL Server的版本号了. RTM (no SP) SP1 SP2 SP3 SP4 SQL Server 2014 c ...
- win7已安装Mysql 开机自启动
1.下载并安装MySql,我用MySQL_5.6.24_winx64_XiaZaiBa,解压缩到磁盘或更低.我在这里安装D菜,D:\install\MySQL\MySQL Server 5.6. 2. ...
- Android开发被添加到桌面快捷方式
Android开发被添加到桌面快捷方式 对于一个希望拥有很多其它用户的应用来说.用户桌面能够说是全部软件的必争之地,假设用户在手机桌面上建立了该软件的快捷方式.用户将会更频繁地使用该软件. 因此,全部 ...
- HDN2048(交错复发)
上帝.神与神 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- 【我们都爱Paul Hegarty】斯坦福大学IOS8公开组个人笔记28 ScrollView 幻灯片视图
随着移动设备,iphone屏幕尺寸的限制.超过内容的屏幕大小为scrollview于,通过滑动来获得.scrollview滑动方向可以是也可以是横向垂直,scrollview可以嵌套,例如,纵向滑动s ...
- HDU 2227 Find the nondecreasing subsequences(DP)
Problem Description How many nondecreasing subsequences can you find in the sequence S = {s1, s2, s3 ...
- 吐槽一下Activiti用户手册和一本书
业余没事的时候,读点Java轮廓,无意中发现Activiti.我们打算跑了几个例子来看看它是如何. 我们一直从事低层次.我们在上面的照顾偶尔有精确地的程度不是什么. 这个过程是如此悲惨开始.第一Act ...
- Serializable 作用
Serializable 作用 序列化的attribute,是为了利用序列化的技术 准备用于序列化的对象必须设置 [System.Serializable] 标签,该标签指示一个类能够序列化. 便于在 ...
- centos7 高速安装 mariadb(mysql)
从最新版本的linux该系统启动,缺省值是 Mariadb代替mysql! 使用系统自带repos安装非常easy: yum install mariadb mariadb-server system ...