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 ...
随机推荐
- angularJS怎么实现与服务端的PHP进行数据交互
//{params: 要传的参数obj },params这个是关键字不能换别的变量 $http.get(url, {params: {id: categoryid, key: keys} }).suc ...
- Oracle Directory文件夹的知识
在上一章介绍expdp/impdp时曾使用过DIRECTORY这个概念,以下再简单说明下DIRECTORY的点点滴滴. MOS上对DIRECTORY的解释(266875.1): (1).基于服务端 v ...
- [Docker] Install Docker on Windows (hp) and start with Kitematic
Well, on Windows costs a little bit effort to run docker. 1. You need to enable Virtulization: Oh hp ...
- 一些使用Android设备调试功能的注意事项(挖职位)
华为3C Activity切换动画过热. 当显示器是不是大图easy显现OOM(应用最大大于其他手机内容).因此,调试OOM不要当问题用这个手机,否则,很难发现问题. 小米3 不要调用系统的裁图功能. ...
- Qt - 与众不同的电子时钟
Qt的电子时钟是个老掉牙的demo了,但是利用lcdNumber显示的样子非常老土(下图第一个显示效果),一看就知道是从qt帮助文档里摘出来的example,毫无新意. 美化一下系统时钟,抛开固有控 ...
- LIB文件和DLL文件的作用
(1)lib是编译时需要的,dll是运行时需要的.如果要完成源代码的编译,有lib就够了.如果也使动态连接的程序运行起来,有dll就够了.在开发和调试阶段,当然最好都有.(2)一般的动态库程序有lib ...
- SOAP 简单对象访问协议
webService三要素 SOAP.WSDL(WebServicesDescriptionLanguage).UDDI(UniversalDescriptionDiscovery andIntegr ...
- virtual-虚方法
看来本人理论果然不行啊,这个东西折腾死我了.即便是到现在,还是云里雾里.... 个人认为virtual的特点就是可以被override而不是必需的,到目前为止我用它的地方也比较少. public cl ...
- css选择器基本属性
选择器一,相邻选择器: 1,相邻选择器 1),定义:相邻选择器匹配指定元素的相邻兄弟元素 2),用法:如果需要选择紧接在另一个元素后的元素,而且二者有相同的父元素,可以使用相邻兄弟选择器 3),表示符 ...
- HTML基础总结<头部>
重点摘录:HTML head 元素 标签 描述 <head> 定义了文档的信息 <title> 定义了文档的标题 <base> 定义了页面链接标签的默认链接地址 & ...