题目链接:

http://poj.org/problem?id=3744

Scout YYF I

Time Limit: 1000MS
Memory Limit: 65536K
#### 问题描述
> 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.

输入

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].

输出

For each test case, output the probabilty in a single line with the precision to 7 digits after the decimal point.

样例输入

1 0.5

2

2 0.5

2 4

样例输出

0.5000000

0.2500000

题意

一个人从1号节点出发向右走,有n个位子有地雷,这个人走一步的概率为p,走两步的概率为1-p,现在问这个人安全走过去的概率为多少。

题解

由于雷很少,所以可以单独处理下有地雷的位置的时候的转移,比如x的位置有雷,那么就有dp[x+1]=dp[x-1]*(1-p),dp[x+2]=dp[x+1]*p,。其他一整片没有雷的就直接矩阵快速幂干过去(类似于斐波那契数列),起始位置的概率为1.0,然后模拟扫过去求出dp[last+1]就可以了(last指最后一个雷的位置)。

代码

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf typedef __int64 LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII; const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-9; const double PI = acos(-1.0); //start---------------------------------------------------------------------- const int maxn=2; struct Matrix{
double mat[maxn][maxn];
Matrix(){ clr(mat,0); }
friend Matrix operator *(const Matrix& A,const Matrix& B){
Matrix ret;
for(int i=0;i<maxn;i++){
for(int j=0;j<maxn;j++){
for(int k=0;k<maxn;k++){
ret.mat[i][j]=ret.mat[i][j]+A.mat[i][k]*B.mat[k][j];
}
}
}
return ret;
}
}I; Matrix pow(Matrix A,int n){
Matrix ret=I;
while(n){
if(n&1) ret=ret*A;
A=A*A;
n/=2;
}
return ret;
} int n;
double p; int main() {
rep(i,0,maxn) I.mat[i][i]=1;
while(scf("%d%lf",&n,&p)==2&&n){
vector<int> arr;
rep(i,0,n){
int x; scf("%d",&x);
arr.pb(x);
} sort(all(arr)); arr.erase(unique(all(arr)),arr.end()); if(arr.sz()==0){ prf("1.0000000\n"); continue; } bool su=true;
if(arr[0]==1) su=false;
else{
for(int i=0;i<arr.sz()-1;i++){
if(arr[i+1]-arr[i]==1){
su=false; break;
}
}
} if(!su){ prf("0.0000000\n"); continue; } Matrix A;
A.mat[0][0]=p, A.mat[0][1]=1-p;
A.mat[1][0]=1, A.mat[1][1]=0; double pre=1.0;
for(int i=0;i<arr.sz();i++){
int cnt;
if(i==0) cnt=arr[i]-1;
else cnt=arr[i]-arr[i-1]-1; if(cnt==1){ pre*=(1-p); }
else{
double f0=pre,f1=pre*p; Matrix vec;
vec.mat[0][0]=pre*p;
vec.mat[1][0]=pre; vec=pow(A,cnt-2)*vec;
pre=vec.mat[0][0]; pre*=(1-p);
}
} prf("%.7f\n",pre);
}
return 0;
} //end-----------------------------------------------------------------------

POJ 3744 Scout YYF I 概率dp+矩阵快速幂的更多相关文章

  1. poj 3744 Scout YYF 1 (概率DP+矩阵快速幂)

    F - Scout YYF I Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub ...

  2. POJ3744 Scout YYF I 概率DP+矩阵快速幂

    http://poj.org/problem?id=3744 题意:一条路,起点为1,有概率p走一步,概率1-p跳过一格(不走中间格的走两步),有n个点不能走,问到达终点(即最后一个坏点后)不踩坏点的 ...

  3. poj 3744 Scout YYF I(概率dp,矩阵优化)

    Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5020   Accepted: 1355 Descr ...

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

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

  5. poj4474 Scout YYF I(概率dp+矩阵快速幂)

    Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4100   Accepted: 1051 Descr ...

  6. 刷题总结—— Scout YYF I(poj3744 矩阵快速幂+概率dp)

    题目: Description YYF is a couragous scout. Now he is on a dangerous mission which is to penetrate int ...

  7. poj3744 (概率DP+矩阵快速幂)

    http://poj.org/problem?id=3744 题意:在一条铺满地雷的路上,你现在的起点在1处.在N个点处布有地雷,1<=N<=10.地雷点的坐标范围:[1,10000000 ...

  8. poj3744 Scout YYF I[概率dp+矩阵优化]

    Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8598   Accepted: 2521 Descr ...

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

    题意: 一条路上,给出n地雷的位置,人起始位置在1,向前走一步的概率p,走两步的概率1-p,踩到地雷就死了,求安全通过这条路的概率. 分析: 如果不考虑地雷的情况,dp[i],表示到达i位置的概率,d ...

随机推荐

  1. SAP函数PREPARE_STRING:提取字符串中的数字

    今天调整一个同事的需求时,要计算一个含税金额.报表内已经取到税率,但存在的形式是字符串格式:16%. 正好SAP内有一个标准函数:PREPARE_STRING 可以处理字符串,将特别标志替换为有效标志 ...

  2. jQuery animate() 改变颜色

    jQuery提供的animate()方法可以实现一些简单的动画效果,但是其核心库不提供颜色动画的效果,如果想实现颜色动画,需要下载相关插件. 但是,animate()的参数中有一个complete,通 ...

  3. 十七、S3C2440 音频解码芯片WM8976声卡驱动移植、madplay测试

    学习目标:1. WM9876接口和工作原理:2. WM9876驱动移植:3. WM9876应用测试:4. 问题总结 1. WM9876接口和工作原理  本节使用了JZ2440开发板移植WM9876驱动 ...

  4. 2017-2018-1 20155239 《信息安全系统设计基础》第五周学习总结+mybash的实现

    2017-2018-1 20155239 <信息安全系统设计基础>第五周学习总结+mybash的实现 mybash的实现 使用fork,exec,wait实现mybash 写出伪代码,产品 ...

  5. WPF 自定义ComboBox样式,自定义多选控件

    原文:WPF 自定义ComboBox样式,自定义多选控件 一.ComboBox基本样式 ComboBox有两种状态,可编辑和不可编辑状态.通过设置IsEditable属性可以切换控件状态. 先看基本样 ...

  6. 【Vijos】lxhgww的奇思妙想

    题面 题解 求$k$级祖先孙子 为什么要用长链剖分啊??? 倍增并没有慢多少... 其实是我不会 长链剖分做这道题还是看这位巨佬的吧. 代码 #include<bits/stdc++.h> ...

  7. [JOISC2018]道路建设 LCT

    [JOISC2018]道路建设 LOJ传送门 考的时候打的大暴力,其实想到了LCT,但是思路有点没转过来.就算想到了估计也不能切,我没有在考场写LCT的自信... 其实这题不是让你直接用LCT维护答案 ...

  8. Java 原生日志 java.util.logging

    简介 Java 中的 Logging API 让 Java 应用可以记录不同级别的信息,它在debug过程中非常有用,如果系统因为各种各样的原因而崩溃,崩溃原因可以在日志中清晰地追溯,下面让我们来看看 ...

  9. POJ-2299 Ultra-QuickSort (树状数组)

    题目链接:Ultra-QuickSort 题意: 给出了一个序列,序列中有n个数,现在每次操作能交换相邻的两个数,要求操作几次可以将这个序列转换为一个从小到大排序的序列. 题解: 我的解法是先把所有的 ...

  10. idea tomcat 热部署方法

    https://jingyan.baidu.com/article/db55b609d2a1564ba20a2f61.html 还好有热部署,不然每次都要重启服务,实在是太浪费时间了~ 注意事项: 在 ...