poj 3744 Scout YYF I (矩阵)
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 -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 ( ≤ N ≤ ) 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 [, ].
Output
For each test case, output the probabilty in a single line with the precision to digits after the decimal point.
Sample Input
0.5 0.5
Sample Output
0.5000000
0.2500000
Source
又是一题矩阵乘法……
这题很显然了, n个雷,分别在 a[1]...a[n] ,走一步概率为 p ,走两步概率为 1-p ,一开始在 1 号位置,问安全到达终点的概率。
显然,如果k 号位有雷,那么安全通过这个雷只可能是在 k-1 号位选择走两步到 k+1 号位。因此,可以得到如下结论:在第 i 个雷的被处理掉的概率就是从 a[i-1]+1 号位到 a[i] 号位的概率。于是,可以用 1 减去就可以求出安全通过第 i 个雷的概率,最后乘起来即可,比较悲剧的是数据很大,所以需要用到矩阵快速幂……
类似斐波那契数列,有ans[i]=p*ans[i-1]+(1-p)*ans[i-2] ,构造矩阵为
|p 1-p | ans[i-1] ans[i]
|1 0 | ans[i-2] ans[i-1]
//ans[i]=p*ans[i-1]+(1-p)*ans[i-2]
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<stdlib.h>
using namespace std;
#define N 16
int n;
double p;
int a[N]; struct Matrix
{
double m[][];
Matrix()
{
memset(m,,sizeof(m));
for(int i=;i<;i++)
m[i][i]=;
}
}; Matrix Mul(Matrix a,Matrix b)
{
Matrix res;
int i,j,k;
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
res.m[i][j]=;
for(int k=;k<;k++)
{
res.m[i][j]=res.m[i][j]+(a.m[i][k]*b.m[k][j]);
}
}
}
return res;
}
Matrix fastm(Matrix a,int b)
{
Matrix res;
while(b)
{
if(b&)
res=Mul(res,a);
a=Mul(a,a);
b>>=;
}
return res;
} int main()
{
while(scanf("%d%lf",&n,&p)!=EOF)
{
for(int i=;i<=n;i++) scanf("%d",&a[i]); sort(a+,a+n+);
double ans=;
Matrix tmp;
tmp.m[][]=p;
tmp.m[][]=-p;
tmp.m[][]=;
tmp.m[][]=; Matrix cnt;
cnt=fastm(tmp,a[]-);
ans*=(-cnt.m[][]);
for(int i=;i<=n;i++)
{
if(a[i]==a[i-]) continue;
cnt=fastm(tmp,a[i]-a[i-]-);
ans*=(-cnt.m[][]);
} printf("%.7lf\n",ans); }
return ;
}
poj 3744 Scout YYF I (矩阵)的更多相关文章
- poj 3744 Scout YYF I (矩阵快速幂 优化 概率dp)
题目链接 分析&&题意来自 : http://www.cnblogs.com/kuangbin/archive/2012/10/02/2710586.html 题意: 在一条不满地雷的 ...
- poj 3744 Scout YYF I(递推求期望)
poj 3744 Scout YYF I(递推求期望) 题链 题意:给出n个坑,一个人可能以p的概率一步一步地走,或者以1-p的概率跳过前面一步,问这个人安全通过的概率 解法: 递推式: 对于每个坑, ...
- 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 I(概率dp,矩阵优化)
Scout YYF I Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5020 Accepted: 1355 Descr ...
- 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 Sub ...
- poj 3744 Scout YYF I (可能性DP+矩阵高速功率)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5062 Accepted: 1370 Description YYF i ...
- POJ 3744 Scout YYF I(矩阵快速幂优化+概率dp)
http://poj.org/problem?id=3744 题意: 现在有个屌丝要穿越一个雷区,雷分布在一条直线上,但是分布的范围很大,现在这个屌丝从1出发,p的概率往前走1步,1-p的概率往前走2 ...
- POJ 3744 Scout YYF I (概率dp+矩阵快速幂)
题意: 一条路上,给出n地雷的位置,人起始位置在1,向前走一步的概率p,走两步的概率1-p,踩到地雷就死了,求安全通过这条路的概率. 分析: 如果不考虑地雷的情况,dp[i],表示到达i位置的概率,d ...
随机推荐
- ASIHTTPRequest使用指南---<<翻译稿>>
ASIHTTPRequest使用指南---<<翻译稿>> 当第一次使用ASIHTTPRequest进行http请求时,会出现非常多的bug提示.查了一些资料,发现在少倒入了几个 ...
- oracle触发器实例
8.1 触发器类型 8.1.1 DML触发器 8.1.2 替代触发器 8.1.3 系统触发器 8.2 创建触发器 8.2.1 触发器触发次序 8.2.2 创建DML触发器 8.2.3 创建替代(INS ...
- android中定位光标位置
edittext.setSelection(int); edittext.setText(123);//设置edittext中的内容 edittext.setSelection(123.length( ...
- qt: flush: BitBlt failed
"BitBlt" is a graphics accelerator function. The message is a warning, not an error. It te ...
- FoxOne---一个快速高效的BS框架--WEB控件属性编辑器
FoxOne---一个快速高效的BS框架--(1) FoxOne---一个快速高效的BS框架--(2) FoxOne---一个快速高效的BS框架--(3) FoxOne---一个快速高效的BS框架-- ...
- Ajax&XMLHttpRequest
XMLHttpRequest 简单省力的方法 将文件编码成base64通过Ajax上传 HTML5学习之FileReader接口 HTML5学习之FileReader接口 通过Ajax方式上传文件,使 ...
- python:字符串取值
某个字符串为stmp="abcdef54321" 取前面5个stmp[:5] #abcde 取后面5个stmp[-5:] #54321 从前面开始取,不包括最后两个stmp[:-2 ...
- IE标签a嵌套table标签,链接点击无效
在IE中,使用如下代码将无法触发跳转: <a href="http://xx.xx.com"> <table> <tr> <td>点 ...
- H3 BPM 笔记
先通过流程设计器设计流程 注意 审批:1个人 会签: 多人用 同意时: 若为有一个同意就通过 则 审批选项卡 的同意出口 设为1 如果需要所有人同意才通过 则 审批选项卡 的同意出口 设为100% ...
- Oracle 如何让别人能够连接到你的数据库
Oracle 初步 --Oracle的一些关键字 i和g只是版本的代号,指oracle运用的技术i代表Internet就是互联网技术g代表grid就是网格技术现在出的最新版是c就是cloud也就是云技 ...