Scout YYF I
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4100   Accepted: 1051

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 1- 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 (1 ≤ 
N ≤ 10) 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 [1, 100000000].

Output

For each test case, output the probabilty in a single line with the precision to 7 digits after the decimal point.

Sample Input

1 0.5
2
2 0.5
2 4

Sample Output

0.5000000
0.2500000

Source

第一次接触矩阵快速幂,矩阵要专门看,快速幂单独看看

显然,如果k 号位有雷,那么安全通过这个雷只可能是在 k-1 号位选择走两步到 k+1 号位。因此,可以得到如下结论:在第 i 个雷的安全通过的概率就是从 a[i-1]+1 号位到 a[i]+1 号位的概率。于是,可以用 1 减去就可以求出安全通过第 i 个雷的概率,最后乘起来即可,比较悲剧的是数据很大,所以需要用到矩阵快速幂……

类似斐波那契数列,ans[i]=p*ans[i-1]+(1-p)*ans[i-2] ,构造矩阵为

#include <iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
int s[20];
double q,p;
struct node
{
double dp[2][2];//矩阵
};
node mult(node a,node b)//矩阵乘法
{
int i,j,n,k;
node temp;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
temp.dp[i][j]=0;
for(k=0;k<2;k++)
temp.dp[i][j]+=a.dp[i][k]*b.dp[k][j];
}
}
return temp;
}
node cal(int N)//快速幂
{
node a,res;
a.dp[0][0]=p;
a.dp[0][1]=q;
a.dp[1][0]=1;
a.dp[1][1]=0;
res.dp[0][0]=1;
res.dp[0][1]=0;
res.dp[1][0]=0;
res.dp[1][1]=1;
while(N)
{
if(N&1)
{
res=mult(res,a);
}
a=mult(a,a);
N>>=1;
}
return res;
}
int main()
{
int i,j,n,m,max,flag;
double tempqn;
node temp,a;
while(scanf("%d%lf",&n,&p)!=EOF)
{
q=1-p;
s[0]=0;
for(i=1;i<=n;i++)
{
cin>>s[i];
}
sort(s+1,s+1+n);
for(i=1,flag=1;i<n;i++)
{
if(s[i]+1==s[i+1])
flag=0;
}
if(!flag||s[1]==1)
{
puts("0.0000000");
continue;
}
a.dp[0][0]=1;
a.dp[0][1]=0;
a.dp[1][0]=0;
a.dp[1][1]=0;
for(i=1;i<=n;i++)
{
temp=cal(s[i]-s[i-1]-2);
a=mult(temp,a);
a.dp[0][0]=a.dp[0][0]*q;
a.dp[0][1]=0;
a.dp[1][0]=0;
a.dp[1][1]=0;
}
printf("%.7f\n",a.dp[0][0]);
}
return 0;
}

poj4474 Scout YYF I(概率dp+矩阵快速幂)的更多相关文章

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

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

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

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

  3. POJ3744 Scout YYF I 概率DP+矩阵快速幂

    http://poj.org/problem?id=3744 题意:一条路,起点为1,有概率p走一步,概率1-p跳过一格(不走中间格的走两步),有n个点不能走,问到达终点(即最后一个坏点后)不踩坏点的 ...

  4. 刷题总结—— Scout YYF I(poj3744 矩阵快速幂+概率dp)

    题目: Description YYF is a couragous scout. Now he is on a dangerous mission which is to penetrate int ...

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

    题意: 一条路上有n个地雷,你从1开始走,单位时间内有p的概率走一步,1-p的概率走两步,问安全通过这条路的概率 解析: 很容易想到 dp[i] = p * dp[i-1] + (1 - p) * d ...

  6. poj3744 (概率DP+矩阵快速幂)

    http://poj.org/problem?id=3744 题意:在一条铺满地雷的路上,你现在的起点在1处.在N个点处布有地雷,1<=N<=10.地雷点的坐标范围:[1,10000000 ...

  7. poj3744 Scout YYF I[概率dp+矩阵优化]

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

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

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

  9. poj 3744 概率dp+矩阵快速幂

    题意:在一条布满地雷的路上,你现在的起点在1处.在N个点处布有地雷,1<=N<=10.地雷点的坐标范围:[1,100000000]. 每次前进p的概率前进一步,1-p的概率前进1-p步.问 ...

随机推荐

  1. PAT_2-08. 用扑克牌计算24点

    一副扑克牌的每张牌表示一个数(J.Q.K分别表示11.12.13,两个司令都表示6).任取4张牌,即得到4个1~13的数,请添加运算符 (规定为加+ 减- 乘* 除/ 四种)使之成为一个运算式.每个数 ...

  2. 八、C# 值类型

    结构.枚举.装箱.拆箱 自定义值类型 如何利用结构来定义新的值类型,并使之具有与大多数预定义 类型相似的行为,这里的关键在于,任何 新定义的值类型都有它们自己的数据和方法. 一般用枚举来定义常量值集合 ...

  3. ZOJ 刷题记录 小黑屋 (`・д・´)

    P1006:模拟 然而我的同余方程能过样例然而就是WA⊙﹏⊙b [已查明:扩展欧几里得算法出了很隐蔽的问题] int exGcd(int x,int y,int& a,int& b) ...

  4. poj 1087.A Plug for UNIX (最大流)

    网络流,关键在建图 建图思路在代码里 /* 最大流SAP 邻接表 思路:基本源于FF方法,给每个顶点设定层次标号,和允许弧. 优化: 1.当前弧优化(重要). 1.每找到以条增广路回退到断点(常数优化 ...

  5. PHPCMS V9 学习总结(转)

    转自:http://www.cnblogs.com/Braveliu/p/5074930.html 在实现PHPCMS网站过程中,根据业务需求,我们遇到很多问题,特此总结如下,以便大家参考学习. [1 ...

  6. 单选按钮 点击value值自动把单选按钮选中

    HTML 代码 <tr>       <td align="right">性别:</td>       <td><inputt ...

  7. 【转】HTML5 LocalStorage 本地存储

    原文见:http://www.cnblogs.com/xiaowei0705/archive/2011/04/19/2021372.html 说到本地存储,这玩意真是历尽千辛万苦才走到HTML5这一步 ...

  8. 简述一个javascript简单继承工具的实现原理

    背景 由于本人非常希望能够开发自己的游戏,所以业余时间一直在想着能不能自己一些好玩又有趣的东西出来,最近随着steam上众多独立游戏的爆发,感觉自己又燃烧了起来,所以又拾起了很久以前的一个2d引擎,决 ...

  9. 写个自己的Xcode4插件(二)

    补充上一篇: 一.在XCode5里面,要在info.plist里面再加入以下两个字段: 1. 内容要保持一致喔,别问我为什么,我也不知道,是参考其他许多插件发现的,那些插件都用了这个字段,而且内容一样 ...

  10. DEDECMS-helper小助手扩展

    今天在做DEDE动态调用模板的时候卡住了,后终被强大的互联网解决,记录解决问题的过程,以备后用 可以在/data/helper.inc.php中进行默认小助手初始化的设置,系统默认载入小助手 例如创建 ...