[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 ...
随机推荐
- Puppet基于Master/Agent模式实现LNMP平台部署
前言 随着IT行业的迅猛发展,传统的运维方式靠大量人力比较吃力,运维人员面对日益增长的服务器和运维工作,不得不把很多重复的.繁琐的工作利用自动化处理.前期我们介绍了运维自动化工具ansible的简单应 ...
- CentOS 6.x Radius
CentOS 6.x Radius 一. 实现环境: 1.系统:CentOS release 6.6 (Final) 2.需要软件包: 1) freeradius-2.1.12-6.e16.x ...
- 蚂蜂窝VS穷游最世界-自由行类App分析
很多其它内容请关注博客: http://www.china10s.com/blog/? p=150 一.产品概述 体验环境: 机型:iPhone 6 型号:64G版 系统:iOS9.2 蚂蜂窝APP版 ...
- 在DIV中自己主动换行
word-break:break-all和word-wrap:break-word都是能使其容器如DIV的内容自己主动换行. 它们的差别就在于: 1,word-break:break-all 比如di ...
- payload和formData有什么不同?
最近做项目的时候,在通过post请求向服务端发送数据的时候,请求失败了.错误信息如下: 返回的400(bad request)错误,说明客户端这边发送的请求是有问题的. 和通过jquery中的ajax ...
- antd 表单验证
antd form 自带方法 /** * 获取 form 自带方法 * getFieldDecorator * getFieldsError * getFieldError * isFieldTou ...
- springMVC4(16)拦截器解析与登陆拦截模拟
在SpringMVC中,我们会常常使用到拦截器,尽管SpringAOP也能帮我们实现强大的拦截器功能,但在Web资源供给上.却没有SpringMVC来得方便快捷. 使用SpringMVC拦截器的核心应 ...
- 报错:Binary XML file line #7: Error inflating class android.support.v7.widget.RecyclerView
近期学习RecyclerView,使用eclipse引用RecyclerView.编写完demo后编译没有问题,一执行就挂掉,错误例如以下: 07-22 23:05:34.553: D/Android ...
- 2016/3/10 PHP (超文本预处理器) 是什么?
PHP(外文名:PHP: Hypertext Preprocessor,中文名:“超文本预处理器”)是一种通用开源脚本语言.语法吸收了C语言.Java和Perl的特点,利于学习,使用广泛,主要适用于W ...
- P1439 排列LCS问题
P1439 排列LCS问题 56通过 220提交 题目提供者yeszy 标签二分动态规划 难度普及+/提高 提交该题 讨论 题解 记录 最新讨论 暂时没有讨论 题目描述 给出1-n的两个排列P1和P2 ...