Description

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, 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, YYF is at step one. For each step after that, 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 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 一条路上一共有n个地雷,每次从 i 位置有 p 的概率走向 i+1 位置,有 1-p 的概率走向 i+2 位置,问在这条路上走不被炸死的概率是多大。
用dp[i] = x 表示走到第 i 个位置的概率,那么dp[i] = p * dp[i-1] + (1-p) * dp[i-2].
但是 i 的范围在 1e8 内,所以不能直接遍历。
一开始想错了,以为第 x 位置的地雷只会对后面两个位置产生影响,这种情况是 p = 0.5的特殊情况下的。
假设地雷的位置是x1, x2 可以把 1->x1 之间不踩到地雷和 x1+1 -> x2 之间不踩到地雷看成两个独立事件,然后最后讲每次(1-dp[地雷])的概率相乘就可以了。
然后接下来就是对从 1->x 过程求 dp[x] 概率了,这个过程可以通过矩阵快速幂来加速。
 /*
.
';;;;;.
'!;;;;;;!;`
'!;|&#@|;;;;!:
`;;!&####@|;;;;!:
.;;;!&@$$%|!;;;;;;!'.`:::::'.
'!;;;;;;;;!$@###&|;;|%!;!$|;;;;|&&;.
:!;;;;!$@&%|;;;;;;;;;|!::!!:::;!$%;!$%` '!%&#########@$!:.
;!;;!!;;;;;|$$&@##$;;;::'''''::;;;;|&|%@$|;;;;;;;;;;;;;;;;!$;
;|;;;;;;;;;;;;;;;;;;!%@#####&!:::;!;;;;;;;;;;!&####@%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''::;;;;|%$$|!!&###&'
|##&%!;;;;;::''''''''''''::;;;;;;;!$@&:`!'
:;!@$|;;;;;;;::''''''''''':;;;;;;;;!%&@$: !@#$'
|##@@&%;;;;;::''''''''':;;;;;;;!%&@#@$%: '%%!%&;
|&%!;;;;;;;%$!:''''''':|%!;;;;;;;;|&@%||` '%$|!%&;
|@%!;;!!;;;||;:'''''':;%$!;;;;!%%%&#&%$&: .|%;:!&%`
!@&%;;;;;;;||;;;:''::;;%$!;;;;;;;|&@%;!$; `%&%!!$&:
'$$|;!!!!;;||;;;;;;;;;;%%;;;;;;;|@@|!$##; !$!;:!$&:
|#&|;;;;;;!||;;;;;;;;!%|;;;;!$##$;;;;|%' `%$|%%;|&$'
|&%!;;;;;;|%;;;;;;;;$$;;;;;;|&&|!|%&&; .:%&$!;;;:!$@!
`%#&%!!;;;;||;;;;;!$&|;;;!%%%@&!;;;!!;;;|%!;;%@$!%@!
!&!;;;;;;;;;||;;%&!;;;;;;;;;%@&!;;!&$;;;|&%;;;%@%`
'%|;;;;;;;;!!|$|%&%;;;;;;;;;;|&#&|!!||!!|%$@@|'
.!%%&%'`|$; :|$#%|@#&;%#%.
*/
#include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define lowbit(x) x & (-x)
#define mes(a, b) memset(a, b, sizeof a)
#define fi first
#define se second
#define pii pair<int, int>
#define INOPEN freopen("in.txt", "r", stdin)
#define OUTOPEN freopen("out.txt", "w", stdout) typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 1e5 + ;
const int maxm = 1e5 + ;
const int mod = 1e9 + ;
const ll INF = 1e18 + ;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-;
using namespace std; int n, m;
int cas, tol, T; struct Mat {
double mat[][];
void init() {
for(int i=; i<=; i++) {
for(int j=; j<=; j++) {
mat[i][j] = 0.0;
}
}
}
};
int a[]; Mat mmul(Mat A, Mat B) {
Mat ans;
ans.init();
for(int i=; i<=; i++) {
for(int j=; j<=; j++) {
for(int k=; k<=; k++) {
ans.mat[i][j] += A.mat[i][k] * B.mat[k][j];
}
}
}
return ans;
} Mat mpow(Mat A, int b) {
Mat ans;
ans.init();
for(int i=; i<=; i++)
ans.mat[i][i] = 1.0;
while(b) {
if(b & )
ans = mmul(ans, A);
A = mmul(A, A);
b >>= ;
}
return ans;
} int main() {
double p, q;
while(~scanf("%d%lf", &n, &p)) {
mes(a, );
q = 1.0 - p;
int flag = ;
for(int i=; i<=n; i++) {
scanf("%d", &a[i]);
if(a[i] == ) {
flag = ;
}
}
if(flag) {
printf("0.0000000\n");
continue;
}
a[n+] = ;
n++;
sort(a+, a++n);
double ans = 1.0;
Mat A;
for(int i=; i<=n; i++) {
if(a[i] == a[i-]) continue;
int tmp = a[i] - a[i-] + ;
if(tmp == ) {
ans *= 0.0;
} else {
A.mat[][] = p;
A.mat[][] = q;
A.mat[][] = 1.0;
A.mat[][] = 0.0;
A = mpow(A, tmp-);
// for(int i=1; i<=2; i++) {
// for(int j=1; j<=2; j++) {
// printf("%f%c", A.mat[i][j], j==2 ? '\n' : ' ');
// }
// }
ans *= ( - A.mat[][]);
}
}
printf("%.7f\n", ans);
}
return ;
}

Scout YYF I POJ - 3744(概率dp)的更多相关文章

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

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

  2. poj 3744 概率dp 快速幂 注意排序 难度:2

    /* Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5304   Accepted: 1455 De ...

  3. 概率dp(A - Scout YYF I POJ - 3744 )

    题目链接:https://cn.vjudge.net/contest/276241#problem/A 题目大意:首先输入n和p,n代表地雷的个数,p代表走一步的概率,1-p代表走两步的概率,然后问你 ...

  4. Scout YYF I POJ - 3744【矩阵乘法优化求概率】

    题意: 一条路上有 $n$ 个地雷,YYF 从位置 $1$ 出发,走一步的概率为 $p$,走两步的概率是 $(1-p)$.求 YYF 能顺利通过这条路的概率. 数据范围: $1\leq n \leq ...

  5. Scout YYF I POJ - 3744(矩阵优化)

    题意:一条路上有n个地雷,给出地雷的位置.某人从起点(位置1)出发,走一步的概率是p,走两步的概率是(1-p),然后问有多少概率走过这个雷区. 思路: 只要走过最后一个地雷就代表走过雷区了. 而每到 ...

  6. poj 3744 概率dp+矩阵快速幂

    题意:在一条布满地雷的路上,你现在的起点在1处.在N个点处布有地雷,1<=N<=10.地雷点的坐标范围:[1,100000000]. 每次前进p的概率前进一步,1-p的概率前进1-p步.问 ...

  7. POJ 2096 (概率DP)

    题目链接: http://poj.org/problem?id=2096 题目大意:n种bug,s个子系统.每天随机找一个bug,种类随机,来自系统随机.问找齐n种bug,且每个子系统至少有一个bug ...

  8. POJ 2151 概率DP

    主要的子问题是每一个队伍有一个做出题目的概率,求做出k个题目的概率.简单的简单的组合数DP.想清楚即可. 1: #include <iostream> 2: #include <cs ...

  9. POJ 3701 概率DP

    给定2^n 支足球队进行比赛,n<=7. 队伍两两之间有一个获胜的概率,求每一个队伍赢得最后比赛的概率是多少? 状态其实都是很显然的,一开始觉得这个问题很难啊,不会.dp[i][j] 表示第i支 ...

随机推荐

  1. react中如何使用动画效果

    在react中想要加入动画效果 需要引入 import {CSSTransitionGroup} from 'react-transition-group' //加入react 动画包 import ...

  2. React Native之倒计时组件的实现(ios android)

    React Native之倒计时组件的实现(ios android) 一,需求分析 1,app需实现类似于淘宝的活动倒计时,并在倒计时结束时,活动也结束. 2,实现订单倒计时,并在倒计时结束时,订单关 ...

  3. Azure系列2.1.5 —— BlobOutputStream

    (小弟自学Azure,文中有不正确之处,请路过各位大神指正.) 网上azure的资料较少,尤其是API,全是英文的,中文资料更是少之又少.这次由于公司项目需要使用Azure,所以对Azure的一些学习 ...

  4. C# Note29: Close()和Dispose()的区别

    待更! 深入解析Close()和Dispose()的区别

  5. 关于手机端适配的问题(rem,页面缩放)

    关于手机端适配的问题(rem,页面缩放) 96 进击的小前端 关注 2018.02.02 13:57 字数 320 阅读 19评论 0喜欢 0 相信很多和会和我碰到一样的情况,就是你用rem去写移动端 ...

  6. python学习笔记(11)--数据组织的维度

    数据的操作周期 存储  -- 表示 -- 操作 一维数据表示 如果数据有序,可以使用列表[]:如果数据没有顺序,可以使用集合{} 一维数组存储 存储方式一:空格分隔 ,使用一个或多个空格分隔进行分隔, ...

  7. python学习笔记(10)--组合数据类型(字典类型)

    理解映射: 映射是一种键(索引)和值(数据)的对应.字典是键值对的集合,键值之间无序.用大括号表示{},和dict()创建,键值对用冒号:表示. {键:值,键:值,键:值} >>> ...

  8. Postman & API

    Postman & API https://www.getpostman.com/ https://www.getpostman.com/downloads/ Postman Canary h ...

  9. 关于浏览器兼容问题——还有移动端meta问题

    <!DOCTYPE html><!--[if lt IE 7]> <html dir="ltr" lang="en-US" cla ...

  10. AI算法第三天【矩阵分析与应用】