poj 3744 Scout YYF I (可能性DP+矩阵高速功率)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5062 | Accepted: 1370 |
Description
At first, YYF is at step one. For each step after that, YYF will walk one step with a probability of p, or jump two step with a probality of 1-p. Here is the task, given the place of each mine, please calculate the probality that YYF can
go through the "mine road" safely.
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
题意:人人从1開始走,p的概率走1步,1-p的概率走2步,求不踩雷的概率。地雷数N<=10,坐标范围在100000000内。
思路:人走到第i位置的概率为dp[i]=dp[i-1]*p+dp[i-2]*(1-p),因为数据范围较大,可有矩阵高速幂高速求出答案;
构造矩阵 | P , 1 | 即 | dp[n+1] , dp[n] | =
| dp[n] , dp[n-1] | * | P , 1 |
| 1-P , 0 | |
1-P , 0 |
然后将每一个a[i]+1到a[i+1]看做一段单独处理即可。
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn=100000050; struct node
{
double mat[2][2];
}A,B,C; double dp[maxn],p;
int n,a[15]; void input()
{
for(int i=0;i<n;i++) scanf("%d",&a[i]);
sort(a,a+n);
A.mat[0][0]=p,A.mat[0][1]=1.0;
A.mat[1][0]=1.0-p,A.mat[1][1]=0.0;
} node mul(node p,node q) // 矩阵相乘
{
node ans;
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
{
ans.mat[i][j]=0.0;
for(int k=0;k<2;k++)
ans.mat[i][j]+=p.mat[i][k]*q.mat[k][j];
}
return ans;
} node pow(node w,int num) // 高速幂
{
node ret=B,c=w;
int coun=num;
while(coun)
{
if(coun & 1) ret=mul(ret,c);
coun>>=1;
c=mul(c,c);
}
return ret;
} void solve()
{
int now=1;
double f1=0.0,f2=1.0;
for(int i=0;i<n;i++)
{
if(a[i]-now>=1)
{
node tmp=pow(A,a[i]-1-now);
f2=(f2*tmp.mat[0][0]+f1*tmp.mat[1][0])*(1.0-p);
f1=0.0;
now=a[i]+1;
}
else
{
printf("0.0000000\n");
return ;
}
}
printf("%.7f\n",f2);
} int main()
{
B.mat[0][0]=1.0,B.mat[0][1]=0.0;
B.mat[1][0]=0.0,B.mat[1][1]=1.0;
while(scanf("%d %lf",&n,&p)!=EOF)
{
input();
solve();
}
return 0;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
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 ...
- 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(递推求期望)
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 ...
- 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 题意: 现在有个屌丝要穿越一个雷区,雷分布在一条直线上,但是分布的范围很大,现在这个屌丝从1出发,p的概率往前走1步,1-p的概率往前走2 ...
- poj 3744 Scout YYF I (矩阵快速幂 优化 概率dp)
题目链接 分析&&题意来自 : http://www.cnblogs.com/kuangbin/archive/2012/10/02/2710586.html 题意: 在一条不满地雷的 ...
- POJ 3744 Scout YYF I (概率dp+矩阵快速幂)
题意: 一条路上,给出n地雷的位置,人起始位置在1,向前走一步的概率p,走两步的概率1-p,踩到地雷就死了,求安全通过这条路的概率. 分析: 如果不考虑地雷的情况,dp[i],表示到达i位置的概率,d ...
随机推荐
- python udp编程实例
与python tcp编程控制见 http://blog.csdn.net/aspnet_lyc/article/details/39854569 c++ udp/tcp 编程见 http://blo ...
- 在Windows基础上(硬盘)安装Linux操作系统(CentOS/RedHat)
注:该方法安装CentOS ,RedHat均没有问题,其它Linux操作系统,没有尝试过. 0.创建一个fat32的盘.我分了8G给这个盘,盘符为F.F盘以后的内存所有删除,作为未分配的内存.这个留用 ...
- ZOJ Monthly, October 2010 ABEFI
ZOJ 3406 Another Very Easy Task #include <cstdio> #include <cstring> const int N = 10000 ...
- YT新人之巅峰大决战04
Problem Description Eddy's interest is very extensive, recently he is interested in prime number. Ed ...
- Http与协议TCP协议简单易懂
于C#编写代码,很多时候会遇到Http协议或TCP合约,这里做一个简单的了解. TCP对应于该传送层协议,和HTTP对应于应用层协议,从本质上讲,两者是没有可比性.Http该协议是基于TCP之上的,当 ...
- 姿势体系结构的详细解释 -- C
我基本上总结出以下4部分: 1.问题的足迹大小. 2.字节对齐问题. 3.特别保留位0. 4.这种结构被存储在存储器中的位置. #include <stdio.h> #include &l ...
- 懵懵懂懂初识J2EE
一.定义 Java2平台包含:标准版.企业版.微缩版.当中J2SE是Java2的标准版,主要用 于桌面应用软件的编程:J2ME是微缩版,主要应用于嵌入式系统开发:还有这次学习的J2EE是Java2的企 ...
- 使用更清晰DebugLog开发和调试工具
在开发和应用的开发和调试过程中难免会发现故障的过程中.我相信很多做iOS开发程序员Xcode的debug调试功能大加关注. 但在这样做Android开发过程中,却不那么方便,虽然IDE也提供了debu ...
- 泛泰A820L (高通公司MSM8660 cpu) 3.4内核CM10.1(Android 4.2.2) 测试版第二版
欢迎关注泛泰非盈利专业第三方开发团队 VegaDevTeam (本team 由 syhost suky zhaochengw(z大) xuefy(大星星) tenfar(R大师) loogeo cr ...
- html不常见问题汇总
写html已经好长一段时间了,也遇到了不少问题,跟大家分享下 form是不可以嵌套的 说明:如果嵌套会有很多问题 但是可以并列 <html> <head> </head& ...