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. SPICE-HTML5 鼠标指针BUG修复

    研究SPICE,找到了他们官方指定的HTML5客户端.下载下来用一下,发现跟网页VNC的水平差不多了.http://www.spice-space.org/page/Html5 服务端直接用QEMU起 ...

  2. 管道命令和xargs的区别(经典解释) 自己的总结

    1. 简介 之所以能用到这个命令,关键是由于很多命令不支 持|管道来传递参数,而日常工作中有有这个必要, 所以就有了xargs命令,例如:find /sbin -perm +700 |ls -l 这个 ...

  3. 关于JDBC访问存储过程的问题

    最近开发一个应用,需要调用一个入参为List的存储过程. 存储过程为: proc_test(p1 OUT Number, p2 IN Number, p3 IN TAB_CUSTOMER); 这个Li ...

  4. CAD交互绘制虚线(网页版)

    用户可以在CAD控件视区任意位置绘制直线. 主要用到函数说明: _DMxDrawX::DrawLine 绘制一个直线.详细说明如下: 参数 说明 DOUBLE dX1 直线的开始点x坐标 DOUBLE ...

  5. U盘制作安装盘后容量不能恢复的解决方案

    diskpartlist diskselect disk 0/1 --看具体U盘是0还是1clean

  6. VR技术在数据中心3D机房中的应用 (下)

    VR技术在数据中心3D机房中的应用 (下) 前面给大家简单科普了一下VR的硬件设备以及VR在各个领域的应用,是不是觉得非常高大上?千言万语概括成一句话,VR能给用户带来前所未有的沉浸感和交互方式,让人 ...

  7. JS常用字符串处理方法应用总结

    这篇文章主要总结了JS常用字符串的处理方法,需要的朋友可以参考下   1.indexOf()方法,从前往后查找字符串位置,大小写敏感,从0开始计数.同理,lastIndexOf() 方法从后往前,两个 ...

  8. URAL1966 Cipher Message 3

    题目描述 题解: 能看出来的是,每一组数只能改最后一位,所以前$7$位动不了. 所以$KMP$跑一跑. 重点在于最后一位怎么搞. 如果$KMP$跑完了还没找到合适的位置,直接$puts("N ...

  9. 【转载】form表单的两种提交方式,submit和button的用法

    1.当输入用户名和密码为空的时候,需要判断.这时候就用到了校验用户名和密码,这个需要在jsp的前端页面写:有两种方法,一种是用submit提交.一种是用button提交.方法一: 在jsp的前端页面的 ...

  10. [MVC][Shopping]Copy Will's Code

    数据模型规划(Models) //DisplayNameAttribute 指定属性的显示名称 [DisplayName("商品类别")] //DisplayColumnAttri ...