http://poj.org/problem?id=3744

题意:直线上n个地雷,n<=10,范围在[1, 100000000],每一次有p的概率向前走一步,1-p的概率向前走两步,问安全通过所有地雷的概率(即走到最后一个地雷的后一格)

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; typedef double mtx[2][2];
void mul(mtx &a, mtx &b, mtx &c) {
static mtx t;
for(int i=0; i<2; ++i) for(int j=0; j<2; ++j) {
t[i][j]=0;
for(int k=0; k<2; ++k) t[i][j]=t[i][j]+a[i][k]*b[k][j];
}
memcpy(c, t, sizeof t);
}
double P;
bool flag;
void mpow(mtx &t, int n) {
if(n<0) { memset(t, 0, sizeof t); flag=1; return; }
if(flag) return;
static mtx a, x;
a[0][0]=P; a[0][1]=1;
a[1][0]=1-P; a[1][1]=0;
memset(x, 0, sizeof x);
x[0][0]=1; x[1][1]=1;
while(n) {
if(n&1) mul(x, a, x);
mul(a, a, a);
n>>=1;
}
mul(t, x, t);
}
mtx a, b;
int x[15], n;
int main() {
memset(b, 0, sizeof b);
while(~scanf("%d%lf", &n, &P)) {
for(int i=1; i<=n; ++i) scanf("%d", &x[i]);
sort(x+1, x+1+n);
b[0][0]=1-P;
flag=0;
memset(a, 0, sizeof a);
a[0][0]=1;
for(int i=1; i<=n; ++i) mpow(a, x[i]-x[i-1]-2), mul(a, b, a);
a[0][0]=(a[0][0]<0?-a[0][0]:a[0][0]);
printf("%.7f\n", a[0][0]);
}
return 0;
}

  

对于每一步状态$d[i]$,发现是由$d[i-1]$和$d[i-2]$转移得到的,那么由于事件$i-1$和事件$i-2$互斥,且根据概率的线性性质,得到:

$d[i]=p*d[i-1]+(1-p)*d[i-2]$

可是有地雷怎么办?分段= =然后讨论之= =

发现范围很大怎么办?转移一样上矩阵= =

反正最后得到矩阵积是这个样子的:

$$
\begin{bmatrix}
1 & 0 \\
\end{bmatrix}
\prod_{i=1}^{n}
\left(
\begin{bmatrix}
p & 1 \\
1-p & 0
\end{bmatrix}^{x_i-x_{i-1}-2}
\begin{bmatrix}
1-p & 0 \\
0 & 0
\end{bmatrix} \right)
$$

然后就行了= =

注意$x$数组不是有序的= =需要排一次序.....要不然就wa了一发QAQ还有poj的bits/stdc++不能用是什么鬼 = =

【POJ】3744 Scout YYF I的更多相关文章

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

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

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

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

  3. 【POJ】1704 Georgia and Bob(Staircase Nim)

    Description Georgia and Bob decide to play a self-invented game. They draw a row of grids on paper, ...

  4. 【POJ】1067 取石子游戏(博弈论)

    Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后 ...

  5. POJ 3744 Scout YYF I

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

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

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

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

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

  8. 【BZOJ】【1986】【USACO 2004 Dec】/【POJ】【2373】划区灌溉

    DP/单调队列优化 首先不考虑奶牛的喜欢区间,dp方程当然是比较显然的:$ f[i]=min(f[k])+1,i-2*b \leq k \leq i-2*a $  当然这里的$i$和$k$都是偶数啦~ ...

  9. 【POJ】【2104】区间第K大

    可持久化线段树 可持久化线段树是一种神奇的数据结构,它跟我们原来常用的线段树不同,它每次更新是不更改原来数据的,而是新开节点,维护它的历史版本,实现“可持久化”.(当然视情况也会有需要修改的时候) 可 ...

随机推荐

  1. 设计模式学习之外观模式(Facade,结构型模式)(8)

    1.什么是外观模式为子系统中的一组接口提供一个一致的界面,Facade模式定义了一个高层接口,这个接口使得这一子系统更加容易使用 2.为什么要使用外观模式在软件开发系统中,客户程序经常会与复杂系统的内 ...

  2. Delphi版本顺序

    1.02.03.04.05.06.07.08.0200520062007 现在应该又出新的版本了

  3. uploadify文件批量上传

    uploadify能够时间文件的批量上传,JS文件包下载地址,使用说明可以参考官网文档(http://www.uploadify.com/documentation/) 使用方法如下代码: $(&qu ...

  4. gcc【数学几何】

    GCC Time Limit: 1000MS Memory limit: 65536K 题目描述 The GNU Compiler Collection (usually shortened to G ...

  5. [LeetCode] Remove Element (三种解法)

    Given an array and a value, remove all instances of that value in place and return the new length. T ...

  6. golang 索引

    入门的基础路线 a Tour of GoEffective GoGo By Example 以上的三部分通读算是入门. 4个重要的组成部分 1. 基础知识2. 并发特性3. 异常处理4. 常用开源项目 ...

  7. How to install the zsh shell in Linux && how to set it as a default login shell

    Z shell’s (zsh) popularity has increased in the last years. I have not moved too zsh yet, but I am g ...

  8. 字节流、字符串、16进制字符串转换__Java(转)

    /** * @Package: * @ClassName:TypeConversion * @Description:字节流.字符串.16进制字符串转换 * @author:xk * @date:Ja ...

  9. ADT开发AndroidManifest.xml file missing错误

    一个错误“AndroidManifest.xml file missing”但helloworld目录下有此文件,几番google仍没能解决.想起曾经在网络上看到的一个修复project的办法,抱着死 ...

  10. DotNet 资源大全中文版,内容包括:编译器、压缩、应用框架、应用模板、加密、数据库、反编译、IDE、日志、风格指南等

    DotNet 资源大全中文版 我想很多程序员应该记得 GitHub 上有一个 Awesome - XXX 系列的资源整理.awesome-dotnet 是由 quozd 发起和维护.内容包括:编译器. ...