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. [入门帮助] Kafka入门经典教程

    问题导读 1.Kafka独特设计在什么地方?2.Kafka如何搭建及创建topic.发送消息.消费消息?3.如何书写Kafka程序?4.数据传输的事务定义有哪三种?5.Kafka判断一个节点是否活着有 ...

  2. J20170616-hm

    所以(ゆえん) 理由,原因,来由

  3. Python机器学习算法 — 决策树(Decision Tree)

    决策树 -- 简介         决策树(decision tree)一般都是自上而下的来生成的.每个决策或事件(即自然状态)都可能引出两个或多个事件,导致不同的结果,把这种决策分支画成图形很像一棵 ...

  4. bzoj 1666: [Usaco2006 Oct]Another Cow Number Game 奶牛的数字游戏【模拟】

    模拟 #include<iostream> #include<cstdio> using namespace std; int n,ans; int main() { scan ...

  5. 清北考前刷题day4下午好

    /* 辗转相除,每次计算多出现了几个数. */ #include<iostream> #include<cstdio> #include<cstring> #inc ...

  6. SP1043 GSS1 - Can you answer these queries I(猫树)

    给出了序列A[1],A[2],…,A[N]. (a[i]≤15007,1≤N≤50000).查询定义如下: 查询(x,y)=max{a[i]+a[i+1]+...+a[j]:x≤i≤j≤y}. 给定M ...

  7. Java经典算法之折半查找(二分法)

    采用二分法时,数据应是有序并且不重复的 与小时候玩的猜数游戏是一样的,会让你猜一个他所想的1~100之间的数,当你猜了一个数后,他会告诉你三种选择中的一个,比他想的大,或小,或猜中了,为了能用最少的次 ...

  8. 【杂文】5亿大质数表(5e8)

    [杂文]\(5\) 亿大质数表(\(5e8\)) 在写哈希,刷数论题时曾一度想要查质数,\(F**k\) 百度文库数据又少,翻页蛋疼,还不给复制,真的是服了. 于是在我闲的蛋疼的时候就搞了个质数表出来 ...

  9. laravel 模型 $table $guarded $hidden

     首先以App\User模型为例 1.$table属性 表名,对应数据库中的表名 2.guarded)属性 guarded表示在create()方法中不能被赋值的字段 3.$hidden属性 $hid ...

  10. C#上机作业及代码Question2

    第二题某文件名为"*.txt",其中*可能由若干个英文单词组成.将此文件名改为"*.dat",并且单词之间用下划线连接,例如: helloworld.txt,改 ...