矩阵快速乘求概率,不难。但有注意的一点是,一定要注意地雷连着的情况,一旦出现两个雷相邻,就必定为0了。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std; int pos[15]; struct matrix{
double m[3][3];
};
matrix per,pt,ps; matrix operator *(matrix a,matrix b){
matrix c;
for(int i=1;i<=2;i++){
for(int j=1;j<=2;j++){
c.m[i][j]=0;
for(int k=1;k<=2;k++)
c.m[i][j]+=(a.m[i][k]*b.m[k][j]);
}
}
return c;
} matrix quick(int k){
matrix ans=per,p=pt;
while(k){
if(k&1) ans=ans*p;
k>>=1;
p=p*p;
}
return ans;
} int main(){
int n; double p;
while(scanf("%d%lf",&n,&p)!=EOF){
memset(per.m,0,sizeof(per.m));
per.m[1][1]=per.m[2][2]=1;
memset(pt.m,0,sizeof(pt.m));
pt.m[1][1]=p;pt.m[2][1]=1; pt.m[1][2]=1-p;
memset(ps.m,0,sizeof(ps.m));
ps.m[1][1]=p; ps.m[2][1]=1;
for(int i=1;i<=n;i++)
scanf("%d",&pos[i]);
sort(pos+1,pos+n+1);
if(pos[1]==1){
printf("%.7lf\n",0);
continue;
}
pos[0]=0;
matrix tmp;
double ans=1;
for(int i=1;i<=n;i++){
int g=pos[i]-1-pos[i-1]-1;
if(g>0){
tmp=quick(g-1);
tmp=tmp*ps;
ans=ans*tmp.m[1][1];
}
if(pos[i+1]==pos[i]+1){
ans=0;
break;
}
ans*=(1-p);
}
printf("%.7lf\n",ans);
}
return 0;
}

  

POJ 3744的更多相关文章

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

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

  2. 矩阵快速幂+概率DP poj 3744

    题意:在一条不满地雷的路上,你现在的起点在1处.在N个点处布有地雷,1<=N<=10.地雷点的坐标范围:[1,100000000]. 每次前进p的概率前进一步,1-p的概率前进1-p步.问 ...

  3. poj 3744 Scout (Another) YYF I - 概率与期望 - 动态规划 - 矩阵快速幂

      (Another) YYF is a couragous scout. Now he is on a dangerous mission which is to penetrate into th ...

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

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

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

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

  6. POJ 3744 Scout YYF I:概率dp

    题目链接:http://poj.org/problem?id=3744 题意: 有n个地雷,位置为pos[i]. 在每个位置,你向前走一步的概率为p,向前走两步的概率为1-p. 你的初始位置为1. 问 ...

  7. 【POJ 3744】 Scout YYF I

    [题目链接] http://poj.org/problem?id=3744 [算法] 概率DP + 矩阵乘法 [代码] #include <algorithm> #include < ...

  8. POJ 3744 Scout YYF I

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

  9. Scout YYF I(POJ 3744)

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

  10. poj 3744 Scout YYF I (矩阵)

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

随机推荐

  1. USACO holstein 超时代码

    /* ID:kevin_s1 PROG:holstein LANG:C++ */第八组数据跪了.半天都不出结果 #include <iostream> #include <cstdi ...

  2. j-link修复 write flash 一直无法点击

    write flash 一直无法点击 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMTcyNDI5NTc=/font/5a6L5L2T/fonts ...

  3. [IOI 1999] 花店橱窗布置

    [题目链接] https://www.luogu.org/problemnew/show/P1854v [算法] f[i][j]表示放了前i束花,第i束花放在第j个花瓶中,所能获得的最大美学值 由于要 ...

  4. docker中出现容器未运行无法进入容器的操作

  5. 关于jquery文件上传插件 uploadify 3.1的使用

    要使用uplaodify3.1,自然要下载相应的包,下载地址http://www.uploadify.com/download/,这里有两种包,一个是基于flash,免费的,一个是基于html5,需要 ...

  6. ROS-OPENCV

    前言:opencv是一个开源的跨平台计算机视觉库. 前提:1.已下载并编译了相关功能包集,如还未下载,可通过git下载:https://github.com/huchunxu/ros_explorin ...

  7. 微软认证Hyper-V咨询工程师认证课程

    课程链接:http://www.microsoftvirtualacademy.com/colleges/hyper-V-Certificate STEP 1:完成课程链接内的认证课程. STEP 2 ...

  8. React组件化开发

    环境搭建: 1.安装node.js 2.安装cnpm  # npm install -g cnpm --registry=https://registry.npm.taobao.org 3.全局安装c ...

  9. 页面加载通过javascript来修改控件属性

    function changeFormElementStatus(tagNames) {            var tagNameArr = tagNames.split("," ...

  10. CDR 2017压感笔和压感设备该怎么设置使用?

    您可以通过CorelDRAW 2017 中的以下工具来运用压感笔.笔或其他设备的压力:艺术笔(表达模式).橡皮擦.涂抹.转动.吸引.排斥.粗糙和弄脏.此外,您还可以通过艺术笔(表达模式)工具.橡皮擦. ...