(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++ (iOS)集成第三方微信分享

    1.新建项目并下载 ShareSDK 1.Cocos2d-x项目环境搭建,不会的童鞋自行面壁哈: 网页链接. 2.ShareSDK iOS版本的 Cocos2d-x 插件是在ShareSDK iOS版 ...

  2. Visual Studio 2015 开发Android Cordova出现unsupported major minor version 52.0错误的解决方法

    JDK版本的问题,需要JDK1.8版本,安装!VS2015做如下设置, 工具->选项->用于Apache Cordoba的工具->环境变量替代->JAVA_HOME设为1.8:

  3. 第二章:Opencv核心類Mat

    Opecv就是做計算機視覺,就是讲图片转换成计算机所能识别的数据 Mat类中由大量的内联函数,主要就是用于提高速度. 一般类型都用rgb,存的时候用CV_8UC3.create函数一般会把原来的空间释 ...

  4. C++实现 safaBase64编码跟nonSafeBase64编码的转换

    默认Base64编码的字符串,用于网络传输是不安全的,因为Base64编码使用的标准字典含有“+”,“/”. 规则如下: //nonSafeBase64 到 safeBase64'+'  ------ ...

  5. InstallShield2015制作安装包----------卸载前结束执行中的进程

    方法一:InstallShiel直接调用cmd命令来杀掉进程. //更新或卸载时先关闭应用程序 sCmdLine=" /c taskkill /f /im \"Frs.exe\&q ...

  6. POJ 3233 Matrix Power Series(二分等比求和)

    Matrix Power Series [题目链接]Matrix Power Series [题目类型]二分等比求和 &题解: 这题我原来用vector写的,总是超时,不知道为什么,之后就改用 ...

  7. IIS 8.0 Using ASP.NET 3.5 and ASP.NET 4.5微软官方安装指导

    from:https://www.iis.net/learn/get-started/whats-new-in-iis-8/iis-80-using-aspnet-35-and-aspnet-45 S ...

  8. Unity shader学习之屏幕后期处理效果之边缘检测

    边缘检测的原理是利用一些边缘检测算子对图像进行卷积操作. 转载请注明出处:http://www.cnblogs.com/jietian331/p/7232707.html 例如: 代码如下: usin ...

  9. sv命令空间 packge

    SV中的module,interface,program,checker,都提供declaration空间,内部定义都local当前的那个scope,相互之间的building block不影响,不识 ...

  10. [11]Windows内核情景分析---设备驱动

    设备驱动 设备栈:从上层到下层的顺序依次是:过滤设备.类设备.过滤设备.小端口设备[过.类.过滤.小端口] 驱动栈:因设备堆栈原因而建立起来的一种堆栈 老式驱动:指不提供AddDevice的驱动,又叫 ...