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 ...
随机推荐
- nginx目录列表和目录访问权限设置
1.目录列表(directory listing) nginx让目录中的文件以列表的形式展现只需要一条指令 autoindex on; autoindex可以放在location中,只对当前locat ...
- softmax function in c++
#include <iostream> #include <vector> #include <cmath> #include <algorithm> ...
- bzoj 1611: [Usaco2008 Feb]Meteor Shower流星雨【BFS】
t记录每个格子最早被砸的时间,bfs(x,y,t)表示当前状态为(x,y)格子,时间为t.因为bfs,所以先搜到的t一定小于后搜到的,所以一个格子搜一次就行 #include<iostream& ...
- Linux学习笔记之Linux相关知识
[想成为某一方面的大神,没有捷径可走,只能不断的记录.练习.总结.coding……] notes:主要从网上摘录了一些关于Linux的历史以及一些相关内容,以便对Linux系统有一定的了解.这不但可以 ...
- Linux学习笔记之Linux系统启动过程
Linux系统的启动过程可以分为五个阶段: 内核的引导 运行init 系统初始化 建立终端 用户登录系统 1.内核引导: 当计算机打开电源后,首先进行BIOS开机自检,按照BIOS中设置的启动设备(一 ...
- scala学习笔记1: scala method
刚接触scala,做练习的时候碰到一个问题,顺便mark一下. 先看下面一段代码: def sum(args:Int*) = { var result = 0 for (arg <- args) ...
- LN : leetcode 3 Longest Substring Without Repeating Characters
lc 3 Longest Substring Without Repeating Characters 3 Longest Substring Without Repeating Characters ...
- Python,计算 ax^2 + bx + c = 0的根
1 #-*-coding : utf-8-*- 2 import math 3 4 def quadratic(a, b, c): 5 if not isinstance(a, (int, float ...
- CF822C Hacker, pack your bags!
思路: 对于一个区间[l, r],只需枚举所有满足r' < l并且二者duration之和为x的区间[l', r'],寻找其中二者cost之和最小的即可.于是可以开一个数组a[],a[i]表示所 ...
- python 模块-easygui.buttonbox
2018-03-0315:43:11 ): Yes_or_No = easygui.buttonbox("是否良品?", choices=['Yes', 'No', '退出']) ...