(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-x c++ (多种屏幕Android与iOS的适配原理)

    1.AppDelegate.cpp 文件中 bool AppDelegate::applicationDidFinishLaunching() { // initialize director aut ...

  2. spring注解式开发之视图解析器

    http://localhost:8089/springmvc-04-viewResovler/springmvc/hello

  3. jdbc连接oracle时使用的字符串格式

    两种方式: SID的方式: jdbc:oracle:thin:@[HOST][:PORT]:SID SERVICE_NAME的方式: jdbc:oracle:thin:@//[HOST][:PORT] ...

  4. 原生JS实现addClass,removeClass,toggleClass

    jQuery操作class的方式非常强大,但是目前还有一些人不知道如何使用或者由于项目统一性的原因无法使用jquery. 在此写了一个利用原生js来实现对dom元素class的操作方法 1.addCl ...

  5. Yii Restful api认证

  6. 键值对Dictionary、KeyValuePair、Hashtable 简单使用。

    KeyValuePair是单个的键值对对象.KeyValuePair可用于接收combox选定的值. 例如:KeyValuePair<string, object> par = (KeyV ...

  7. uvalive 5731 Qin Shi Huang’s National Road System

    题意: 秦始皇要修路使得所有的城市连起来,并且花费最少:有一个人,叫徐福,他可以修一条魔法路,不花费任何的钱与劳动力. 秦始皇想让修路的费用最少,但是徐福想要受益的人最多,所以他们经过协商,决定让 A ...

  8. 取n到m行

    取n到m行 . select top m * from tablename where id not in (select top n id from tablename order by id as ...

  9. sublime text3 快捷键和好用的插件

    常用快捷键: Ctrl + D 选中一个单词 Ctrl + L 选中一行 Ctrl + A 全选 Ctrl + M 选中括号内所有内容 (编写CSS或JS时非常实用) Ctrl + G 快速定位到某一 ...

  10. ymPrompt,jcs缓存架构

    jcs.auxiliary.LTCP=org.apache.jcs.auxiliary.lateral.socket.tcp.LateralTCPCacheFactory#jcs.auxiliary. ...