POJ 3744 Scout YYF I:概率dp
题目链接:http://poj.org/problem?id=3744
题意:
有n个地雷,位置为pos[i]。
在每个位置,你向前走一步的概率为p,向前走两步的概率为1-p。
你的初始位置为1。
问你通过雷区的概率。
题解:
表示状态:
dp[i] = probability moving to i
表示走到i的概率
找出答案:
ans = dp[last_mine+1]
last_mine:最右边一颗雷的位置
如何转移:
dp[i] = dp[i-1] * p + dp[i-2] * (1-p)
if(i is a mine) dp[i] = 0
对于位置i,有可能是从i-1走来的,也有可能是从i-2走来的。
加法原理。
边界条件:
dp[1] = 1
初始位置为1。
优化:
矩阵快速幂。
对于某一段没有地雷的区间,是满足矩阵快速幂的(只用到递推式,dp不改为0)。
所以分段进行矩阵快速幂。
将雷区划分为n段:
1~pos[1], pos[1]+1~pos[2], pos[2]+1~pos[3]...
容斥原理:P(通过某一段雷区) = 1 - P(踩到最右边的雷)
乘法原理:P(通过总雷区) = ∏ P(通过每一段雷区)
矩阵格式:
初始矩阵:
特殊矩阵:
AC Code:
- // state expression:
- // dp[i] = probability moving to i
- //
- // find the answer:
- // dp[last mine + 1]
- //
- // transferring:
- // dp[i] = dp[i-1] * p + dp[i-2] * (1-p)
- //
- // boundary:
- // dp[1] = 1
- // others = 0
- //
- // optimization:
- // quick pow for matrix
- // from x to y
- // res = start * special ^ (y-x)
- // dp[i] = res.val[0][0]
- #include <iostream>
- #include <stdio.h>
- #include <string.h>
- #include <algorithm>
- #define MAX_N 15
- #define MAX_L 5
- using namespace std;
- struct Mat
- {
- int n;
- int m;
- double val[MAX_L][MAX_L];
- Mat()
- {
- n=;
- m=;
- memset(val,,sizeof(val));
- }
- void print_mat()
- {
- for(int i=;i<n;i++)
- {
- for(int j=;j<m;j++)
- {
- cout<<val[i][j]<<" ";
- }
- cout<<endl;
- }
- cout<<endl;
- }
- };
- int n;
- int pos[MAX_N];
- double p;
- double ans;
- Mat make_unit(int k)
- {
- Mat mat;
- mat.n=k;
- mat.m=k;
- for(int i=;i<k;i++)
- {
- mat.val[i][i]=;
- }
- return mat;
- }
- Mat make_start()
- {
- Mat mat;
- mat.n=;
- mat.m=;
- mat.val[][]=;
- mat.val[][]=;
- return mat;
- }
- Mat make_special()
- {
- Mat mat;
- mat.n=;
- mat.m=;
- mat.val[][]=;
- mat.val[][]=-p;
- mat.val[][]=;
- mat.val[][]=p;
- return mat;
- }
- Mat mul_mat(const Mat &a,const Mat &b)
- {
- Mat c;
- if(a.m!=b.n)
- {
- cout<<"Error: mul_mat"<<endl;
- return c;
- }
- c.n=a.n;
- c.m=b.m;
- for(int i=;i<a.n;i++)
- {
- for(int j=;j<b.m;j++)
- {
- for(int k=;k<a.m;k++)
- {
- c.val[i][j]+=a.val[i][k]*b.val[k][j];
- }
- }
- }
- return c;
- }
- Mat quick_pow_mat(Mat mat,long long k)
- {
- Mat ans;
- if(mat.n!=mat.m)
- {
- cout<<"Error: quick_pow_mat"<<endl;
- return ans;
- }
- ans=make_unit(mat.n);
- while(k)
- {
- if(k&)
- {
- ans=mul_mat(ans,mat);
- }
- mat=mul_mat(mat,mat);
- k>>=;
- }
- return ans;
- }
- void read()
- {
- pos[]=;
- for(int i=;i<=n;i++)
- {
- cin>>pos[i];
- }
- }
- void solve()
- {
- sort(pos+,pos++n);
- Mat special=make_special();
- ans=;
- for(int i=;i<=n;i++)
- {
- Mat start=make_start();
- Mat res=mul_mat(start,quick_pow_mat(special,pos[i]-pos[i-]));
- ans*=(-res.val[][]);
- }
- }
- void print()
- {
- printf("%.7f\n",ans);
- }
- int main()
- {
- while(cin>>n>>p)
- {
- read();
- solve();
- print();
- }
- }
POJ 3744 Scout YYF I:概率dp的更多相关文章
- POJ 3744 Scout YYF I 概率dp+矩阵快速幂
题目链接: http://poj.org/problem?id=3744 Scout YYF I Time Limit: 1000MSMemory Limit: 65536K 问题描述 YYF is ...
- poj 3744 Scout YYF I(概率dp,矩阵优化)
Scout YYF I Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5020 Accepted: 1355 Descr ...
- poj 3744 Scout YYF 1 (概率DP+矩阵快速幂)
F - Scout YYF I Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- poj 3744 Scout YYF I(递推求期望)
poj 3744 Scout YYF I(递推求期望) 题链 题意:给出n个坑,一个人可能以p的概率一步一步地走,或者以1-p的概率跳过前面一步,问这个人安全通过的概率 解法: 递推式: 对于每个坑, ...
- POJ 3744 Scout YYF I
分段的概率DP+矩阵快速幂 Scout YYF I Time Limit: 1000MS Memory Limit: 65536K Total Sub ...
- POJ-3744 Scout YYF I 概率DP
题目链接:http://poj.org/problem?id=3744 简单的概率DP,分段处理,遇到mine特殊处理.f[i]=f[i-1]*p+f[i-2]*(1-p),i!=w+1,w为mine ...
- poj3744 Scout YYF I[概率dp+矩阵优化]
Scout YYF I Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8598 Accepted: 2521 Descr ...
- POJ3744 Scout YYF I 概率DP+矩阵快速幂
http://poj.org/problem?id=3744 题意:一条路,起点为1,有概率p走一步,概率1-p跳过一格(不走中间格的走两步),有n个点不能走,问到达终点(即最后一个坏点后)不踩坏点的 ...
- POJ 3744 Scout YYF I(矩阵快速幂优化+概率dp)
http://poj.org/problem?id=3744 题意: 现在有个屌丝要穿越一个雷区,雷分布在一条直线上,但是分布的范围很大,现在这个屌丝从1出发,p的概率往前走1步,1-p的概率往前走2 ...
随机推荐
- CentOS下配置iptables防火墙 linux NAT(iptables)配置
CentOS下配置防火墙 配置nat转发服务CentOS下配置iptables防火墙 linux NAT(iptables)配置 CentOS下配置iptables 1,vim /etc/syscon ...
- jquery相冊图片来回选择
<!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <script sr ...
- HDU 4355 Party All the Time(三分|二分)
题意:n个人,都要去參加活动,每一个人都有所在位置xi和Wi,每一个人没走S km,就会产生S^3*Wi的"不舒适度",求在何位置举办活动才干使全部人的"不舒适度&quo ...
- 【Python】ModuleNotFoundError: No module named 'matplotlib.pyplot'
安装好matplotlib后,很激动的建立了一个文件夹matplotlib,并且在其下面建立了,mpl_squraes.py文件,代码编辑完成以后,点击运行,报错如下: 仔细分析了之后,发现是文件夹名 ...
- .net 网站登录
如何实现,按回车键,自动登录,在相应控件上添加onkeypress事件 function CheckCodePress(e){ var e = e||window.event if (e.keyCod ...
- UIView属性的动画
//标记着动画块的开始,第一个参数表示动画的名字,起到标识作用 [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDurat ...
- QTreeWidget 的用法
Qt QTreeWidget 新建一个Qt Widgets Application,拖拽一个Tree Widget 到 ui 界面上,最后实现的效果如下: 添加代码 //test.h //在头文件里添 ...
- request获取数据的几种方法
1.request.getparameter(); String value=request.getparameter("key"); 2.request.getParameter ...
- 多通道 移位寄存器 verilog
// Quartus II Verilog Template // Basic 64-stage shift register with multiple taps module basic_shif ...
- Windows 10 1703创意者更新官方ISO镜像大全
2017年04月07日 20:00 19867 次阅读 稿源:快科技 12 条评论 Windows 10 Creators Update创意者更新正式版已经发布,目前只能通过易生.MCT工具或者ISO ...