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. springboot中的406(Not Acceptable)错误

    今天遇到一个很奇怪的问题,我用springboot开发时,用ajax请求服务想返回json数据,页面总是报406错误,这个错误使我郁闷了很久,原来我的后台服务是这么写的 看到没value的后面有个.h ...

  2. jQuery -&gt; 获取兄弟元�

    获取指定元素的兄弟元素时,能够使用adjacent sibling combinator (+),当中+的两側内容都是selector expression. 假设要获取下例中全部的 h1的直接兄弟元 ...

  3. [React] React Router: Route Parameters

    A router library is no good if we have to hardcode every single route in our application. In this le ...

  4. LDAP缓存命令

    启动cacao及实例: [root@rusky bin]# cd /home/ldap/iamldap/dsee6/cacao_2/cacao/bin [root@rusky bin]# ./caca ...

  5. LDAP索引及缓存优化

    一.设置索引 索引将查找信息和 Directory Server 条目关联起来. Directory Server支持以下几种索引: 1出现索引 (pres) - 列出了具有特定属性的条目,与属性的值 ...

  6. DC综合流程

    Design Compiler and the Design Flow 步骤 将HDL描述的设计输入到Design Compiler中 Design Compiler使用technology libr ...

  7. 对html制作新手的一些建议,大牛可以忽略

    本篇主要讲前端并给制作html页面的新手一些建议,大牛勿喷大牛可以绕过. 感受:我是搞后端开发的,有时拿到一些静态(Html)页面,看到里面的页面结构命名规则极不规范,就有点不好的 感觉了.当然出现这 ...

  8. JS简单实现图片切换

    <!DOCTYPE html><html> <head> <meta charset="utf-8" /> <title> ...

  9. Ajax--WebService返回List

    WebService: using System.Web.Script.Services; [GenerateScriptType(typeof(people))] [WebMethod] publi ...

  10. IOS添加自定义字体库

    1.将需要的字体库xxx.ttf添加到工程中,注意一定要在copy bundle resources中存在,如果没有添加上去 2.在info.plist 文件中添加 fonts provided by ...