Scout YYF I
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10214   Accepted: 2980

Description

YYF is a couragous scout. Now he is on a dangerous mission which is to penetrate into the enemy's base. After overcoming a series difficulties, YYF is now at the start of enemy's famous "mine road". This is a very long road, on which there are numbers of mines. At first, YYF is at step one. For each step after that, YYF will walk one step with a probability of p, or jump two step with a probality of 1-p. Here is the task, given the place of each mine, please calculate the probality that YYF can go through the "mine road" safely.

Input

The input contains many test cases ended with EOF.
Each test case contains two lines.
The First line of each test case is N (1 ≤ N ≤ 10) and p (0.25 ≤ p ≤ 0.75) seperated by a single blank, standing for the number of mines and the probability to walk one step.
The Second line of each test case is N integer standing for the place of N mines. Each integer is in the range of [1, 100000000].

Output

For each test case, output the probabilty in a single line with the precision to 7 digits after the decimal point.

Sample Input

1 0.5
2
2 0.5
2 4

Sample Output

0.5000000
0.2500000

Source

    起始点在1,总共有n枚炸弹,位置位于x[i],(1<=x[i]<=1e8),每次有P的概率走一步,(1-P)的概率走两步,问安全走过所有炸弹的概率。
设f[i]表示安全到i点的概率,那么答案就是f[ max{x[i]} +1 ] ,因为每次只能走1/2,要想走过一个炸弹xi,只能在xi-1处走两步来实现(最后一次走)。
有f[i]=f[i-1]*P+f[i-2]*(1-P) ,但是N很大会超时。想到用矩阵优化,但是中间有炸弹的地方就不好处理了。对这个道路进行划分,根据炸弹的位置分为
[1,x[1]] , [x[1]+1,x[2]]......[x[n-1]+1,x[n]] ; 分成了N段,假设成功通过第i段的概率是Pi,那么答案就是∏Pi ,Pi就等价于从x[i-1]+1走到x[i]+1且最后一步走的大小是2
的概率,由于中间过程没有炸弹所以可以矩阵幂来算。
(f(n),f(n-1)) = (f(n-1),f(n-2)) * ((P,1),(1-P,0))
  注意判断特殊情况,两个炸弹相邻或者起点是炸弹那么输出0.0000000即可。
  

 #include<iostream>
#include<cstring>
#include<queue>
#include<cstdio>
#include<stack>
#include<set>
#include<map>
#include<cmath>
#include<ctime>
#include<time.h>
#include<algorithm>
using namespace std;
#define mp make_pair
#define pb push_back
#define debug puts("debug")
#define LL long long
#define pii pair<int,int>
#define eps 1e-12 struct matrix{
double a[][];
matrix& operator*(matrix &tmp){
matrix ans;
memset(ans.a,,sizeof(ans.a));
for(int i=;i<;++i){
for(int j=;j<;++j){
for(int k=;k<;++k){
ans.a[i][j]+=a[i][k]*tmp.a[k][j];
}
}
}
return ans;
}
}A,E;
double qpow(matrix H,int b){
matrix ans=E;
while(b){
if(b&) ans=ans*H;
H=H*H;
b>>=;
}
return ans.a[][];
}
int main()
{
int n,m,i,j,k,t;
int x[];
double P;
E.a[][]=E.a[][]=;
E.a[][]=E.a[][]=;
while(scanf("%d%lf",&n,&P)!=EOF){
A.a[][]=P;
A.a[][]=1.00-P;
A.a[][]=;
A.a[][]=;
for(i=;i<=n;++i) scanf("%d",x+i);
sort(x+,x++n);
bool ok=;
for(i=;i<=n;++i)
if(x[i]-x[i-]==) ok=;
if(ok==||x[]==){
printf("%.7f\n",0.0);
continue;
}
double ans=;
ans=ans*(-P)*qpow(A,x[]-);
for(i=;i<=n;++i){
if(x[i]==x[i-]) continue;
ans=ans*((double)-P)*qpow(A,x[i]-x[i-]-);
}
printf("%.7f\n",ans);
}
return ;
}
 

POJ-3744-概率dp+矩阵幂(分段)的更多相关文章

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

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

  2. poj 3744 概率dp+矩阵快速幂

    题意:在一条布满地雷的路上,你现在的起点在1处.在N个点处布有地雷,1<=N<=10.地雷点的坐标范围:[1,100000000]. 每次前进p的概率前进一步,1-p的概率前进1-p步.问 ...

  3. poj 3744 概率dp 快速幂 注意排序 难度:2

    /* Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5304   Accepted: 1455 De ...

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

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

  5. hdu 4576(简单概率dp | 矩阵优化)

    艰难的一道题,体现出菜菜的我... 首先,先吐槽下. 这题到底出题人是怎么想的,用普通概率dp水过??? 那为什么我概率dp写的稍微烂点就一直tle?  感觉很不公平.大家算法都一致,因为我程序没有那 ...

  6. [Poj3744]Scout YYF I (概率dp + 矩阵乘法)

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

  7. BZOJ1444[Jsoi2009]有趣的游戏——AC自动机+概率DP+矩阵乘法

    题目描述 输入 注意 是0<=P, n , l, m≤ 10. 输出 样例输入 input 1 3 2 2 1 2 1 2 AB BA AA input 2 3 4 2 1 2 1 2 AABA ...

  8. bzoj-4870-组合dp+矩阵幂

    4870: [Shoi2017]组合数问题 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 829  Solved: 446[Submit][Statu ...

  9. 【BZOJ1444】[Jsoi2009]有趣的游戏 AC自动机+概率DP+矩阵乘法

    [BZOJ1444][Jsoi2009]有趣的游戏 Description Input 注意 是0<=P Output Sample Input Sample Output HINT  30%的 ...

随机推荐

  1. could not execute menu item系统找不到指定的文件

    Wamp3.0.6 64bit,系统任务栏图标,左键,Apache菜单,httpd.conf,报错“could not execute menu item.....系统找不到指定的文件” 根据网上的搜 ...

  2. PL/SQL编程基础(二):变量的声明、赋值、(赋值、连接、关系、逻辑)运算符

    变量的声明.赋值.运算符 1.声明并使用变量 变量可以在声明时赋值,也可以先定义后赋值: 使用%TYPE与%ROWTYPE可以根据已有类型定义变量. PL/SQL是一种强类型的编程语言,所有的变量都必 ...

  3. Python开发【前端】:Ajax(一)

    Ajax Ajax即“Asynchronous Javascript And XML”(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术,AJAX = 异步 JavaScr ...

  4. 重定向、feed输出:控制台输出的内容存放到文件

    重定向.feed输出:控制台输出的内容存放到文件 1.重定向 os.system('wget -r -p -np -k http://www.baidu.com/ -o wget.log' ) 2.f ...

  5. 运行HBase应用开发程序产生异常,提示信息包含org.apache.hadoop.hbase.ipc.controller.ServerRpcControllerFactory的解决办法

    Using Spark's default log4j profile: org/apache/spark/log4j-defaults.properties Exception in thread ...

  6. 【译】3 ways to define a JavaScript class

    本文真没啥难点,我就是为了检验我英语水平退化了没哈哈虽然我英语本来就渣翻译起来也像大白话.将原文看了一遍也码完翻译了一遍差不多一个小时,其中批注部分是自己的理解如有疏漏或误解还请指出感激不尽呐,比如J ...

  7. 用Python实现的数据结构与算法:堆栈

    一.概述 堆栈(Stack)是一种后进先出(LIFO)的线性数据结构,对堆栈的插入和删除操作都只能在栈顶(top)进行. 二.ADT 堆栈ADT(抽象数据类型)一般提供以下接口: Stack() 创建 ...

  8. expdp&impdp备份恢复常用命令

    备份前准备 创建备份用户 create user backup identified by backup#2018 ; 授予导入导出角色 grant connect,resource to backu ...

  9. cocos代码研究(25)Widget子类PageView学习笔记

    基础理论 ListView控件是一个显示滚动项目列表的视图组. 列表项是通过使用addChild或insertDefaultItem插入到列表中的,继承自ScrollView. 代码实践 static ...

  10. 工具推荐. 在线unix, 在线python/perl脚本测试环境

    在线python, perl, javascript, Lisp, Ruby等  http://melpon.org/wandbox/ 正则表达式在线测试工具 http://tools.jb51.ne ...