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. 洛谷 P1969 积木大赛 —— 水题

    题目:https://www.luogu.org/problemnew/show/P1969 看每个高度和前面的关系即可. 代码如下: #include<iostream> #includ ...

  2. 从csv文件读取数据到二维vector

    void ReadDataFromCsv(std::string &filename, std::vector<std::vector<std::string> > & ...

  3. Centos7 配置防火墙 firewall

    一.firewall 1.从CentOS7开始,默认使用firewall来配置防火墙,没有安装iptables(旧版默认安装). 2.firewall的配置文件是以xml的格式,存储在 /usr/li ...

  4. 深入浅出Android makefile(2)--LOCAL_PATH(转载)

    转自:http://nfer-zhuang.iteye.com/blog/1752387 一.说明 上文我们对acp的Android.mk文件做了一个大致的描述,使得大家对Android.mk文件有了 ...

  5. RandomAccessFile使用场景及总结

    大家在学到Java中IO流的时候学到了各种流,对文件的各种操作.但是唯独可能对RandomAccessFile对象不会去过多的研究,那么这个到底有什么用呢? RandomAccessFile的唯一父类 ...

  6. Sql2008调试问题

    t-sql调试的时候,报以下错误 处理 1.要在服务器本机,不要远程 2.服务器名称用电脑名称(cmd->hostname),不要用IP,(local)或. 调试快捷键跟VS一样 F11逐语句 ...

  7. (3)左右值再探与decltype

    Decltype 类型指示符 “引用从来都作为其所指对象的同义词出现,只有用在decltype处是一个例外” 理解: Decltype和auto区别: 1.     auto是从表达式类型推断出要定义 ...

  8. Json-->Newton.Json.dll的使用方法

    Newton.Json.dll  for .NET2.0 实体1 public class Student    {        public string ID { get; set; }     ...

  9. ubuntu部署java环境

    一.安装java sudo add-apt-repository ppa:webupd8team/java sudo apt-get update sudo apt-get install oracl ...

  10. document.mozFullScreen

    非标准该特性是非标准的,请尽量不要在生产环境中使用它! 概述 返回一个布尔值,表明当前文档是否处于全屏模式. 语法 var isFullScreen = document.mozFullScreen ...