(Another) 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, (Another) 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, (Another) YYF is at step one. For each step after that, (Another) 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 (Another) 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

  题目大意 另一个叫做yyf的人在一个数轴上,他在位置1,每次他有p的概率向右跳1格,有(1 - p)的概率向右跳2格,如果踩到地雷就死了,问生还的概率。

  显然动态规划。当位置i没有地雷的时候,显然有

  如果位置i有地雷,那么有

  由于值域很大,所以按照递推式动态规划的通用套路:构建转移矩阵加快动态规划。

  由于n很小,所以可以分段,段内用矩阵快速幂处理,然后特殊处理一下f[i],继续往前。

  现在来构建转移矩阵:

Code

 /**
* poj
* Problem#3744
* Accepted
* Time: 0ms
* Memory: 232k
*/
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cctype>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#include <bitset>
#include <cassert>
#ifndef WIN32
#define Auto "%lld"
#else
#define Auto "%I64d"
#endif
using namespace std;
typedef bool boolean;
#define clra(a) memset(a, false, sizeof(a))
const signed int inf = ((~0u) >> );
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b) template <typename T>
class Matrix {
public:
T a[][];
int row;
int col; Matrix() { }
Matrix(int row, int col):row(row), col(col) { } Matrix<T> operator * (Matrix<T> b) {
Matrix<T> rt(row, b.col);
assert(col == b.row);
for(int i = ; i < row; i++)
for(int j = ; j < b.col; j++) {
rt[i][j] = ;
for(int k = ; k < col; k++)
rt[i][j] += a[i][k] * b[k][j];
}
return rt;
} T* operator [] (int pos) {
return &a[pos][];
}
}; int n;
double p;
Matrix<double> unit(, );
Matrix<double> trans(, );
int boom[]; template <typename T>
Matrix<T> matpow(Matrix<T>& a, int pos) {
if(pos == ) return unit;
if(pos == ) return a;
Matrix<T> temp = matpow(a, pos >> );
temp = temp * temp;
if(pos & ) temp = temp * a;
return temp;
} inline void prepare() {
unit[][] = unit[][] = ;
unit[][] = unit[][] = ;
} inline void init() {
for(int i = ; i < n; i++)
scanf("%d", boom + i);
sort(boom, boom + n);
trans[][] = p, trans[][] = - p;
trans[][] = , trans[][] = ;
} inline void solve() {
int last = boom[n - ];
Matrix<double> f(, );
f[][] = , f[][] = ;
for(int i = n - ; ~i; f[][] = , last = boom[i], i--)
f = matpow(trans, last - boom[i]) * f;
f = matpow(trans, last - ) * f;
printf("%.7lf\n", f[][]);
} int main() {
prepare();
while(~scanf("%d%lf", &n, &p)) { //坑啊,scanf交G++ WA了,交C++过了
init();
solve();
}
return ;
}

poj 3744 Scout (Another) YYF I - 概率与期望 - 动态规划 - 矩阵快速幂的更多相关文章

  1. BZOJ2553 Beijing2011禁忌(AC自动机+动态规划+矩阵快速幂+概率期望)

    考虑对一个串如何分割能取得最大值.那么这是一个经典的线段覆盖问题,显然每次取右端点尽量靠前的串.于是可以把串放在AC自动机上跑,找到一个合法串后就记录并跳到根. 然后考虑dp.设f[i][j]表示前i ...

  2. poj 2778 DNA Sequence 状态及状态转移 AC自动机 矩阵快速幂

    题目链接 题意 给定\(m\)个字符串,问长度为\(n\)的字符串中有多少个不包含那\(m\)个字符串. (字符集为\(A,T,C,G\),\(m\leq 10\),长度\(\leq 10\),\(n ...

  3. POJ 2778 DNA Sequence ( AC自动机、Trie图、矩阵快速幂、DP )

    题意 : 给出一些病毒串,问你由ATGC构成的长度为 n 且不包含这些病毒串的个数有多少个 分析 : 这题搞了我真特么久啊,首先你需要知道的前置技能包括 AC自动机.构建Trie图.矩阵快速幂,其中矩 ...

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

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

  5. POJ 3744 Scout YYF I:概率dp

    题目链接:http://poj.org/problem?id=3744 题意: 有n个地雷,位置为pos[i]. 在每个位置,你向前走一步的概率为p,向前走两步的概率为1-p. 你的初始位置为1. 问 ...

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

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

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

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

  8. POJ 3744 Scout YYF I

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

  9. poj 3744 Scout YYF I (矩阵)

    Description YYF -p. Here is the task, given the place of each mine, please calculate the probality t ...

随机推荐

  1. React对比Vue(02 绑定属性,图片引入,数组循环等对比)

    import React, { Component } from 'react'; import girl from '../assets/images/1.jpg' //这个是全局的不要this.s ...

  2. ORA-01919: role 'PLUSTRACE' does not exist

    环境:Oracle 10g,11g. 现象:在一次迁移测试中,发现有这样的角色赋权会报错不存在: SYS@orcl> grant PLUSTRACE to jingyu; grant PLUST ...

  3. CentOS下nginx+php的配置及nginx开机启动配置

    关闭防火墙 (不然外链接是访问不了 apache) service iptables stop 关闭安全系统 SELinux( 不然报403 访问页面错误 ) 1.Nginx安装主要在于配置文件的修改 ...

  4. 步进电机 28BYJ-48介绍和驱动及编程

    28BYJ-48步进电机: 步进电机是一种将电脉冲转化为角位移的执行机构.通俗一点讲:当步进驱动器接收到一个脉冲信号,它就驱动步进电机按设定的方向转动一个固定的角度(及步进角).您可以通过控制脉冲个来 ...

  5. RootConfig类

    package com.ssm.yjblogs.config; import java.util.Properties; import javax.sql.DataSource; import org ...

  6. 配置tomcat的https域名

    配置tomcat的https域名: <Connector port=" protocol="org.apache.coyote.http11.Http11NioProtoco ...

  7. python 文件描述符

    先上一张图 文件描述符是内核为了高效管理已经被打开的文件所创建的索引, ----非负整数 ----用于指代被打开的文件 ----所有执行i/o操作的系统调用都是通过文件描述符完成的 进程通过文件描述符 ...

  8. Presto上使用SQL遇到的一些坑

    本文转载自:https://segmentfault.com/a/1190000013120454?utm_source=tag-newest 最近换了新工作,在数据处理方面,公司是用Presto连接 ...

  9. Linux基础命令---arch

    Arch         Arch指令主要用于显示当前主机的硬件结构类型,我们可以看到它输出的结果有:i386.i486.mips.alpha等.此命令的适用范围:RedHat.RHEL.Ubuntu ...

  10. 面试题-JAVA算法题

    1.编写一个程序,输入n,求n!(用递归的方式实现). public static long fac(int n){ if(n<=0) return 0; else if(n==1) retur ...