传送门:http://poj.org/problem?id=3744

令f(i)表示到i,安全的概率。则f(i) = f(i - 1) * p + f(i - 2) * (1 - p),若i位置有地雷,则f(i) = 0.很显然,要用矩阵来加速,矩阵也很好构造,懒得写了,百度图片搜“poj3744”就能看到。注意一下边界的处理。

#include <cstdio>
#include <algorithm>
#include <cstring> const int maxn = 15; int n, a[maxn], ima;
double p, mtx[2][2] = {{0.5, 0.5}, {1.0, 0.0}}, rt[2][2], tem[2][2], ans[2][2]; inline void mul(double a[2][2], double b[2][2]) {
tem[0][0] = a[0][0] * b[0][0] + a[0][1] * b[1][0];
tem[0][1] = a[0][0] * b[0][1] + a[0][1] * b[1][1];
tem[1][0] = a[1][0] * b[0][0] + a[1][1] * b[1][0];
tem[1][1] = a[1][0] * b[0][1] + a[1][1] * b[1][1];
memcpy((void*)b, (void*)tem, sizeof tem);
}
inline void poww(int mi) {
int i;
for (i = 31; mi >> i & 1 ^ 1; --i);
memcpy((void*)rt, (void*)mtx, sizeof mtx);
for (--i; ~i; --i) {
mul(rt, rt);
if (mi >> i & 1) {
mul(mtx, rt);
}
}
} int main(void) {
//freopen("in.txt", "r", stdin);
while (scanf("%d%lf", &n, &p) != EOF) {
mtx[0][0] = p;
mtx[0][1] = 1.0 - p;
for (int i = 1; i <= n; ++i) {
scanf("%d", a + i);
}
std::sort(a + 1, a + n + 1);
n = std::unique(a + 1, a + n + 1) - a - 1;\
for (int i = 1; i <= n; ++i) {
if (a[i] - a[i - 1] == 1) {
puts("0.0000000");
goto end;
}
}
ans[0][0] = 0.0;
ans[1][0] = 1.0 / (1.0 - p);
for (int i = 1; i <= n; ++i) {
poww(a[i] - a[i - 1] - 1);
mul(rt, ans);
ans[1][0] = ans[0][0];
ans[0][0] = 0.0;
}
printf("%.7f\n", ans[0][0] * p + ans[1][0] * (1.0 - p)); end:;
}
return 0;
}

  

[poj3744] Scout YYF I【概率dp 数学期望】的更多相关文章

  1. poj3744 Scout YYF I[概率dp+矩阵优化]

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

  2. POJ-3744 Scout YYF I 概率DP

    题目链接:http://poj.org/problem?id=3744 简单的概率DP,分段处理,遇到mine特殊处理.f[i]=f[i-1]*p+f[i-2]*(1-p),i!=w+1,w为mine ...

  3. POJ3744 Scout YYF I 概率DP+矩阵快速幂

    http://poj.org/problem?id=3744 题意:一条路,起点为1,有概率p走一步,概率1-p跳过一格(不走中间格的走两步),有n个点不能走,问到达终点(即最后一个坏点后)不踩坏点的 ...

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

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

  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 1 (概率DP+矩阵快速幂)

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

  7. bzoj1415 [Noi2005]聪聪和可可【概率dp 数学期望】

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1415 noip2016 D1T3,多么痛的领悟...看来要恶补一下与期望相关的东西了. 这是 ...

  8. CF 148D D. Bag of mice (概率DP||数学期望)

    The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests ...

  9. [poj2096] Collecting Bugs【概率dp 数学期望】

    传送门:http://poj.org/problem?id=2096 题面很长,大意就是说,有n种bug,s种系统,每一个bug只能属于n中bug中的一种,也只能属于s种系统中的一种.一天能找一个bu ...

随机推荐

  1. GO语言 --socket.io

    socket.io是对websocket的封装以及扩展, 可以跨平台使用, 具体可看官网.. GO语言实现: package main import ( "github.com/googol ...

  2. 【Nginx】ngx_event_core_module事件模块

    功能:创建连接池,决定使用哪些事件驱动机制,以及初始化将要使用的事件模块 该模块定义了ngx_event_core_commands数组处理其感兴趣的7个配置项 ngx_event_conf_t为该模 ...

  3. PDO防止SQL注入具体介绍

    <span style="font-size:18px;"><?php $dbh = new PDO("mysql:host=localhost; db ...

  4. Oracle APEX 4.2安装和配置

    A standard Oracle 11.2.0.3 database installation comes bundled with Application Express (APEX) 3.2.1 ...

  5. 在 Ubuntu 开启 GO 程序编译之旅

    本文将使用 putty 连接到一台阿里云 Ubuntu 16.04 服务器,在其上安装 go 语言的编译环境,旨在呈现从安装到"你好,世界!"涉及的方方面面,希望完成这个过程无须觅 ...

  6. Redis入门教程(二)— 基本数据类型

    阅读以下内容时,手边打开一个redis-cli一起输入,输入命令敲击回车键前在心中想好你的答案,如果结果不合你的预期,请分析原因,使极大地提高学习效率.如果没有条件,每个数据类型后有代码运行结果,供你 ...

  7. Bean Query 第一个版本号(1.0.0)已公布

    BeanQuery 是一个把对象转换为Map的Java工具库. 支持选择Bean中的一些属性.对结果进行排序和依照条件查询. 不只能够作用于顶层对象,也能够作用于子对象.很多其它具体的介绍能够看我的博 ...

  8. 2016/4/7 datatype:①json ②XML

    ①JSON 1,postjsonxml.php     json用循环方式处理传来的值  for(key in data) for(var i=0;i<data.length;i++){data ...

  9. 检查 统计 异常 通信 time_wait

    [root@hadoop1 conf]# netstat -n | grep -v 127.0.0.1 | grep -v :3306  | grep TIME_WAIT | sort -k 5n | ...

  10. TCP/IP具体解释--TCP/IP可靠的原理 滑动窗体 拥塞窗体

    TCP和UDP处在同一层---运输层,可是TCP和UDP最不同的地方是,TCP提供了一种可靠的数据传输服务,TCP是面向连接的,也就是说,利用TCP通信的两台主机首先要经历一个"拨打电话&q ...