/*
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. Django - 路由层(URLconf)

    一.django 静态文件配置 /mysite1/settings.py STATIC_URL = '/static/' STATICFILES_DIRS = [ os.path.join(BASE_ ...

  2. Nginx服务基础

    Nginx的英文官方网站是http://nginx.org,在这里可以查看Nginx的各个软件版本信息.Nginx软件有三种版本:稳定版.开发版和历史稳定版.开发版更新较快,包含最新的功能和bug的修 ...

  3. php源码编译常见错误解决方案大全

    php源码编译常见错误解决方案大全http://www.cnlvzi.com/index.php/Index/article/id/143 在CentOS编译PHP5的时候有时会遇到以下的一些错误信息 ...

  4. Linux下如何执行Shell脚本

    Linux下你可以有两种方式执行Shell脚本: 1.用shell程序执行脚本:根据你的shell脚本的类型,选择shell程序,常用的有sh,bash,tcsh等(一般来说第一行#!/bin/bas ...

  5. PhotoSwipe中文API(四)

    在幻灯片自定义HTML内容 为了使PhotoSwipe显示HTML内容的幻灯片,你需要在幻灯片对象定义html属性.它应该包含HTML字符串或DOM元素对象. var items = [ // sli ...

  6. OPENSSL编程 (secure shell, ssh)

    很好的 OPENSSL编程 教程,名字就叫“OPENSSL编程” 它里面还有很多关于密码学的东西. http://www.pengshuo.me http://www.pengshuo.me/2014 ...

  7. bat笔记

    背景介绍 现入职的公司包含发送EDM的项目,每天都有各种题型邮件需要发送,但是由于各种原因,发送EDM程序的服务器老是被网管各种重启 :) 作为负责人,对这事很恼火,隔几天就被投诉,怎么又没收到考勤邮 ...

  8. 001-java虚拟机的概念

    https://blog.csdn.net/yfqnihao/article/details/8289363 1.什么是java虚拟机.(你以为你知道,如果你看我下面的例子,你会发现你其实不知道) ( ...

  9. Java实现使用位图生成真值组合

    摘要: 使用位图生成真值组合. 难度: 初级.   /** * 问题描述: 给定 n 个布尔变量,打印所有真值组合. * 例如, n = 2 时 , 所有真值组合为 (true, false),(tr ...

  10. 搭建Mybatis 出现 Error querying database. Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for mapper.BatchCustomer.findBatchCustomerOneToOne

    Error querying database. Cause: java.lang.IllegalArgumentException: Mapped Statements collection doe ...