POJ 3744
矩阵快速乘求概率,不难。但有注意的一点是,一定要注意地雷连着的情况,一旦出现两个雷相邻,就必定为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的更多相关文章
- poj 3744 Scout YYF I(递推求期望)
poj 3744 Scout YYF I(递推求期望) 题链 题意:给出n个坑,一个人可能以p的概率一步一步地走,或者以1-p的概率跳过前面一步,问这个人安全通过的概率 解法: 递推式: 对于每个坑, ...
- 矩阵快速幂+概率DP poj 3744
题意:在一条不满地雷的路上,你现在的起点在1处.在N个点处布有地雷,1<=N<=10.地雷点的坐标范围:[1,100000000]. 每次前进p的概率前进一步,1-p的概率前进1-p步.问 ...
- 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 ...
- POJ 3744 Scout YYF I(矩阵快速幂优化+概率dp)
http://poj.org/problem?id=3744 题意: 现在有个屌丝要穿越一个雷区,雷分布在一条直线上,但是分布的范围很大,现在这个屌丝从1出发,p的概率往前走1步,1-p的概率往前走2 ...
- 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
题目链接:http://poj.org/problem?id=3744 题意: 有n个地雷,位置为pos[i]. 在每个位置,你向前走一步的概率为p,向前走两步的概率为1-p. 你的初始位置为1. 问 ...
- 【POJ 3744】 Scout YYF I
[题目链接] http://poj.org/problem?id=3744 [算法] 概率DP + 矩阵乘法 [代码] #include <algorithm> #include < ...
- POJ 3744 Scout YYF I
分段的概率DP+矩阵快速幂 Scout YYF I Time Limit: 1000MS Memory Limit: 65536K Total Sub ...
- Scout YYF I(POJ 3744)
Scout YYF I Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5565 Accepted: 1553 Descr ...
- poj 3744 Scout YYF I (矩阵)
Description YYF -p. Here is the task, given the place of each mine, please calculate the probality t ...
随机推荐
- byte类型取值范围以及溢出具体解释
例1: public class test { public static void main(String[] args) { byte a = 127 ; a = (byte)(a+3) ; Sy ...
- jQuery中实现自己定义方法的扩展
JQuery包装器提供了大量的方法.能够再页面中直接使用.可是.没有 不论什么一个库能够满足全部的需求.所以.JQuery库提供了丰富的扩展功能 .以禁用一组表单元素为例.看看怎么简单有效的在JQue ...
- 全屏滚动实现:fullPage.js和fullPage
fullPage.js和fullPage都能实现全屏滚动,二者差别是:fullPage.js需依赖于JQuery库,而fullPage不须要依赖不论什么一个js库.能够单独使用. 一.fullPage ...
- (素数求解)I - Dirichlet's Theorem on Arithmetic Progressions(1.5.5)
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit cid=1006#sta ...
- USACO 2.1 Ordered Fractions
Ordered Fractions Consider the set of all reduced fractions between 0 and 1 inclusive with denominat ...
- SQL纯手写创建数据库到表内内容
建表啥的只点点鼠标,太外行了,不如来看看我的纯手写,让表从无到有一系列:还有存储过程临时表,不间断的重排序: 一:建数据库 create Database Show on primary ( name ...
- Android自定义日历控件(继承系统控件实现)
Android自定义日历控件(继承系统控件实现) 主要步骤 编写布局 继承LinearLayout设置子控件 设置数据 继承TextView实现有圆圈背景的TextView 添加Attribute 添 ...
- 性能优化实战-join与where条件执行顺序
昨天经历了一场非常痛苦的性能调优过程,但是收获也是刻骨铭心的,感觉对sql引擎的原理有了进一步认识. 问题起源于测试人员测一个多条件检索的性能时,发现按某个条件查询会特别慢.对应的sql语句简化为: ...
- ZBrush软件特性之Marker标记调控板
在ZBrush®中使用Marker标记调控板来记忆物体属性,因此能在任何时间回到标记并使用它给其他物体或改变物体作为参考点. ZBrush软件下载:http://pan.baidu.com/s/1sl ...
- ZBrush软件Texture纹理调控板
在zbrush4r8中对一个模型进行纹理制作在速度和易用性方面有诸多优势,通过Texture调控板创建.导入和输出纹理是及其方便且快捷的. Import (导入):导入Photoshop (.psd) ...