/*
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. iOS定位原理和使用建议(转)

    原文:http://ibbs.91.com/thread-1548870-1-1.html 看到很多网友讨论iOS设备定位的问题,这里将我们所了解的关于iPhone.iPad.iPod等的定位原理做详 ...

  2. CCScene,CCLayer,CCSprite,CCDirector

    一.CCScene : 游戏中不同的画面可以用不同的场景展示出来,大致的可以分为以下的几类场景: 1. 展示类场景.游戏开场画面,游戏简介,胜利以及失败提示,帮助. 2. 选择类场景.主菜单,游戏设置 ...

  3. java模拟网页http-url访问

    package com.iflytek; import java.io.InputStream; import java.net.HttpURLConnection; import java.net. ...

  4. Python扩展之类的魔术方法

    Python中类的魔术方法 在Python中以两个下划线开头的方法,__init__.__str__.__doc__.__new__等,被称为"魔术方法"(Magic method ...

  5. Windows mysql默认字符集修改

    一.通过MySQL命令行修改: set character_set_client=utf8; set character_set_connection=utf8; set character_set_ ...

  6. git相关使用技巧和问题

    本地有修改和提交,如何强制用远程的库更新更新.我尝试过用git pull -f,总是提示 You have not concluded your merge. (MERGE_HEAD exists). ...

  7. Integration Services 变量

    如果没有变量,你会发现在ssis里面啥都干不成,和人没有灵魂一样 对系统变量唯一可配置的选项是指定变量在更改值时是否引发事件. 待续

  8. iOS重签名脚本

    unzip xxx.ipa //解压ipa rm -rf Payload/ xxx.app/_CodeSignature //删除旧签名 cp newEmbedded.mobileprovision ...

  9. 项目中使用protobuf

    在互种系统中数据通信或数据交换可以使用protobuf,他比json.xml的数据量要小一些. 另外因为消息要单独写一个.proto文件,来生成各平台的代码,所以对跨平台通信来说也比较友好. 一.使用 ...

  10. 337BRoutine Problem

    /*给出你图片的长和宽的比例a:b 和摄像头的比例c:d,然后叫你求最后将图片放进摄像头 以后,剩余的面积比上摄像头的总面积,注意要化简为最简形式,而且摄像头要设置成至少一条边和图片相等 做法:先将两 ...