poj 3744 Scout YYF I(递推求期望)
poj 3744 Scout YYF I(递推求期望)
题意:给出n个坑,一个人可能以p的概率一步一步地走,或者以1-p的概率跳过前面一步,问这个人安全通过的概率
解法:
递推式:
对于每个坑,我们可以这么定义一个数组: d[i]代表它安全落在位置i的概率,在这个1到max(a[i])的范围中,只有那些坑是不安全的,答案只需求出所有不掉入坑的概率的连乘即可
矩阵:
由于数字范围巨大,需要对递推式进行矩阵连乘加速
| p 1-p | | d[i] | | d[i+1] |
| 1 0 | * | d[i-1] | =| d[i] |
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
typedef double ll;
const int MOD=1e9+7;
int tn;
struct Matrix
{
double m[111][111];
Matrix()
{
memset(m,0,sizeof(m));
}
friend Matrix operator*(Matrix a,Matrix b)
{
Matrix res;
double x;
for(int i=0; i<tn; i++)
{
for(int j=0; j<tn; j++)
{
x=0;
for(int k=0; k<tn; k++)
{
x=(x+(ll)a.m[i][k]*b.m[k][j]);
}
res.m[i][j]=x;
}
}
return res;
}
friend Matrix operator+(Matrix a,Matrix b)
{
Matrix res;
ll x;
for(int i=0; i<tn; i++)
{
for(int j=0; j<tn; j++)
{
res.m[i][j]=(a.m[i][j]+b.m[i][j]);
}
}
return res;
}
friend Matrix operator^(Matrix a,int b)
{
Matrix ans;
for(int i=0;i<tn;i++) ans.m[i][i]=1;
for(int i=b; i; i>>=1,a=a*a)
if(i&1)ans=ans*a;
return ans;
}
} T,F;
int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
void init(double p){
for(int i=0;i<tn;i++) F.m[i][i]=1;
T.m[0][0]=p;T.m[0][1]=1-p;
T.m[1][0]=1;
}
int main()
{
tn=2;
int n,a[20]={0};
double p;
while(cin>>n>>p){
init(p);
for(int i=1;i<=n;i++) cin>>a[i];
sort(a+1,a+1+n);
double ans=1;
if(a[1]==1){
printf("%.7lf\n",0);
continue;
}
Matrix tmp;
for(int i=1;i<=n;i++){
if(a[i]==a[i-1]){
continue;
}
if(a[i]-a[i-1]>2){
tmp=T^(a[i]-a[i-1]-2);
ans*=(tmp.m[0][0])*(1-p); //算出a[i]-1的概率,然后跳过a[i]的概率
}else{
tmp=T^(a[i]-a[i-1]-1);
ans*=1-(tmp.m[0][0]); //反向求出不落入a[i]的概率
}
}
printf("%.7lf\n",ans);
}
return 0;
}
poj 3744 Scout YYF I(递推求期望)的更多相关文章
- 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 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 题意: 现在有个屌丝要穿越一个雷区,雷分布在一条直线上,但是分布的范围很大,现在这个屌丝从1出发,p的概率往前走1步,1-p的概率往前走2 ...
- POJ 3744 Scout YYF I:概率dp
题目链接:http://poj.org/problem?id=3744 题意: 有n个地雷,位置为pos[i]. 在每个位置,你向前走一步的概率为p,向前走两步的概率为1-p. 你的初始位置为1. 问 ...
- 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 1 (概率DP+矩阵快速幂)
F - Scout YYF I Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- poj 2096 Collecting Bugs 【概率DP】【逆向递推求期望】
Collecting Bugs Time Limit: 10000MS Memory Limit: 64000K Total Submissions: 3523 Accepted: 1740 ...
- poj 3744 Scout YYF I (矩阵)
Description YYF -p. Here is the task, given the place of each mine, please calculate the probality t ...
随机推荐
- bzoj4619
4619: [Wf2016]Swap Space Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 129 Solved: 54[Submit][Sta ...
- P4971 断罪者
传送门 首先,不难看出可以给每个集合开一个可并堆,然后乱搞就可以了 主要的问题就是将罪恶值清零和减少罪恶值该怎么搞 罪恶值清零可以直接找到这个节点然后把值变为零,再把它的左右儿子分别并到这个节点所在的 ...
- 二分搜索poj106
Cable master Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 49944 Accepted: 10493 De ...
- BFS POJ 3126 Prime Path
题目传送门 /* 题意:从一个数到另外一个数,每次改变一个数字,且每次是素数 BFS:先预处理1000到9999的素数,简单BFS一下.我没输出Impossible都AC,数据有点弱 */ /**** ...
- 题解报告:poj 3468 A Simple Problem with Integers(线段树区间修改+lazy懒标记or树状数组)
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- Storm概念学习系列之核心概念(Tuple、Spout、Blot、Stream、Stream Grouping、Worker、Task、Executor、Topology)(博主推荐)
不多说,直接上干货! 以下都是非常重要的storm概念知识. (Tuple元组数据载体 .Spout数据源.Blot消息处理者.Stream消息流 和 Stream Grouping 消息流组.Wor ...
- RabbitMQ三:Rabbit的安装
本章文章,摘自 园友 章为忠 的文章,查找了很多资料,他总结的最细,最全面,我就直接拿过来了 他的原文 http://www.cnblogs.com/zhangweizhong/p/5689209.h ...
- EasyUI系列学习(八)-ProgressBar(进度条)
一.创建组件 1.class加载 <div class="easyui-progressbar"></div> 2.js加载 <div id=&quo ...
- CF811C Vladik and Memorable Trip
思路: 令dp[i]表示前i个的最大舒适度.则如果区间[j, i](1 < j <= i)满足条件,有如下转移:dp[i] = max(dp[i], dp[j - 1] + cur).其中 ...
- MYSQL 使用事务
直接上代码,ID是唯一标识 CREATE PROCEDURE PRO2() BEGIN DECLARE t_error INTEGER; DECLARE CONTINUE HANDLER FOR SQ ...