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(递推求期望)的更多相关文章

  1. POJ 3744 Scout YYF I 概率dp+矩阵快速幂

    题目链接: http://poj.org/problem?id=3744 Scout YYF I Time Limit: 1000MSMemory Limit: 65536K 问题描述 YYF is ...

  2. poj 3744 Scout YYF I(概率dp,矩阵优化)

    Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5020   Accepted: 1355 Descr ...

  3. POJ 3744 Scout YYF I(矩阵快速幂优化+概率dp)

    http://poj.org/problem?id=3744 题意: 现在有个屌丝要穿越一个雷区,雷分布在一条直线上,但是分布的范围很大,现在这个屌丝从1出发,p的概率往前走1步,1-p的概率往前走2 ...

  4. POJ 3744 Scout YYF I:概率dp

    题目链接:http://poj.org/problem?id=3744 题意: 有n个地雷,位置为pos[i]. 在每个位置,你向前走一步的概率为p,向前走两步的概率为1-p. 你的初始位置为1. 问 ...

  5. POJ 3744 Scout YYF I

    分段的概率DP+矩阵快速幂                        Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Sub ...

  6. POJ 3744 Scout YYF I (概率dp+矩阵快速幂)

    题意: 一条路上,给出n地雷的位置,人起始位置在1,向前走一步的概率p,走两步的概率1-p,踩到地雷就死了,求安全通过这条路的概率. 分析: 如果不考虑地雷的情况,dp[i],表示到达i位置的概率,d ...

  7. poj 3744 Scout YYF 1 (概率DP+矩阵快速幂)

    F - Scout YYF I Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub ...

  8. poj 2096 Collecting Bugs 【概率DP】【逆向递推求期望】

    Collecting Bugs Time Limit: 10000MS   Memory Limit: 64000K Total Submissions: 3523   Accepted: 1740 ...

  9. poj 3744 Scout YYF I (矩阵)

    Description YYF -p. Here is the task, given the place of each mine, please calculate the probality t ...

随机推荐

  1. bzoj3884

    http://www.lydsy.com/JudgeOnline/problem.php?id=3884 拓展欧拉定理 http://blog.csdn.net/Pedro_Lee/article/d ...

  2. http报文和协议首部

    http报文和协议首部 http报文 3>报文格式 request 报文 <method> <request-URL> <version> <heade ...

  3. E20171226-hm

    stack n.栈 heap  n.堆 backtracking 回溯法,後戻り storage  n. 贮存; 贮藏; 储藏处,仓库; 贮存器,蓄电(瓶); ストレージ

  4. 17年day5

    /* 嗯,一切都快要结束了. 觉得不必要写太多,从day5开始就挺好吧. 记得去年这时候看到峰峰博客里的倒计时,心里还毫无波动,只是走的时候挺伤心. 现在轮到了我们. 峰峰我想你. 衷心祝zjk和my ...

  5. bzoj4720: [Noip2016]换教室(期望dp)

    4720: [Noip2016]换教室 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 1294  Solved: 698[Submit][Status ...

  6. 乐搏讲自动化测试- Python环境搭建(7)

    Python的下载和安装 Python可应用于多平台包括 Linux 和 Mac OS X.你可以通过终端窗口输入 "python" 命令来查看本地是否已经安装Python以及Py ...

  7. 无法收集统计信息,怎样优化SQL。

    特殊情况如下 客户的统计信息是固定的,没办法收集统计信息 . SQL profile 是最后考虑方案,因为同样写法sql 比较多,几十条. Parallle 并行客户一般不考虑接受,OLTP 系统. ...

  8. 279 Perfect Squares 完美平方数

    给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...) 使得他们的和等于 n.你需要让平方数的个数最少.比如 n = 12,返回 3 ,因为 12 = 4 + 4 + 4 : ...

  9. Discuz!伪静态原理分析

    伪静态在seo火热的时代,是每个站长都比较关注的问题,discuz!论坛如何伪静态,为什么伪静态失效了,为什么列表页无法实现伪静态,为什么有些页面不是伪静态呢?下面dz官方nxy105从两个角度入手为 ...

  10. 413 Request Entity Too Large报错处理

    修改nginx配置   这是最简单的一个做法,着报错原因是nginx不允许上传配置过大的文件,那么件把nginx的上传大小配置调高就好.    1.打开nginx主配置文件nginx.conf,一般在 ...