我是靠谱客的博主 隐形小笼包,这篇文章主要介绍poj 3744 Scout YYF I(矩阵快速幂优化dp),现在分享给大家,希望可以做个参考。

题目链接:

点击打开链接

题目大意:

给出一些地雷,当前人在位置1,人走一步的概率为p,走两步的概率为1-p,问这个人安全走完这段路的概率

题目分析:

很容易得到的dp式子,dp[i] = p*dp[i-1] + (1-p)*dp[i-2],然后这个式子的推倒是可以通过矩阵快速幂进行优化的,达到Log级的复杂度,其中dp[i]表示走到i这个位置的概率,

那么最后结果就是一个递推:

初始ans =1 , 每遇到一个雷,ans *= ( 1 - dp[loc[i] - loc[i-1]-1] ) ,就是前几个雷都没踩到的概率,乘以从loc[i-1]+1开始走当前雷踩不到的概率,然后推倒到最后就是所有的雷都踩不到

代码如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> #include<math.h> using namespace std; struct Matrix { double mat[2][2]; }; Matrix mul(Matrix a,Matrix b) { Matrix ret; for(int i=0;i<2;i++) for(int j=0;j<2;j++) { ret.mat[i][j]=0; for(int k=0;k<2;k++) ret.mat[i][j]+=a.mat[i][k]*b.mat[k][j]; } return ret; } Matrix pow_M(Matrix a,int n) { Matrix ret; memset(ret.mat,0,sizeof(ret.mat)); for(int i=0;i<2;i++)ret.mat[i][i]=1; Matrix temp=a; while(n) { if(n&1)ret=mul(ret,temp); temp=mul(temp,temp); n>>=1; } return ret; } int x[30]; int main() { int n; double p; while( cin >> n >> p ) { for(int i=0;i<n;i++) scanf("%d",&x[i]); sort(x,x+n); double ans=1; Matrix tt; tt.mat[0][0]=p; tt.mat[0][1]=1-p; tt.mat[1][0]=1; tt.mat[1][1]=0; Matrix temp; temp=pow_M(tt,x[0]-1); ans*=(1-temp.mat[0][0]); for(int i=1;i<n;i++) { if(x[i]==x[i-1])continue; temp=pow_M(tt,x[i]-x[i-1]-1); ans*=(1-temp.mat[0][0]); } printf("%.7fn",ans); } return 0; }


最后

以上就是隐形小笼包最近收集整理的关于poj 3744 Scout YYF I(矩阵快速幂优化dp)的全部内容,更多相关poj内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(72)

评论列表共有 0 条评论

立即
投稿
返回
顶部