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. 删除我的电脑中360随身WiFi云U盘的图标

    可通过删除注册表的方法 运行-regedit 找到这个项 HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\My ...

  2. 嵌入式css样式,写在当前的文件中

    现在有一任务,把下面的“超酷的互联网”.“服务及时贴心”.“有趣易学”这三个短词文字字号修改为18px. 如果用内联式css样式的方法进行设置将是一件很头疼的事情(为每一个<span>标签 ...

  3. list-style-type -- 定义列表样式

    取值:disc | circle | square | decimal | decimal-leading-zero | lower-roman | upper-roman | lower-greek ...

  4. Linux下ln链接命令详解

    ln是linux中又一个非常重要命令,它的功能是为某一个文件在另外一个位置建立一个不同的链接,这个命令最常用的参数是-s,具体用法是:ln –s 源文件 目标文件. 当我们需要在不同的目录,用到相同的 ...

  5. 简单概述 .NET Framework 各版本区别

    目前已发行的版本有1.0.1.1.2.0.3.0.3.5.4.0.4.5(及4.5.1.4.5.2).4.6(及4.6.1). 1.0版本:最初的.net framework版本,作为一个独立的工具包 ...

  6. Fedora 18 安装前指南

    Secure Boot 与 Win 8   随着 Win8 的发布,先前关于 Secure Boot 和 UEFI 的诸多猜测也得到了证实,Fedora 18 也将如同当初计划的那样使用 shim + ...

  7. nginx 要改进的地方基础

  8. 【算法】简单选择排序 O(n^2) 不稳定的 C语言

    简单选择排序 一.算法描述 假设序列中有N个元素: 第1趟找到第1到N个元素之间最小的一个,与第1个元素进行交换 第2趟找到第2到N个元素之间最小的一个,与第2个元素进行交换 第3趟找到第3到N个元素 ...

  9. J - 计算两点间的距离

      Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Description 输入两 ...

  10. iso 开发学习--简易音乐播放器(基于iPhone4s屏幕尺寸)

    三个按钮  一个进度条 贴图(软件中部分图片,来自网络,如果侵犯了您的权益,请联系我,会立刻撤下) 核心代码 // // ViewController.m // 08-10-MusicPlayer / ...