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 ...
随机推荐
- 529 Minesweeper 扫雷游戏
详见:https://leetcode.com/problems/minesweeper/description/ C++: class Solution { public: vector<ve ...
- (025)[系统故障]XP下禁止将串口设备识别成鼠标(转)
很多人都遇到过这种问题:Windows XP启动时将一台连续发送数据的串口设备识别成串口鼠标,自动安装了串口鼠标驱动,而设备因此无法正常工作,而光标则无规律地到处跑.对此问题,Internet上的方法 ...
- HDU 5808 Price List Strike Back bitset优化的背包。。水过去了
http://acm.hdu.edu.cn/showproblem.php?pid=5808 用bitset<120>dp,表示dp[0] = true,表示0出现过,dp[100] = ...
- G. 24 观察 + 树状数组
http://codeforces.com/gym/101257/problem/G 首先要看到题目,题目是本来严格大于score[i] > score[j].然后score[i] < s ...
- R语言中的并行处理
网上有人说foreach包可以并行,我就去弄了,结果发现一个普通的二重循环什么事都不错都很卡!捣鼓了半天才发现是foreach的问题 为了提速,做了如下事宜: 直接利用矩阵列加减,不是一个个遍历加 把 ...
- [转].NET MVC 分页以及增删查改
本文转自:http://blog.csdn.net/sust2012/article/details/30761867 . 数据库操作,DAL 层: using System; using Syste ...
- [转]深入浅出WPF(7)——数据的绿色通道,Binding
本文转自:http://liutiemeng.blog.51cto.com/120361/95273 小序: 怎么直接从2蹦到7啦?!啊哦,实在是不好意思,最近实在是太忙了,忙的原因也非常简单——自己 ...
- .net 音频转换 .amr 转 .mp3 (七牛转换法)
.amr 用于移动设备的音频,压缩比比较大,多用于人声.通话,效果还行!所以,移动设备多采用amr格式来进行录存!比较常见的例子:通话录音,微信语音以及录音等! 这个鬼,用两个字来形容,就是“蛋疼”: ...
- IF-MIB::ifTable = No Such Object available on this agent at this OID
在server端口执行如下命令 [root@localhost ~]# snmpwalk -v2c -c public 客户端ip ifIF-MIB::ifTable = No Such Object ...
- 微软将于12月起开始推送Windows 10 Mobile
[环球科技报道 记者 陈薇]据瘾科技网站10月8日消息,根据微软Lumia官方Faceboo发布的消息,新版系统Windows 10 Mobile 将会12月起陆续开始推送. 推送的具体时程根据地区. ...