Scout YYF I

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9552   Accepted: 2793

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


 0.5

 0.5
 

Sample Output


0.5000000
0.2500000

分析:

概率dp是需要顺着推的,定义dp[i]表示到i的概率。当i有雷时,dp[i] = 0;

否则 dp[i] = dp[i - 1] * p + dp[i - 1] * (1 - p)

最后输出dp[dis[n] + 1]即行。

因为n很大,转移又固定,可以联想到矩乘 + 快速幂。于是就0MS过了

坑: 输入雷不一定有序,需要排序,精度输出需要%.7f  不能 %.7lf。

AC代码:

# include <iostream>
# include <cstdio>
# include <cstring>
# include <algorithm>
using namespace std;
int n;
int dis[];
struct fi{
double data[][];
fi(){
for(int i = ;i < ;i++){
for(int j = ;j < ;j++){
data[i][j] = ;
}
}
}
}A,T,O;
inline fi operator *(fi A,fi B){
fi t;
for(int i = ;i < ;i++){
for(int j = ;j < ;j++){
for(int k = ;k < ;k++){
t.data[i][j] += A.data[i][k] * B.data[k][j];
}
}
}
return t;
}
fi cmd(fi C,int k){
fi D = O;
while(k){
if(k & )D = D * C;
k >>= ;
C = C * C;
}
return D;
}
double p;
int main(){
for(int i = ;i < ;i++)O.data[i][i] = 1.0;
while(~scanf("%d %lf",&n,&p)){
T.data[][] = (1.0 - p);
T.data[][] = p;
T.data[][] = 1.0;
T.data[][] = 0.0;
for(int i = ;i < ;i++){
for(int j = ;j < ;j++){
A.data[i][j] = 0.0;
}
}
A.data[][] = 1.0;
for(int i = ;i <= n;i++){
scanf("%d",&dis[i]);
}
sort(dis + ,dis + n + );
for(int i = ;i <= n;i++){
A = A * cmd(T,dis[i] - dis[i - ] - );
A.data[][] = ;
A = A * T;
}
printf("%.7f\n",A.data[][]);
}
}

[Poj3744]Scout YYF I (概率dp + 矩阵乘法)的更多相关文章

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

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

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

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

  3. poj 3744 Scout YYF 1 (概率DP+矩阵快速幂)

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

  4. POJ 3744 Scout YYF I 概率dp+矩阵快速幂

    题目链接: http://poj.org/problem?id=3744 Scout YYF I Time Limit: 1000MSMemory Limit: 65536K 问题描述 YYF is ...

  5. POJ-3744 Scout YYF I 概率DP

    题目链接:http://poj.org/problem?id=3744 简单的概率DP,分段处理,遇到mine特殊处理.f[i]=f[i-1]*p+f[i-2]*(1-p),i!=w+1,w为mine ...

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

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

  7. BZOJ2553[BeiJing2011]禁忌——AC自动机+概率DP+矩阵乘法

    题目描述 Magic Land上的人们总是提起那个传说:他们的祖先John在那个东方岛屿帮助Koishi与其姐姐Satori最终战平.而后,Koishi恢复了读心的能力…… 如今,在John已经成为传 ...

  8. 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 ...

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

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

随机推荐

  1. SQL函数 Nullif

    函数 Nullif 功能: 如果两个函数都为空字符串则返回null 语法: Nullif(参数1,参数2) 一般该函数与函数coalesc一起使用 例: coalesce ( nullif ( [参数 ...

  2. Android从图库选择照片

    从机里取照片,开头用网上找的代码测试,导致类似下面这样的Crash:java.lang.RuntimeException: Failure delivering result java.lang.Se ...

  3. 如何实现Windows宿主系统和虚拟机ubuntu系统文件互相访问

    我的宿主操作系统是Windows 10,使用Oracle的Virtual Box安装了Ubuntu. 因为工作需要我经常得在两个系统之间互相拷贝一些数据,下面是具体步骤,可以实现Windows 10和 ...

  4. flask_SQLAlchemy 中常用的过滤和执行器

    常用的SQLAlchemy查询过滤器 过滤器 说明 filter() 把过滤器添加到原查询上,返回一个新查询 filter_by() 把等值过滤器添加到原查询上,返回一个新查询 limit 使用指定的 ...

  5. Vue构建命令

    node -v npm -v vue -V npm install vue (这个命令不行) 提示信息:+ vue@2.6.10 updated 1 package and audited 1 pac ...

  6. iOS代理模式

    iOS代理模式的简单理解:当一个对象无法直接获取到另一个对象的指针,又希望对那个变量进行一些操作时,可以使用代理模式. 代理主要由三部分组成: (1)协议:用来指定代理双方可以做什么,必须做什么. ( ...

  7. android打包需要的图标

    ldpi:mdpi:hdpi:xhdpi:xxhdpi=3:4:6:8:12 大小: 32x32.png 48 72 96 144

  8. python基础一 day5 知识点

    Unicode转化为gbk和utf-8 表现形式:str转化为bytes

  9. Archive for required library: 'D:/Program Files/Apache/maven-repository/dom4j/dom4j/1.6.1/dom4j-1.6.1.jar'

    今天导入一个项目工程,发现报错:Archive for required library: 'D:/Program Files/Apache/maven-repository/dom4j/dom4j/ ...

  10. HTML中获取input中单选按钮radio数据(性别例子)

    个人学习整理 1.编写HTML <!doctype html> <html> <head> <meta charset="utf-8"&g ...