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

    lenum点特性 lenum点有如下特点 如果状态字是自定义的,只能在bacnet / ip的aln层使用 如果想在ms/tp层使用lenum的功能,就必须将system profile中bacnet ...

  2. 使用angularjs的$http.post异步提交数据时,服务器接收不了的问题

    一,在正常情况下,使用表单的post方法提交数据,默认请求头的Content-Type:application/x-www-form-urlencoded类型, 提交数据格式如下: 二,使用angul ...

  3. C专家编程之为什么C语言把数组形參当做指针:数组/指针实參

    #include<stdio.h> void print_array_test(char ca[]) { printf("ca : %s\n",ca); printf( ...

  4. HDU 4196

    很容易由算术基本定理知道,完全平方数就是所有质因子指数为偶数的数.而求得N以下的质因子,可由前两篇的公式知,由N!与p的关系求得.对于指数为p的,用N!除去就可以,因为p必定属于N以内,且无重复. 至 ...

  5. 在Eclipse中创建Maven多模块项目

    在Eclipse中创建Maven多模块项目1,创建多模块项目选择File>New>Project,打开New Project窗口,选择Maven>Maven Project,选择下一 ...

  6. R语言的帮助使用和图形功能简单介绍

    R语言的帮助使用和图形功能简单介绍 R语言帮助,在Windows桌面下,有很多种.最长使用的是在命令行下help() > help.start() 会在浏览器中,打开帮助的主页 watermar ...

  7. Mac OS下PHP开发环境的搭建——基于XAMPP和IntelliJ IDEA

    简单记录一下在MacOS下,搭建PHP的开发环境吧.其实,从本质上来说,Mac对于PHP的支持还是很好的,默认带了PHP和Apache,但是由于前期对系统本身不熟悉,所以还是略微走了一些弯路--也就是 ...

  8. hdoj--1018--Big Number(简单数学)

    Big Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...

  9. Spark SQL 编程API入门系列之Spark SQL的作用与使用方式

    不多说,直接上干货! Spark程序中使用SparkSQL 轻松读取数据并使用SQL 查询,同时还能把这一过程和普通的Python/Java/Scala 程序代码结合在一起. CLI---Spark ...

  10. MVC、控件、一般处理程序中的session and cookie

    Mvc中: session: if (!string .IsNullOrEmpty(find)) //设置 Session["oip"] = "无锡"; Vie ...