POJ 3744 Scout YYF I (概率dp+矩阵快速幂)
题意:
一条路上,给出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+矩阵快速幂)的更多相关文章
- 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 1 (概率DP+矩阵快速幂)
F - Scout YYF I Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- POJ3744 Scout YYF I 概率DP+矩阵快速幂
http://poj.org/problem?id=3744 题意:一条路,起点为1,有概率p走一步,概率1-p跳过一格(不走中间格的走两步),有n个点不能走,问到达终点(即最后一个坏点后)不踩坏点的 ...
- poj 3744 Scout YYF I(概率dp,矩阵优化)
Scout YYF I Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5020 Accepted: 1355 Descr ...
- Scout YYF I POJ - 3744(概率dp + 矩阵快速幂)
题意: 一条路上有n个地雷,你从1开始走,单位时间内有p的概率走一步,1-p的概率走两步,问安全通过这条路的概率 解析: 很容易想到 dp[i] = p * dp[i-1] + (1 - p) * d ...
- poj4474 Scout YYF I(概率dp+矩阵快速幂)
Scout YYF I Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4100 Accepted: 1051 Descr ...
- 刷题总结—— Scout YYF I(poj3744 矩阵快速幂+概率dp)
题目: Description YYF is a couragous scout. Now he is on a dangerous mission which is to penetrate int ...
- poj3744 (概率DP+矩阵快速幂)
http://poj.org/problem?id=3744 题意:在一条铺满地雷的路上,你现在的起点在1处.在N个点处布有地雷,1<=N<=10.地雷点的坐标范围:[1,10000000 ...
- poj3744 Scout YYF I[概率dp+矩阵优化]
Scout YYF I Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8598 Accepted: 2521 Descr ...
随机推荐
- IOS委托设计模式(摘自IOS开发指南)
- POJ1008Maya Calendar
http://poj.org/problem?id=1008&lang=default&change=true 这个题倒是不难,就是麻烦一点,但是还WA了几次都是因为处理天数的时候没处 ...
- 李洪强iOS开发之OC[015]#pragma mark的使用
// // main.m // 14 - #pragma mark的使用 // // Created by vic fan on 16/7/10. // Copyright © 2016年 李 ...
- IOS开发--上传图片
IOS图片上传功能实现总结 IOS图片上传主要分两种方式实现,一个是将图片信息以表单的形式上传,一种是将图片以JSON的格式上传. 首先要讲的是以这两个方式上传的一个比较明显的区别就是HTTP Hea ...
- ipconfig
当使用ipconfig时不带任何参数选项,那么它为每个已经配置了的接口显示IP地址.子网掩码和缺省网关值. 如果你安装了虚拟机和无线网卡的话,它们的相关信息也会出现在这里.
- vc2005中没有classwizard这个命令
vc2005中没有classwizard这个命令了 2005下怎么添加鼠标事件 vc2005中没有classwizard这个命令了 取代classwizard 中的添加消息映射,添加类,等等的功能主要 ...
- Unigui有用的网址
http://www.cnblogs.com/ChinaEHR/tag/Delphi/
- AE CreateFeatureClass 创建shp. 删除shp. 向shp中添加要素
/// <summary> /// 创建多边形shp /// </summary> /// <param name="pPolygon">< ...
- iOS ARC下dealloc过程及.cxx_destruct的探究
前言 这次探索源自于自己一直以来对ARC的一个疑问,在MRC时代,经常写下面的代码: 1 2 3 4 5 6 7 8 9 - (void)dealloc { self.array = nil; ...
- 盘点PHP编程常见失误
概述:本文盘点PHP开发者在编码时,容易忽略或不注意引起的小失误与错误. 变量声明 如果在一条语句中声明一个变量,如下所示:$var='value';编译器首先会求出语句右半部分的值,恰恰正是语句的这 ...