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

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
*/
/*
思路:
1.因为只有两种转移方式,即向前跳一步(p)或者向前跳两步(1-p),所以转移是单向的,
转移方程:dp[i]=dp[i-1]*p+dp[i-2]*(1-p),如果是踩到地雷的情况就不能再继续转移了,加入答案中
2.最多走1e8,也就转移1e8下,但是还是悬在边界上,因为n小,步数小,用快速幂增加速度
3.注意排序,在这里被坑了
*/ #include <cstdio>
#include <algorithm>
using namespace std;
int lame[11];
void copy(double des[2][2],double src[2][2]){
des[0][0]=src[0][0];des[0][1]=src[0][1];des[1][0]=src[1][0];des[1][1]=src[1][1];
}
void multi(double l[2][2],double r[2][2],double ans[2][2]){
double temp[4];
temp[0]=l[0][0]*r[0][0]+l[0][1]*r[1][0];
temp[1]=l[0][0]*r[0][1]+l[0][1]*r[1][1];
temp[2]=l[1][0]*r[0][0]+l[1][1]*r[1][0];
temp[3]=l[1][0]*r[0][1]+l[1][1]*r[1][1];
ans[0][0]=temp[0];ans[0][1]=temp[1];ans[1][0]=temp[2];ans[1][1]=temp[3];
}
void pow(double pro[2][2],double faim[2][2],int times){
double base[2][2] ;
copy(base,pro);
while(times>0){
if((times&1)==1)multi(base,faim,faim);
multi(base,base,base);
times>>=1;
}
}
int main(){
int n;
double p;
while(scanf("%d",&n)==1){
scanf("%lf",&p);
for(int i=0;i<n;i++)scanf("%d",lame+i);
sort(lame,lame+n);
double faim[2][2];faim[1][0]=faim[0][1]=faim[1][1]=0;faim[0][0]=1;
double pro[2][2];pro[0][0]=p;pro[0][1]=1-p;pro[1][0]=1;pro[1][1]=0;
int f=1;
for(int i=0;i<n;i++){
pow(pro,faim,lame[i]-f);
faim[0][0]=0;
f=lame[i];
}
faim[1][0]*=(1-p);
printf("%.7f\n",faim[1][0]);
}
return 0;
}

  

poj 3744 概率dp 快速幂 注意排序 难度:2的更多相关文章

  1. POJ 3744 【矩阵快速幂优化 概率DP】

    搞懂了什么是矩阵快速幂优化.... 这道题的重点不是DP. /* 题意: 小明要走某条路,按照个人兴致,向前走一步的概率是p,向前跳两步的概率是1-p,但是地上有地雷,给了地雷的x坐标,(一维),求小 ...

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

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

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

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

  4. 【bzoj4870】[Shoi2017]组合数问题 dp+快速幂/矩阵乘法

    题目描述 输入 第一行有四个整数 n, p, k, r,所有整数含义见问题描述. 1 ≤ n ≤ 10^9, 0 ≤ r < k ≤ 50, 2 ≤ p ≤ 2^30 − 1 输出 一行一个整数 ...

  5. BZOJ.4818.[SDOI2017]序列计数(DP 快速幂)

    BZOJ 洛谷 竟然水过了一道SDOI!(虽然就是很水...) 首先暴力DP,\(f[i][j][0/1]\)表示当前是第\(i\)个数,所有数的和模\(P\)为\(j\),有没有出现过质数的方案数. ...

  6. Scout YYF I (概率+矩阵快速幂)

    YYF is a couragous scout. Now he is on a dangerous mission which is to penetrate into the enemy's ba ...

  7. [CSP-S模拟测试]:山洞(DP+快速幂)

    题目传送门(内部题17) 输入格式 一行两个整数$n$,$m$,含义如题面. 输出格式 一行一个整数,表示方案数模$1e9+7$. 样例 样例输入1: 4 6 样例输出1: 样例输入2: 707 18 ...

  8. poj 3070 Fibonacci (矩阵快速幂乘/模板)

    题意:给你一个n,输出Fibonacci (n)%10000的结果 思路:裸矩阵快速幂乘,直接套模板 代码: #include <cstdio> #include <cstring& ...

  9. poj 3070 Fibonacci 矩阵快速幂

    Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. F ...

随机推荐

  1. TA-Lib函数对照

    Overlap Studies 重叠研究指标 BBANDS Bollinger Bands 布林带 DEMA Double Exponential Moving Average 双指数移动平均线 EM ...

  2. Django - ORM - 进阶

    一.多表操作 创建模型 实例:我们来假定下面这些概念,字段和关系 作者模型:一个作者有姓名和年龄. 作者详细模型:把作者的详情放到详情表,包含生日,手机号,家庭住址等信息.作者详情模型和作者模型之间是 ...

  3. day13(JSTL和自定义标签&MVC模型&javaweb三层框架)

    day13 JSTL标签库(重点) 自定义标签(理解) MVC设计模式(重点中的重点) Java三层框架(重点中的重点) JSTL标签库   1 什么是JSTL JSTL是apache对EL表达式的扩 ...

  4. mysql数据库导入黑窗口导入导出数据

     一.导出数据库用mysqldump命令(注意mysql的安装路径,即此命令的路径):1.导出数据和表结构:mysqldump -u用户名 -p密码 数据库名 > 数据库名.sql#/usr/l ...

  5. mysql 数据操作 多表查询 多表连接查询 内连接

    内连接:只连接匹配的行 只取两张表共同的部分,相当于利用where 过滤条件从笛卡尔积结果中筛选出了正确的结果 select * from 左表 inner join 要连接的表 on 条件 #dep ...

  6. iOS邮箱、手机号等常用验证功能 判断字符串是否int float

    手机号验证 /* 手机号码 13[0-9],14[5|7|9],15[0-3],15[5-9],17[0|1|3|5|6|8],18[0-9] 移动:134[0-8],13[5-9],147,15[0 ...

  7. 最长DNA重复序列长度,并输出该序列。 JAVA

    1:  最长DNA重复序列长度,并输出该序列. 例如  ATCGTAGATCG,它的最大长度为4,序列为 ATCG. package com.li.huawei; import java.util.S ...

  8. PAT 1147 Heaps[难]

    1147 Heaps(30 分) In computer science, a heap is a specialized tree-based data structure that satisfi ...

  9. 【mybatis】认识selectKey

    转:https://mybatis.github.io/mybatis-3/zh/sqlmap-xml.html 对于不支持自动生成类型的数据库或可能不支持自动生成主键 JDBC 驱动来说,MyBat ...

  10. Django 模型(数据库)

    Django 模型(数据库) )         email = models.EmailField()         memo = models.TextField()   def __unico ...