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. oracle创建表空间、创建用户、授权角色和导入导出用户数据

    使用数据库管理员身份登录 -- log as sysdba sqlplus / as sysdba; 创建临时表空间 -- create temporary tablespace create tem ...

  2. [转帖]linux sed命令

    linux sed命令就是这么简单 https://www.cnblogs.com/wangqiguo/p/6718512.html 用到的最多的就是一个sed -i 's/nn/mm/' 的命令了. ...

  3. 1363. ZigZag Conversion

    public class Solution { /** * @param s: the given string * @param numRows: the number of rows * @ret ...

  4. Angular 自定义过滤器

    <!DOCTYPE html><html ng-app="myApp"><head lang="en"> <meta ...

  5. 爬取B站视频

    先安装you_get pip install you_get 爬取代码,爬了个ASMR的,学习困了自我催眠 import sys from you_get import common as you_g ...

  6. 区分Python中的可变对象和不可变对象

    参考: https://www.cnblogs.com/sun-haiyu/p/7096918.html """不过注意函数传参既不是传值也不是传引用,正确的叫法是传对象 ...

  7. Django--权限信息操作

    一 . 权限控制 表结构的设计 rbca(Role Based Access Control)  基于角色的权限控制 3个model  5张表 class User(models.Model): # ...

  8. Springboot中使用Xstream进行XML与Bean 相互转换

    在现今的项目开发中,虽然数据的传输大部分都是用json格式来进行传输,但是xml毕竟也会有一些老的项目在进行使用,正常的老式方法是通过获取节点来进行一系列操作,个人感觉太过于复杂.繁琐.推荐一套简单的 ...

  9. linux 地址解析协议 arp

    随便转载,保留出处:http://www.cnblogs.com/aaron-agu/ arp –na #查看 arp –s 123.253.68.209 00:19:56:6F:87:D4 #添加

  10. python之读取和写入csv文件

    写入csv文件源码: #输出数据写入CSV文件 import csv data = [ ("Mike", "male", 24), ("Lee&quo ...