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 ...
随机推荐
- RH033读书笔记(4)-Lab 5 File Permissions
Lab 5 File Permissions Sequence 1: Determining File Permissions 1. What is the symbolic representati ...
- [MSSQL]最小公约数
[摘要]一个朋友在展BOM的时候有这种需求,两列字段(数值):A ,B A=用量,B=底数,组成用量=用量/底数.A/B,若能被整除,显示整除的结果,若不能整除显示分数形式A/B(分数形式要是约分 ...
- 使用Simple DNS plus 构建自己的DNS
1.下载并安装Simple DNS plus 2.界面例如以下: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2tfYm9zcw==/font/5a6L ...
- 低压电力采集平台DW710C与PC沟通
集电极485接口RS-485与RS-232转换模块485端相连.RS-485与RS-232转换模块232通过串行电缆末端PC的232串口.我们通过书面沟通PC通信软件来实现双方并执行收购方案. 1)上 ...
- 电脑知识--Windows一片
.com档 Dos可执行命令文件,一般小于64kb, .com文件包括程序的一个绝对映像.就是说,为了执行程序准确的处理器指令和内存中的数据.Ms-Dos通过直接把该映像从文件复制到内存. 而 载入. ...
- JAVA中的I/O流以及文件操作
一 JAVA语言中主要通过流来完成IO操作. 流:计算机的输入输出之间流动的数据序列,也是类的对象.java中的流方式就像是建立在数据交换源和目的之间的一条通信路径. 数据源:计算机中的数据源是指可以 ...
- 微通道产品经理Grover采访:美国的微通道设计
"'哥'在中国是一种尊称吗?哈哈.我们平时都叫张小龙'龙哥'." "是的.Dan哥,当你认为某个人牛逼的时候,你就能够叫他'哥'." 我对于Dan Grover ...
- nisi 脚本示例
只是简单的copy文件和添加快捷方式,安装和卸载时对程序是否运行进行检测 ;-------------------------------- ;Include Modern UI !include & ...
- 小丁带你走进git的世界三-撤销修改(转)
一.撤销指令 git checkout还原工作区的功能 git reset 还原暂存区的功能 git clean 还没有被添加进暂存区的文件也就是git还没有跟踪的文件可以使用这个命令清除他们 g ...
- 使用eclips发展java当闪回的问题
近期開始android的开发学习.当然要先从java入手了.我选择eclips作为开发的IDE,在測试java代码例子时,假设我的代码是能够出现系统自己主动代码补齐时eclips就会立马闪退. 刚開始 ...