矩阵快速乘求概率,不难。但有注意的一点是,一定要注意地雷连着的情况,一旦出现两个雷相邻,就必定为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. Ubuntu16.04安装Caffe

    一.安装ccmake ccmake和cmake的功能是一样的,但它很方便设置编译前的一些参数,安装只需从官网下载压缩包,解压,最后将解压得到的文件夹中的bin文件夹的路径加入PATH环境变量中即可. ...

  2. rails Installer之后的调整rails.bat等文件

    rails Installer之后的调整rails.bat文件 出现系统找不到指定路径 学习了:http://www.jianshu.com/p/065355a731ee 修改rails.bat为 @ ...

  3. bram和dram差别

    选择distributed memory generator和block memorygenerator标准: Dram和bram差别: 1.bram 的输出须要时钟,dram在给出地址后既可输出数据 ...

  4. [HTML 5] aria-hidden

    You want to use aria-hidden to prevent screen reader to access some content should be hidden from us ...

  5. 【C++ Primer每日刷】之三 标准库 string 类型

    标准库 string 类型 string 类型支持长度可变的字符串.C++ 标准库将负责管理与存储字符相关的内存,以及提供各种实用的操作.标准库string 类型的目的就是满足对字符串的一般应用. 与 ...

  6. 【Android 应用开发】 ActionBar 样式具体解释 -- 样式 主题 简单介绍 Actionbar 的 icon logo 标题 菜单样式改动

    作者 : 万境绝尘 (octopus_truth@163.com) 转载请著名出处 : http://blog.csdn.net/shulianghan/article/details/3926916 ...

  7. UVA - 11722 Joining with Friend 几何概率

                            Joining with Friend You are going from Dhaka to Chittagong by train and you ...

  8. USACO 2.1 Hamming Codes

    Hamming CodesRob Kolstad Given N, B, and D: Find a set of N codewords (1 <= N <= 64), each of ...

  9. Regexp-Utils:银行卡号Luhm校验

    ylbtech-Regexp-Utils:银行卡号Luhm校验 1.返回顶部 1.方法 //Description: 银行卡号Luhm校验 //Luhm校验规则:16位银行卡号(19位通用): // ...

  10. 18.QT消息链筛选机制以及组合键

    mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> 5 #include <Q ...