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 (矩阵)的更多相关文章

  1. poj 3744 Scout YYF I (矩阵快速幂 优化 概率dp)

    题目链接 分析&&题意来自 : http://www.cnblogs.com/kuangbin/archive/2012/10/02/2710586.html 题意: 在一条不满地雷的 ...

  2. poj 3744 Scout YYF I(递推求期望)

    poj 3744 Scout YYF I(递推求期望) 题链 题意:给出n个坑,一个人可能以p的概率一步一步地走,或者以1-p的概率跳过前面一步,问这个人安全通过的概率 解法: 递推式: 对于每个坑, ...

  3. POJ 3744 Scout YYF I 概率dp+矩阵快速幂

    题目链接: http://poj.org/problem?id=3744 Scout YYF I Time Limit: 1000MSMemory Limit: 65536K 问题描述 YYF is ...

  4. poj 3744 Scout YYF I(概率dp,矩阵优化)

    Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5020   Accepted: 1355 Descr ...

  5. poj 3744 Scout YYF 1 (概率DP+矩阵快速幂)

    F - Scout YYF I Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub ...

  6. POJ 3744 Scout YYF I

    分段的概率DP+矩阵快速幂                        Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Sub ...

  7. poj 3744 Scout YYF I (可能性DP+矩阵高速功率)

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5062   Accepted: 1370 Description YYF i ...

  8. POJ 3744 Scout YYF I(矩阵快速幂优化+概率dp)

    http://poj.org/problem?id=3744 题意: 现在有个屌丝要穿越一个雷区,雷分布在一条直线上,但是分布的范围很大,现在这个屌丝从1出发,p的概率往前走1步,1-p的概率往前走2 ...

  9. POJ 3744 Scout YYF I (概率dp+矩阵快速幂)

    题意: 一条路上,给出n地雷的位置,人起始位置在1,向前走一步的概率p,走两步的概率1-p,踩到地雷就死了,求安全通过这条路的概率. 分析: 如果不考虑地雷的情况,dp[i],表示到达i位置的概率,d ...

随机推荐

  1. 让 Dreamweaver 支持 Emmet(原ZenCoding)

    注:目前暂不支持 DW CC,期待作者早是更新.Update:2013/10/12 鉴于某些原因,每个 Coder 所钟爱的 IDE 各不相同.而作为一个软件爱好者,我几乎所有 IDE 都使用过一段时 ...

  2. CentOS6.3(64位)下安装Oracle11gR2(64)服务器

    安装环境 Linux服务器:Centos6.3 64位 Oracle服务器:Oracle11gR2 64位 系统要求 1.Linux安装Oracle系统要求 系统要求 说明 内存 必须高于1G的物理内 ...

  3. leetcodequestion_56 Merge Intervals

    Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8, ...

  4. Bitmap的一些操作

    1.截取 Bitmap 的部分区域 mBitmap = Bitmap.createBitmap(bmp, 100, 100, 120, 120); 这句代码从 bmp 的 (100,100) 处截取 ...

  5. Java基础知识强化41:StringBuffer类之StringBuffer的反转功能

    1. StringBuffer 的反转功能: public StringBuffer reverse(): 2. 案例演示: package cn.itcast_05; /* * StringBuff ...

  6. Form( 表单) 组件

    本节课重点了解 EasyUI 中 Form(表单)组件的使用方法, 这个组件不依赖于任何组件.一. 加载方式表单组件只能在 JS 区域设置,首先定义一张表单.<form id="box ...

  7. .NET中常见的内存泄露问题——GC、委托事件和弱引用

    一.什么是内存泄露(memory leak)? 内存泄露不是指内存坏了,也不是指内存没插稳漏出来了,简单来说,内存泄露就是在你期待的时间内你程序所占用的内存没有按照你想象中的那样被释放. 因此什么是你 ...

  8. MVC路由规则以及前后台获取Action、Controller、ID名方法

    1.前后台获取Action.Controller.ID名方法 前台页面:ViewContext.RouteData.Values["Action"].ToString(); Vie ...

  9. c++中static的使用

    static可以用来修饰变量,包括函数的局部变量,类的成员变量.可以用来修饰函数,包括类的成员函数,普通函数. 今天就只说说static修饰类之外的函数的情况.假设你写了一个head.h,一个a.cp ...

  10. wordpress高级教程

    1.获取博客信息 <?php bloginfo(''); ?> // 显示博客的信息 /* 部分常用参数: default:默认 name:名称 description:说明 url.ho ...