poj 3744 Scout YYF I(概率dp,矩阵优化)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5020 | Accepted: 1355 |
Description
Input
Each test case contains two lines.
The First line of each test case is N (1 ≤ N ≤ 10) and p (0.25 ≤ p ≤ 0.75) seperated by a single blank, standing for the number of mines and the probability to walk one step.
The Second line of each test case is N integer standing for the place of N mines. Each integer is in the range of [1, 100000000].
Output
Sample Input
- 1 0.5
- 2
- 2 0.5
- 2 4
Sample Output
- 0.5000000
- 0.2500000
具体来说,对于某一段长度为nk的线段k,设a是线段k的开头,b是线段k的结尾,nk=a-b-1,
到达a的概率设为1,到达a+1概率是p,到达a+2的概率就是Pa*(1-p)+Pa+1*p,这样就可以递推了。
由于Pam=Pam-1*p+Pam-2*(1-p),即推的公式都是一样的,可以用矩阵乘法+快速幂来做。
问题的解可以看做Pn1*(1-p)*Pn2*(1-p)*....Pnn*(1-p),因为最后一个陷阱要跳过去才安全。
- #include<iostream>
- #include<cstdio>
- #include<cstring>
- #include<vector>
- #include<algorithm>
- #include<cmath>
- #define M(a,b) memset(a,b,sizeof(a))
- typedef long long LL;
- using namespace std;
- int n;
- double p;
- int num[];
- struct matrix
- {
- double mat[][];
- void init()
- {
- mat[][] = p;
- mat[][] = -p;
- mat[][] = ;
- mat[][] = ;
- }
- };
- matrix mamul(matrix aa,matrix bb)
- {
- matrix c;
- for(int i = ;i<;i++)
- {
- for(int j = ;j<;j++)
- {
- c.mat[i][j] = ;
- for(int k = ;k<;k++)
- c.mat[i][j]+=(aa.mat[i][k]*bb.mat[k][j]);
- }
- }
- return c;
- }
- matrix mul(matrix s, int k)
- {
- matrix ans;
- ans.init();
- while(k>=)
- {
- if(k&)
- ans = mamul(ans,s);
- k = k>>;
- s = mamul(s,s);
- }
- return ans;
- }
- int main()
- {
- while(scanf("%d%lf",&n,&p)==)
- {
- for(int i = ;i<=n;i++)
- scanf("%d",&num[i]);
- sort(num+,num+n+);
- num[] = ;
- if(num[]==) {puts("0.0000000"); continue;}
- matrix ans;
- ans.init();
- double out = ;
- matrix tem;
- tem.mat[][] = (-p)+p*p;
- tem.mat[][] = p;
- tem.mat[][] = p;
- tem.mat[][] = ;
- int flag = ;
- for(int i = ;i<=n;i++)
- {
- if(num[i]-num[i-]<)
- {puts("0.0000000"); flag = ; break;}
- if(num[i]-num[i-]-==)
- out*=tem.mat[][];
- else
- {
- ans.init();
- ans = mul(ans,num[i]-num[i-]-);
- matrix c;
- for(int i = ;i<;i++)
- {
- for(int j = ;j<;j++)
- {
- c.mat[i][j] = ;
- for(int k = ;k<;k++)
- c.mat[i][j]+=(ans.mat[i][k]*tem.mat[k][j]);
- }
- }
- //cout<<c.mat[1][1]<<'!'<<endl;
- out*=c.mat[][];
- }
- out*=(-p);//cout<<out<<endl;
- tem.mat[][] = (-p)+p*p;
- tem.mat[][] = p;
- tem.mat[][] = p;
- tem.mat[][] = ;
- }
- if(!flag)
- printf("%.7f\n",out);
- }
- return ;
- }
poj 3744 Scout YYF I(概率dp,矩阵优化)的更多相关文章
- 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 ...
- poj3744 Scout YYF I[概率dp+矩阵优化]
Scout YYF I Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8598 Accepted: 2521 Descr ...
- POJ3744 Scout YYF I 概率DP+矩阵快速幂
http://poj.org/problem?id=3744 题意:一条路,起点为1,有概率p走一步,概率1-p跳过一格(不走中间格的走两步),有n个点不能走,问到达终点(即最后一个坏点后)不踩坏点的 ...
- poj 3744 Scout YYF I(递推求期望)
poj 3744 Scout YYF I(递推求期望) 题链 题意:给出n个坑,一个人可能以p的概率一步一步地走,或者以1-p的概率跳过前面一步,问这个人安全通过的概率 解法: 递推式: 对于每个坑, ...
- POJ 3744 Scout YYF I
分段的概率DP+矩阵快速幂 Scout YYF I Time Limit: 1000MS Memory Limit: 65536K Total Sub ...
- POJ 3744 Scout YYF I (概率dp+矩阵快速幂)
题意: 一条路上,给出n地雷的位置,人起始位置在1,向前走一步的概率p,走两步的概率1-p,踩到地雷就死了,求安全通过这条路的概率. 分析: 如果不考虑地雷的情况,dp[i],表示到达i位置的概率,d ...
- 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 ...
- hdu 4576(简单概率dp | 矩阵优化)
艰难的一道题,体现出菜菜的我... 首先,先吐槽下. 这题到底出题人是怎么想的,用普通概率dp水过??? 那为什么我概率dp写的稍微烂点就一直tle? 感觉很不公平.大家算法都一致,因为我程序没有那 ...
随机推荐
- c#自适应窗体的实现
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...
- GridView控件RowDataBound事件的一个实例
实现点击两个按钮,跳转到同一个界面,HyperLink显示不同的东西,主要代码段如下 前台代码: <asp:TemplateField HeaderText="操作"> ...
- 如何判断ios设备中是否安装了某款应用
URL Schemes关键字研究一下即可 常见得URL Schemes见http://www.cnblogs.com/huangzs/p/4491286.html if ([[UIApplicatio ...
- 高可用与负载均衡(8)之聊聊 LVS重要参数和优化以及监控
preface 在明白LVS-DR模式的部署之后,我们看看LVS的几个重要参数: 如有问题,请联系我18500777133@sina.cn [root@localhost ~]# ls /proc/s ...
- 真机调试之android手机+chrome
真机调试之android手机+chrome 虽然chrome上的移动设备模拟器很强大,但是在真机运行的时候,总会遇到一些小问题,这时就需要使用真机调试了. 第一步:准备一台android手机,并在手机 ...
- MVC过滤器之 OnActionExcuted
Controller里 [SendMessage] public Action SendSmsMessage() { var resultExtendInfo=new ResultExtendInfo ...
- 利用百度开发者中心的api实现地图及周边的搜索
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- Android项目结构 以及体系结构
学习Android平台的人一般对Android的平台的应该有点认识 其它的就不多讲了 Android项目一般由以下几个部分构成 以上是一个简单的Android项目结构目录图 1. src 主要是 源 ...
- MySQL学习笔记——安装及配置环境
1.安装的版本为mysql-5.6.24-win32.1432006610压缩版 查看教程http://jingyan.baidu.com/article/f3ad7d0ffc061a09c3345b ...
- Cache-Aside Pattern解析
使用这种模式,可以帮助我们维护Cache中的数据. 使用Cache容易遇到的问题: 使用缓存,主要是为了将一些重复访问的数据存到缓存,开发者希望缓存中的数据和数据源中的保持一致,这就需要程序中有相应的 ...