poj-3744 Scout YYF I [用矩阵优化概率递推式]
2016-05-20 19:47
302 查看
/* 题意:在一条不满地雷的路上,你现在的起点在1处。 在N个点处布有地雷,1<=N<=10。地雷点的坐标范围:[1,100000000]. 每次前进p的概率前进一步,1-p的概率前进两步。 问顺利通过这条路的概率。就是不要走到有地雷的地方。 dp[i] 表示走到i的概率 dp[i+1]=dp[i]*p+dp[i-1]*(1-p); 用矩阵快速幂优化 递推式转矩阵 | 0 1| |dp(k-1)| = | dp(k) | | 1-p p| | dp(k) | | dp(k+1)| dp[0]=0,dp[1]=1; 设x[i] 表示第i个地雷的位置 把道路分成开 1~x[1] x[1]+1~x[2]...... x[n-1]+1~x 对于每一段 sus[i] 表示 x[i-1]+1~x[i]-1这一段没有挂掉 那么sus[i]= 1-从x[i-1]+1刚好走到x[i]的概率; 那么 ans为sus[i]的总乘积(即每一段都没屎) */ #include <stdio.h> #include <algorithm> #include <vector> using namespace std; typedef vector<double> vec; typedef vector<vec> mat; const int maxn=30; const double eps=1E-8; int x[maxn]; int n; double p; mat mul(const mat &a,const mat &b) { mat c(2,vec(2,0)); for(int i=0;i<2;++i) for(int j=0;j<2;++j) for(int k=0;k<2;++k) c[i][j]+=a[i][k]*b[k][j]; return c; } double T(int k) { double s[2][2]={{0,1},{1-p,p}}; mat A(2,vec(2)); mat p(2,vec(2,0)); p[0][0]=p[1][1]=1; for(int i=0;i<2;++i) for(int j=0;j<2;++j) A[i][j]=s[i][j]; while(k>0) { if(k&1) p=mul(A,p); k=k>>1; A=mul(A,A); } return p[1][1]; } void DP() { double ans=1; double tmp=T(x[1]-1); ans*=(1-tmp); for(int i=2;i<=n;++i) { if(x[i]==x[i-1]) continue; tmp=1-T(x[i]-x[i-1]-1); ans*=tmp; } printf("%.7f\n",ans<eps?0:ans); } int main() { while(~scanf("%d %lf",&n,&p)) { for(int i=1;i<=n;++i) scanf("%d",x+i); sort(x+1,x+1+n); DP(); } return 0; }
相关文章推荐
- Linux文件权限
- 链表删除节点(O(1))
- 广商14级软件工程分数:第十回合
- iOS-Network学习笔记(一)——NSURLRequest/NSURLResponse
- MySQL入门--创建数据表、显示指定数据库的数据表、查看数据表结构
- C++基础语法
- sdutoj 3379 ///数据结构实验之查找七:线性之哈希表 ti(哈希)
- 第六次c++作业
- MongoDB内部结构
- 实现strcat的功能【不可以使用strcat函数】
- iCheck状态不能改变
- launchOptions的key
- jenkins下使用git获取源码的配置方法
- windows配置git之二:首次clone
- JAVA学习之——ArrayList实现原理
- leetcode - Merge Sorted Array (run time beats 100.00% of cpp submissions.)
- Android之ListView使用方法-ArrayAdapter-SimpleAdapter-BaseAdapter
- malloc与free的配对使用的注意
- selinux禁用后系统无法正常启动的问题
- nyoj 73 比大小 【strcmp+if else】