(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. cocos2d JS 艺术字特殊符号的显示

    this.setSocreAtion(score, this.tfMoneyList[index],mun); //传入分数与对象,调用下面的函数 setSocreAtion : function ( ...

  2. 32.js 判断当前页面是否被浏览

    可以通过document.hidden属性判断当前页面是否是激活状态. 兼容性:IE10+,Firefox10+,Chrome14+,Opera12.1+,Safari7.1+ 兼容性写法示例: va ...

  3. node.js初识01

    1.对于node.js的安装在这里就不做过多的介绍了,安装成功后,可以通过cmd 输入node -v查看node的版本号,如图所示 2.开始我们的hello world,通过cmd进入所属文件夹,输入 ...

  4. webpack 解决跨域问题

    一.webpack 内置了 http-proxy-middleware 可以解决 请求的 URL 代理的问题 安装:npm install --save http-proxy-middleware 二 ...

  5. C# HtmlDocument和HtmlNode的使用以及节点的模糊查询

    C#HtmlAgilityPack.HtmlDocument和HtmlAgilityPack.HtmlNode的使用 HtmlAgilityPack.HtmlDocument response = n ...

  6. [5]windows内核情景分析---进程线程

    本篇主要讲述进程的启动过程.线程的调度与切换.进程挂靠 进程的启动过程: BOOL CreateProcess ( LPCTSTR lpApplicationName,                 ...

  7. ubuntu安装mysql,redis,python-mysqldb

    sudo apt-get install mysql-server sudo apt-get install redis-server sudo apt-get install python-redi ...

  8. CentOS下Yum的$releasever和$basearch的取值

    CentOS下Yum源配置文件中如CentOS-Base.repo的$releasever和$basearch的取值 $releasever的值,这个表示当前系统的发行版本,可以通过如下命令查看: r ...

  9. 【转】Apache Kylin 2.0为大数据带来交互式的BI

    本文转载自:[技术帖]Apache Kylin 2.0为大数据带来交互式的BI 编者注:Kyligence的联合创始人兼CEO Luke Han在上做题为“”的演讲. 基于Hadoop的SQL一直在被 ...

  10. Windows 7关闭睡眠(休眠)模式和删除休眠文件

    原文地址:https://www.192ly.com/pc/win7/gb-sm.html 怎么关闭Windows 7关闭睡眠(休眠)功能?Windows 7系统中怎么删除休眠文件?Windows 7 ...