题目大意:有n颗地雷分布在一条直线上,有个人的起始位置在1,他每次前进1步的概率为p,前进两步的概率为1-p,问他不碰到地雷的概率。

题目分析:定义状态dp(i)表示到了dp(i)的概率,则状态转移方程为dp(i)=p*dp(i-1)+(1-p)*dp(i-2)。要想安全通过x(i)到达x(i)+1只能由x(i)-1走两步,所以,可以将整条直线分成n段,那么从x(i-1)+1安全通过第 i 颗地雷概率为1-p(到达x(i))。坐标之间的距离又很大,所以可以用矩阵二分幂优化。

代码如下:

# include<iostream>
# include<cstdio>
# include<cstring>
# include<algorithm>
using namespace std; struct Matrix
{
int r,c;
double m[2][2];
}; int x[12];
double p; Matrix multiply(Matrix a,Matrix b)
{
Matrix res;
res.r=a.r;
res.c=b.c;
for(int i=0;i<res.r;++i){
for(int j=0;j<res.c;++j){
res.m[i][j]=0;
for(int k=0;k<a.c;++k)
res.m[i][j]+=a.m[i][k]*b.m[k][j];
}
}
return res;
} Matrix myPow(Matrix a,int n)
{
if(n==0){
a.m[0][0]=a.m[1][1]=1;
a.m[0][1]=a.m[1][0]=0;
return a;
}else{
Matrix res=myPow(a,n>>1);
res=multiply(res,res);
if(n&1)
res=multiply(res,a);
return res;
}
} double solve()
{
Matrix mat;
mat.r=mat.c=2;
mat.m[0][0]=p,mat.m[0][1]=1-p;
mat.m[1][0]=1,mat.m[1][1]=0;
sort(x+1,x+x[0]+1);
Matrix temp=myPow(mat,x[1]-1);
double ans=1-temp.m[0][0];///ans为安全通过第i颗地雷的概率。
for(int i=2;i<=x[0];++i){
if(x[i]==x[i-1]) continue;
temp=myPow(mat,x[i]-x[i-1]-1);
ans*=(1-temp.m[0][0]);
}
return ans;
} int main()
{
while(~scanf("%d",&x[0]))
{
scanf("%lf",&p);
for(int i=1;i<=x[0];++i)
scanf("%d",x+i);
printf("%.7lf\n",solve());
}
return 0;
}

  

POJ-3744 Scout YYF I (矩阵优化概率DP)的更多相关文章

  1. poj 3744 Scout YYF I (矩阵快速幂 优化 概率dp)

    题目链接 分析&&题意来自 : http://www.cnblogs.com/kuangbin/archive/2012/10/02/2710586.html 题意: 在一条不满地雷的 ...

  2. poj 3744 Scout YYF I (矩阵)

    Description YYF -p. Here is the task, given the place of each mine, please calculate the probality t ...

  3. poj 3744 Scout YYF I(递推求期望)

    poj 3744 Scout YYF I(递推求期望) 题链 题意:给出n个坑,一个人可能以p的概率一步一步地走,或者以1-p的概率跳过前面一步,问这个人安全通过的概率 解法: 递推式: 对于每个坑, ...

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

    题目链接: http://poj.org/problem?id=3744 Scout YYF I Time Limit: 1000MSMemory Limit: 65536K 问题描述 YYF is ...

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

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

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

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

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

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

  8. POJ3744 Scout YYF I (矩阵优化的概率DP)

    Scout YYF I YYF is a couragous scout. Now he is on a dangerous mission which is to penetrate into th ...

  9. POJ 3744 Scout YYF I

    分段的概率DP+矩阵快速幂                        Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Sub ...

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

    http://poj.org/problem?id=3744 题意: 现在有个屌丝要穿越一个雷区,雷分布在一条直线上,但是分布的范围很大,现在这个屌丝从1出发,p的概率往前走1步,1-p的概率往前走2 ...

随机推荐

  1. 如何安装nginx_lua_module模块,升级nginx,nginx-lua-fastdfs-GraphicsMagick动态生成缩略图,实现图片自动裁剪缩放

    如何安装nginx_lua_module模块,升级nginx,nginx-lua-fastdfs-GraphicsMagick动态生成缩略图,实现图片自动裁剪缩放 参考网站:nginx-lua-fas ...

  2. Linux(CentOS)下同时启动两个tomcat

    问题背景:在配置nginx时,配置了两个tomcat,tomcat01和tomcat02,改了tomcat02中server.xml的端口,可是还是启动不起来. 解决方法: 一.编辑环境变量: 1 v ...

  3. JS方法的使用

    $("#yingxiang_id li").each(function () { if ($(this).find(".div-relative").attr( ...

  4. Django框架----render函数和redirect函数的区别

    render函数和redirect函数的区别: render:只会返回页面内容,但是未发送第二次请求 redirect:发挥了第二次请求,url更新 具体实例说明 render: redirect:

  5. xml声明中的standalone属性

    晚上,在测试tinyxml的时候,发现其中声明了<?xml version="1.0" standalone="no" ?>,经查,其含义为stan ...

  6. 20145302张薇《课程设计》数据恢复——WinHex实践

    20145302张薇<课程设计>数据恢复--WinHex实践 实践内容 使用WinHex破损一个U盘 使用WinHex通过DBR备份数据来修复已损坏U盘 实践详细步骤 1.准备一个文件格式 ...

  7. 20145212罗天晨 逆向及Bof基础实践

    20145212罗天晨<网络对抗>第1周学习总结--逆向及Bof基础实践 逆向及Bof基础实践 一.实践目标 1.运行原本不可访问的代码片段 2.强行修改程序执行流 3.以及注入运行任意代 ...

  8. JS高级语法与JS选择器

    元素(element)和节点(node) childNode属性和children属性的区别 <!DOCTYPE html> <html lang="en"> ...

  9. Large Division(大数)题解

    Given two integers, a and b, you should check whether a is divisible by b or not. We know that an in ...

  10. BZOJ 1003: [ZJOI2006]物流运输(spfa+dp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1003 题意: 思路: 首先用spfa计算一下任意两天之内的最短路,dis[a][b]表示的就是在第a ...