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. 十三、C# 事件

    1.多播委托 2.事件 3.自定义事件   在上一章中,所有委托都只支持单一回调. 然而,一个委托变量可以引用一系列委托,在这一系列委托中,每个委托都顺序指向一个后续的委托, 从而形成了一个委托链,或 ...

  2. Elasticsearch学习2--Elasticsearch数据类型简介

    1.Elasticsearch 是 面向文档型数据库,这意味着它存储的是整个对象或者 文档,它不但会存储它们,还会为他们建立索引,这样你就可以搜索他们了.你可以在 Elasticsearch 中索引. ...

  3. 不带头结点的单链表递归删除元素为X的结点

    #include <iostream> using namespace std; struct Node { Node *next; int elem; }; void creatList ...

  4. 完美让IE兼容input placeholder属性的jquery实现

    调用时直接引用jquery与下面的js就行了,相对网上的大多数例子来说,这个是比较完美的方案. /* * 球到西山沟 * http://www.cnzj5u.com * 2014/11/26 12:1 ...

  5. SGU 158.Commuter Train

    一道简单题. 火车停的位置不是在整点就是在二分之一点,坐标*2,然后枚举火车停的位置,计算总距离即可. code: #include <iostream> #include <cma ...

  6. 24种设计模式--单例模式【Singleton Pattern】

    这个模式是很有意思,而且比较简单,但是我还是要说因为它使用的是如此广泛,如此的有人缘,单例就是单一.独苗的意思,那什么是独一份呢?你的思维是 独一份,除此之外还有什么不能山寨的呢?我们举个比较难复制的 ...

  7. PHP常用代码:

    1.$array=explode(separator,$string); //字符串->数组 2.$string=implode(glue,$array);//数组->字符串 3.file ...

  8. android:persistent属性

    application PhoneApp既没有被Broadcast唤醒,也没有被其他service调用,那么是android是通过什么方式来启动PhoneApp,所以就发现了属性android:per ...

  9. CentOS 6.8安装Python2.7.13

    查看当前系统中的 Python 版本 python --version 返回 Python 2.6.6 为正常. 检查 CentOS 版本 cat /etc/redhat-release 返回 Cen ...

  10. sql 使用 FOR XML PATH实现字符串拼接

    sql中经常需要把多个行数据合成一行下面是利用 FOR XML PATH来实现的简单介绍. 1,把图一的转换为图二: SELECT articleID, (),tagID)+',' FROM arti ...