(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. cocos2dx 3.x(绘制线条)

    // //  MainScene.hpp //  helloworld // //  Created by apple on 16/9/19. // // #ifndef MainScene_hpp ...

  2. cocos2dx JS 层(Layer)的生命周期

    场景的生命周期: 一般情况下一个场景只需要一个层,需要创建自己的层类.一些主要的游戏逻辑代码都是写在层中的,场景的生命周期是通过层的生命周期反映出来的,通过重写层的生命周期函数,可以处理场景不同声明周 ...

  3. node.js连接MongoDB数据库,db.collection is not a function完美解决

    解决方法一. mongodb数据库版本回退: 这个错误是出在mongodb的库中,在nodejs里的写法和命令行中的写法不一样,3.0的api已经更新和以前的版本不不一样,我们在npm中没指定版本号的 ...

  4. gitlab覆盖率

    https://danny50610.blogspot.com/2017/09/gitlab-ci-phpunit-coverage.html

  5. Msfvenom木马使用及TheFatRat工具

    msfvenom –platform windows -p windows/x64/shell/reverse_tcp LHOST=192.168.168.111 LPORT=3333 EXITFUN ...

  6. arc 092D Two Sequences

    题意: 给出两个长度N相同的整数序列A和B,有N^2种方式从A中选择一个数Ai,从B中选择一个数Bj,让两个数相加,求这N^2个数的XOR,即异或. 思路: 暴力的求显然是会超时的,因为是异或,就考虑 ...

  7. QT构建窗体(父窗体传为野指针)异常案例

    [1]源码 工作中,时常会遇到各种各样的异常场景,有些异常场景很常见,必要备录,以防再犯. 分享本案例为:QT创建窗体时parent父窗体传野指针引起异常. 本案例源码如下: 1.1 默认新建一个QT ...

  8. array_contains 分析函数使用演示

    Hive中的array_contains函数与SQL中的 in关键字 操作类似,用于判定 包含(array_contains)或不包含(!array_contains)关系.与 in不同的是array ...

  9. vc709时钟信号报单端信号错误的记录

    话说,为什么我又要跑去搞fpga玩了,不是应该招个有经验的开发人员么?大概是练度不够吧…… Xilinx这个板子阿,真鸡儿贵,我这还没啥基础,慢慢试吧: 看了乱七八糟各种文档先不提,我还是决定先控制L ...

  10. qq网吧弹框如何去掉?如何删掉NetBar文件夹?

    qq网吧弹框如何去掉?如何删掉NetBar文件夹?有些qq会弹出qq网吧,让人烦恼.而且点了那个不是网吧的反馈了多次都还会弹出.如何退出关闭删除取消去掉qq网吧呢,下面介绍一种解决方法:1.打开qq安 ...