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 ...
随机推荐
- 性能计数器自动收集-logman
1.在桌面云测试中,往往需要模拟并发连接中服务器的性能数据,这里主要介绍如何自动收集性能数据 2.创建xxxx.bat文件,文件内容如下: logman create counter test -cf ...
- 【HPP开发】让所有中小企业拥有自己的APP
HPP hybirdApp或者hbuilderApp, 指通过html,css,js语言开发出ios和android两个版本的APP, 开发效率成倍上升,开发时间大幅缩减,开发成本同样也大大缩减. 移 ...
- java关键字-transient
java语言的关键字,变量修饰符,如果用transient声明一个实例变量,当对象存储时,它的值不需要维持. Java的serialization提供了一种持久化对象实例的机制.当持久化对象时,可能有 ...
- [ES6] When should use Map instead of Object
Use Maps when keys are unknown until runtime: Map: let recentPosts = new Map(); createPost( newPost, ...
- Android开发编码规范(自用)
转载请注明本文出自Cym的博客(http://blog.csdn.net/cym492224103),谢谢支持! Android开发编码规范 目的及指导原则 目的 统一规范 Eclipse编辑环境 ...
- 栈溢出之rop到syscall
当程序开启了nx,但程序有syscall调用的时候.这时栈溢出的利用就可以通过rop来执行syscall的59号调用execve('/bin/sh',null,null),这是这次alictf一道pw ...
- tomcat端口号、日志、启停
cd到tomcat目录下 1.[root@rusky bin]# ./shutdown.sh 关闭tomcat 2.[root@rusky bin]# ./startup.sh ...
- Python购物车的实现课程
需求: 1.用户输入工资收入 2.打印商品列表 3.用户选择商品,不断的加入购物车 4.检测用户余额,直接捐款,不足提示余额不足 5.允许主动退出,退出时,打印已购商品列表 重点方法: 打印列表下标的 ...
- WPF控件---Border应用
内容模型:Border 只能具有一个子元素.若要显示多个子元素, 需要将一个容器元素放置在父元素Border中. <Grid> <Border BorderBrush="B ...
- (七)《Java编程思想》——多态的缺陷
1.不能“覆盖”私有方法 package chapter8; /** * 不能"覆盖"私有方法 */ public class PrivateOverride { private ...