/*
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. tow sum

    今天面试好打脸!!! 解决方案方法一:暴力法暴力法很简单.遍历每个元素 xx,并查找是否存在一个值与 target−x 相等的目标元素. public int[] twoSum(int[] nums, ...

  2. The adidas NMD Singapore is one of the brands top selling

    Like pointed out, we've two adidas NMD Singapore releases using the first arriving Blue and Black as ...

  3. 团队作业5-测试与发布(alpha阶段)

    团队作业5-测试与发布(alpha阶段) 一.测试 请根据团队项目中软件的需求文档.功能说明.系统设计和测试计划,写出软件的测试过程和测试结果,并回答下述问题. 1. 在测试过程中总共发现了多少Bug ...

  4. [LeetCode]83. Remove Duplicates from Sorted List(排序链表去重)

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  5. 2016-2017 ACM-ICPC Pacific Northwest Regional Contest (Div. 1) Solution

    A:Alphabet Solved. 签. #include<bits/stdc++.h> using namespace std; ]; ]; int main(){ scanf(); ...

  6. HttpClient4.x 使用cookie保持会话

    HttpClient4.x可以自带维持会话功能,只要使用同一个HttpClient且未关闭连接,则可以使用相同会话来访问其他要求登录验证的服务(见TestLogin()方法中的“执行get请求”部分) ...

  7. bzoj1627 / P2873 [USACO07DEC]泥水坑Mud Puddles

    P2873 [USACO07DEC]泥水坑Mud Puddles bfs入门. 对于坐标为负的情况,我们可以给数组下标加上$abs(min(minx,miny))$转正(根据题意判断) #includ ...

  8. 实验五分析system_call中断处理过程

    一.实验要求: 1.使用gdb跟踪分析一个系统调用内核函数 2.根据本周所学知识分析系统调用的过程,从system_call开始到iret结束之间的整个过程,并画出简要准确的流程图 二.实验步骤: 1 ...

  9. 高级bash脚本编程(三)

    高级bash脚本编程 知识点 compound 和 comparison -a 逻辑与 exp1 -a exp2 如果表达式 exp1 和 exp2 都为真的话,那么结果为真. -o 逻辑或 exp1 ...

  10. CEF3开发者系列之CefEnableHighDPISupport详解

    在CEF3中,CefEnableHighDPISupport()这个接口函数在使用时一般不为人所注意,但是如果稍有不慎,会造成打开的网页不能填满窗口的问题.如果是需要flash插件才能运行的游戏.则会 ...