/*
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. 通过 微软 pai-fs 上传数据到HDFS (Microsoft OpenPAI)

    准备环境 (个人使用记录,方便下次使用查阅~~) 首先保证PAI是登陆状态: 进入GitHub项目所在地址: https://github.com/Microsoft/pai/ 然后切换分支到  具体 ...

  2. 【Python】自动化测试框架-共通方法汇总

    1.滚动滚动条(有的时候页面元素element取得对但是并没有回显正确的数据,可能是因为页面第一次加载很慢,所以页面可能做了滚动到哪里就加载到哪里的效果,此刻我们就需要用到滚动条自动滚动这段代码让页面 ...

  3. python分段算利润、税收

    ''' 题目:企业发放的奖金根据利润提成. 利润(I)低于或等于10万元时,奖金可提10%: 利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.5%: 2 ...

  4. (1.4)DML增强功能-Output

    Output在CRUD的区别 1.对于INSERT,可以引用inserted表以查询新行的属性.在insert into table output . 2.对于DELETE,可以引用deleted表以 ...

  5. 用SQL语句检查CPU和磁盘空间

    --查看4小时内的CPU变化值,1分钟统计一次 DECLARE @ts_now BIGINT; SELECT @ts_now = ms_ticks FROM sys.dm_os_sys_info; - ...

  6. testng生成报告ReportNG美化测试报告

    testng生成报告ReportNG美化测试报告 testng生成报告ReportNG美化测试报告 ReportNG 是一个配合TestNG运行case后自动帮你在test-output文件内生成一个 ...

  7. 3.6 Templates -- Binding Element Class Names(绑定元素类名)

    1. 一个HTML元素的class属性可以像其他属性一样被绑定: <div class={{priority}}> Warning! </div> 生成的HTML <di ...

  8. C# 使用BackgroundWorker实现WinForm异步

    写了一个基于BackgorundWorker演示异步操作的例子.由于这个理基本上实现了BackgorundWorker的大部分功能:异步操作的启动.操作结束后的回调.异步操作的撤销和进度报告等等.尽管 ...

  9. navicat中文破解版,navicat破解版,navicat for mysql10.0.11简体中文破解版

    https://blog.csdn.net/weixin_40426638/article/details/78933585 下载链接如下(里面有破解码) https://pan.baidu.com/ ...

  10. 20145326《Java程序设计》第二周学习总结

    20145326<Java程序设计>第二周学习总结 教材学习内容总结 本周学习教材第三章,本章主要讲述了java语言中的一些基础语法,java是个支持面向对象的程序语言,但在正式进入面向对 ...