题意:

一条路上,给出n地雷的位置,人起始位置在1,向前走一步的概率p,走两步的概率1-p,踩到地雷就死了,求安全通过这条路的概率。

分析:

如果不考虑地雷的情况,dp[i],表示到达i位置的概率,dp[i]=dp[i-1]*p+dp[i-2]*(1-p),要想不踩地雷求出到达地雷位置的概率tmp,1-tmp就是不踩地雷的情况,问题又来了,位置最大是10^9,普通递推超时,想到了用矩阵优化。

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define read freopen("in.txt", "r", stdin)
const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod = ;
struct Mat{
double mat[][];
};
int pos[],n;
double p;
Mat mul(Mat a,Mat b){
Mat tmp;
for(int i=;i<;++i)
for(int j=;j<;++j){
tmp.mat[i][j]=;
for(int k=;k<;++k)
tmp.mat[i][j]+=a.mat[i][k]*b.mat[k][j];
}
return tmp;
}
Mat pow(Mat a,int n){
Mat tmp;
memset(tmp.mat,,sizeof(tmp.mat));
for(int i=;i<;++i)
tmp.mat[i][i]=;
while(n){
if(n&)tmp=mul(tmp,a);
a=mul(a,a);
n>>=;
}
return tmp;
}
void solve(){
Mat init,tmp;
init.mat[][]=p;
init.mat[][]=1.0-p;
init.mat[][]=;
init.mat[][]=;
sort(pos,pos+n);
tmp=pow(init,pos[]-);
double total=(-tmp.mat[][]);
for(int i=;i<n;++i){
if(pos[i]==pos[i-])continue;
tmp=pow(init,pos[i]-pos[i-]-);
total*=(-tmp.mat[][]);
}
printf("%.7lf\n",total);
}
int main()
{
while(~scanf("%d%lf",&n,&p)){
for(int i=;i<n;++i)
scanf("%d",&pos[i]);
solve();
}
return ;
}

POJ 3744 Scout YYF I (概率dp+矩阵快速幂)的更多相关文章

  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 1 (概率DP+矩阵快速幂)

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

  3. POJ3744 Scout YYF I 概率DP+矩阵快速幂

    http://poj.org/problem?id=3744 题意:一条路,起点为1,有概率p走一步,概率1-p跳过一格(不走中间格的走两步),有n个点不能走,问到达终点(即最后一个坏点后)不踩坏点的 ...

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

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

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

    题意: 一条路上有n个地雷,你从1开始走,单位时间内有p的概率走一步,1-p的概率走两步,问安全通过这条路的概率 解析: 很容易想到 dp[i] = p * dp[i-1] + (1 - p) * d ...

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

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

  7. 刷题总结—— Scout YYF I(poj3744 矩阵快速幂+概率dp)

    题目: Description YYF is a couragous scout. Now he is on a dangerous mission which is to penetrate int ...

  8. poj3744 (概率DP+矩阵快速幂)

    http://poj.org/problem?id=3744 题意:在一条铺满地雷的路上,你现在的起点在1处.在N个点处布有地雷,1<=N<=10.地雷点的坐标范围:[1,10000000 ...

  9. poj3744 Scout YYF I[概率dp+矩阵优化]

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

随机推荐

  1. sun.misc.unsafe类的使用

    http://blog.csdn.net/fenglibing/article/details/17138079

  2. JAVASCRIPT中的作用域和原型链,应该算是难点了,要好好多学学,练练

    今天初六,要上班啦... JAVASCRIPT,看来是丢不了了.. http://www.dengdeng90.com/wordpress/?p=241 http://www.cnblogs.com/ ...

  3. Java 反编译工具几枚(class转java)

    1.Java Decompiler Yet another fast Java decompiler. 下载地址:http://jd.benow.ca/#jd-gui-download 一款非常简洁的 ...

  4. python 编码问题(二)

    >>> a = '中文' >>> chardet.detect(a) {'confidence': 0.7525, 'encoding': 'utf-8'} > ...

  5. Ajax省市联动

    以JQuery为JS,写的Ajax省市联动. 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&q ...

  6. NSRange

    int main(int argc, const char * argv[]) {    @autoreleasepool {        // insert code here...       ...

  7. 如何查看linux是32位还是64位

    可以用命令“getconf LONG_BIT”查看,如果返回的结果是32则说明是32位,返回的结果是64则说明是64位. 此外还可以使用命令“uname -a”查看,输出的结果中,如果有x86_64就 ...

  8. Sqlserver数据库分页查询

    Sqlserver数据库分页查询一直是Sqlserver的短板,闲来无事,想出几种方法,假设有表ARTICLE,字段ID.YEAR...(其他省略),数据53210条(客户真实数据,量不大),分页查询 ...

  9. 协同滤波 Collaborative filtering 《推荐系统实践》 第二章

    利用用户行为数据 简介: 用户在网站上最简单存在形式就是日志. 原始日志(raw log)------>会话日志(session log)-->展示日志或点击日志 用户行一般分为两种: 1 ...

  10. 安卓开发44:解决 INSTALL_FAILED_UID_CHANGED 等问题

    apk无法卸载,一般可以下面的方法试一下: 1. 删除/data/app/(filename) 文件夹下的apk包 2. 删除/system/app/(filename) 文件夹下的apk包 3. 将 ...