[poj3744] Scout YYF I【概率dp 数学期望】
传送门: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 数学期望】的更多相关文章
- poj3744 Scout YYF I[概率dp+矩阵优化]
Scout YYF I Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8598 Accepted: 2521 Descr ...
- 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 ...
- POJ3744 Scout YYF I 概率DP+矩阵快速幂
http://poj.org/problem?id=3744 题意:一条路,起点为1,有概率p走一步,概率1-p跳过一格(不走中间格的走两步),有n个点不能走,问到达终点(即最后一个坏点后)不踩坏点的 ...
- poj 3744 Scout YYF I(概率dp,矩阵优化)
Scout YYF I Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5020 Accepted: 1355 Descr ...
- 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 1 (概率DP+矩阵快速幂)
F - Scout YYF I Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- bzoj1415 [Noi2005]聪聪和可可【概率dp 数学期望】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1415 noip2016 D1T3,多么痛的领悟...看来要恶补一下与期望相关的东西了. 这是 ...
- 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 ...
- [poj2096] Collecting Bugs【概率dp 数学期望】
传送门:http://poj.org/problem?id=2096 题面很长,大意就是说,有n种bug,s种系统,每一个bug只能属于n中bug中的一种,也只能属于s种系统中的一种.一天能找一个bu ...
随机推荐
- kvm虚拟化学习笔记(一)之kvm虚拟化环境安装
平时一直玩RHEL/CentOS/OEL系列的操作,玩虚拟化也是采这一类系统,kvm在RHEL6系列操作系统支持比较好,本文采用采用OEL6.3操作系统,网上所有文章都说KVM比xen简单,我怎么感觉 ...
- 【Nginx】Hello world程序
模块如何在运行中生效 配置文件中的location块决定了匹配某种URL的请求将会由相应的HTTP模块处理,因此,运行时HTTP框架会在接收完毕HTTP请求的头部后,将请求的URL与配置文件中的所有l ...
- linux 下shell脚本执行多个命令的方法
1.每个命令之间用;隔开说明:各命令的执行给果,不会影响其它命令的执行.换句话说,各个命令都会执行,但不保证每个命令都执行成功. 2.每个命令之间用&&隔开说明:若前面的命令执行成功, ...
- [wxWidgets]_[0基础]_[不常见但有用的类wxCmdLineParser]
场景: 1. 有时候须要构造命令行字符串传递給函数调用,比方CreateProcess,假设參数是动态的,那么就得使用类似std::vector<string>加入单个參数,之后拼接为一个 ...
- #Virtual hosts #Include conf/extra/httpd-vhosts.conf 开启就不能启动apache
#Virtual hosts#Include conf/extra/httpd-vhosts.conf我只要把其中任何一个开启就是吧#去掉就启动不了apache.怎么回事error.log是这样的ht ...
- ubuntu下打开eclipse·发现没有顶尖菜单项
在安装eclipse时,打开集成开发环境后没有菜单项. 网上些人说要写个shell脚步,感觉有点麻烦,其实就是少了一个环境变量 BUNTU_MENUPROXY. 在/etc/profile 里面新建这 ...
- JavaScript语句-流程控制语句
JavaScript定义了一组语句,语句通常用于执行一定的任务.语句可以很简单,也可以很复杂. 选择结构,可以在程序中创建交叉结构来指定程序流的可能方向.JavaScript中有四种选择结构: 1.单 ...
- Sql数据库查询语言
1.概述 Sql是一种面向数据库的结构化查询语言.是符合美国国家标准化组织ANSI的一种计算机标准语言. Sql具对数据库的操作有:增删改查.创建数据库.创建表.创建存储过程.创建视图等 RDBMS关 ...
- java垃圾回收机制的使用
public class Test { public static void main(String[] args) throws Exception { Book b=new Book(true); ...
- “Invalid configuration file. File "I:/My Virtual Machines/Windows XP english Professional/Windows XP Professional.vmx" was created by a VMware product
“Invalid configuration file. File "I:/My Virtual Machines/Windows XP english Professional/Windo ...