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? 感觉很不公平.大家算法都一致,因为我程序没有那 ...
随机推荐
- 团队项目UML用例图
团队项目UML用例图
- K米测评
第一部分 调研,评测 K米APP使用体验 从小五音不全,所以KTV这类地方我是能不去就不去的,更别说为了点个歌去下个APP,所以K米是我第一个点歌类APP.说说第一次上手的体验吧,APP颜值一般吧,U ...
- jboss wildfly 外网访问
在standalone.xml中: 找到下面三行,看到是要访问public(8080端口的)和management的interface,将interface中的127.0.0.1改为0.0.0.0即可 ...
- Handler,Thread,Looper之间关系小结
http://blog.csdn.net/sunxingzhesunjinbiao/article/details/6794840 (1) Looper类别用来为一个线程开启一个消息循环.默认情况下A ...
- 捉襟见肘之TableView的手势(删除、编辑等)与转场动画手势冲突
在使用PresentModel的方式进行转场动画时,出现UIPercentDrivenInteractiveTransition和 UITableView的自带手势冲突,问题需要总结,今天系统复习和总 ...
- 点亮第一个LED灯
1.代码: #include <reg52.h> //<reg51.h> 包含52单片机寄存器库sbit led = P1^0; //只有地址可以被8整除的 才可以用s ...
- eclipse安装springsource-tool-suite
到http://spring.io/tools/sts/all找到安装的eclipse对应的springsource-tool-suite版本,复制下载的网址 然后在eclipse的install n ...
- Java数据结构——解析算术表达式
- (新手向)基于Bootstrap的简单轮播图的手机实现
个人电脑里存了不少适合手机欣赏的图片,但是放手机里看是件很占据资源的事.鉴于家里有一台电脑经常开着,正好用来做家庭局域网共享,于是笔者就设想通过一种比较简单环保的思路.通过手机访问电脑内的图片. 首先 ...
- json_encode详解,转义
1.json_encod基本用法:数组转字符串 <?php $arr = array (,,,,); echo json_encode($arr); ?> 以上例程会输出: {,,,,} ...