BNU 4346 Scout YYF I
A. Scout YYF I
64-bit integer IO format: %lld Java class name: Main
Input
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
Sample Input
1 0.5
2
2 0.5
2 4
Sample Output
0.5000000
0.2500000
解题:动态规划+矩阵快速幂。 dp[i] = dp[i-1]*p+dp[i-2]*(1-p);表示抵达第i个格子的概率。转化成矩阵相乘
| p 1-p | | dp[i-1] | | dp[i] |
| 1 0 | | dp[i-2] | = | dp[i-1] |
分别求出这个雷区到上一个雷区踩雷的概率,然后求对立事件,就是等于成功越过第一个雷区,成功越过第二个雷区,成功越过第三个雷区。。。。等一系列步骤而完成,根据乘法法则。相乘呗。
$$ \begin{bmatrix} dp[i]\\ dp[i-1] \\ \end{bmatrix}\quad = \begin{bmatrix}p & 1-p\\ 1 & 0 \\ \end{bmatrix} \times \begin{bmatrix} dp[i-1] \\ dp[i-2] \\ \end{bmatrix}$$
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
double p;
int n,d[];
struct Matrix{
double m[][];
};
Matrix multi(Matrix a,Matrix b){
Matrix c;
for(int i = ; i < ; i++){
for(int j = ; j < ; j++){
c.m[i][j] = 0.0;
for(int k = ; k < ; k++)
c.m[i][j] += a.m[i][k]*b.m[k][j];
}
}
return c;
}
Matrix fast_pow(Matrix base,int index) {
Matrix temp;
temp.m[][] = temp.m[][] = ;
temp.m[][] = temp.m[][] = ;
while(index) {
if(index&) temp = multi(base,temp);
index >>= ;
base = multi(base,base);
}
return temp;
}
int main() {
double ans;
int i;
while(~scanf("%d %lf",&n,&p)){//double的读入一定要用lf%
Matrix temp;
temp.m[][] = p;
temp.m[][] = ;
temp.m[][] = ;
temp.m[][] = -p;
ans = ;
for(i = ; i < n; i++)
scanf("%d",d+i);
sort(d,d+n);
Matrix temp2 = fast_pow(temp,d[]-);
ans *= -temp2.m[][];
for(i = ; i < n; i++){
if(d[i] == d[i-]) continue;
temp2 = fast_pow(temp,d[i]-d[i-]-);
ans *= -temp2.m[][];
}
printf("%.7f\n",ans);
}
return ;
}
BNU 4346 Scout YYF I的更多相关文章
- POJ 3744 Scout YYF I
分段的概率DP+矩阵快速幂 Scout YYF I Time Limit: 1000MS Memory Limit: 65536K Total Sub ...
- 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)
Scout YYF I Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5565 Accepted: 1553 Descr ...
- poj4474 Scout YYF I(概率dp+矩阵快速幂)
Scout YYF I Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4100 Accepted: 1051 Descr ...
- POJ 3744 Scout YYF I 概率dp+矩阵快速幂
题目链接: http://poj.org/problem?id=3744 Scout YYF I Time Limit: 1000MSMemory Limit: 65536K 问题描述 YYF is ...
- poj3744 Scout YYF I[概率dp+矩阵优化]
Scout YYF I Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8598 Accepted: 2521 Descr ...
- 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)
Scout YYF I YYF is a couragous scout. Now he is on a dangerous mission which is to penetrate into th ...
- [Poj3744]Scout YYF I (概率dp + 矩阵乘法)
Scout YYF I Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9552 Accepted: 2793 Descr ...
随机推荐
- 递推DP HDOJ 5092 Seam Carving
题目传送门 /* 题意:从上到下,找最短路径,并输出路径 DP:类似数塔问题,上一行的三个方向更新dp,路径输出是关键 */ #include <cstdio> #include < ...
- Suricata的初始化脚本
见官网 https://suricata.readthedocs.io/en/latest/initscripts.html
- archsummit_bj2016
http://bj2016.archsummit.com/schedule 大会日程 2016年12月02日,星期五 7:45-9:00 签到 8:45-9:00 开始入场 9:00-9:30 开场致 ...
- logback日志异步打印
最近碰到一个问题:客户的服务器程序偶尔出现请求响应过慢的情况,通过查看日志发现RSA验证签名的代码执行超过20秒,而正常情况下只需要16毫秒. RSA证书是服务器启动就加载好的,不存在读文件慢的问题. ...
- 上交oj1219 重要的逆序数对
题意: https://acm.sjtu.edu.cn/OnlineJudge/problem/1219 思路: 在经典的归并排序求逆序数对算法基础上稍作修改. 实现: #include <io ...
- 应用-如何使不同的企业使用独自的数据源。使用ejb3.0+jboss6.2EAP+JPA
摘要: 如何使不同的企业使用独自的数据源.使用ejb3.0+jboss6.2EAP+JPA10C应用系统被多个企业同时使用,为了提供个性化服务,如何使不同的企业使用独自的 ...
- 如何通过Java代码判断当前的环境是否支持JRE 9
JDK9已经出来有一段时间了,因此很多流行的Java应用纷纷增添了对JDK9乃至JDK10的支持,比如Tomcat. 我们通过这个链接下载最新的Tomcat源文件包,总共7MB: https://to ...
- 恩智浦iMX6Q核心板/飞思卡尔Cortex-A9高稳定性低功耗开发板
iMX6Q核心板-商业级 iMX6Q-Plus核心板 iMX6DL核心板-商业级 iMX6Q核心板-工业级 iMX6核心板区别: 名称 主频 内存 存储 SATA接口 运行温度 引角扩展 iMX6Q核 ...
- 升级或者重装Discuz! 版本后 QQ互联英文乱码显示的正确解决方法
升级Discuz! X3版本QQ互联英文乱码!connect_viewthread_share_to_qq! 目前Discuz!论坛上 最简单的解决方法: 第一步:后台----->站长---- ...
- ArrayList Vector LinkedList分析
1.创建 ArrayList 的底层是一个数组. ArrayList<String> list1 = new ArrayList<>(); list1.add("a ...