刷题总结—— Scout YYF I(poj3744 矩阵快速幂+概率dp)
题目:
Description
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]为走到i位置的概率··容易得出dp方程f[i]=f[i-1]*p+f[]i-2]*[1-p],但题目中的路径长度太大··然而地雷数量却很少··因此我们可以将地雷与地雷间的路程分段··分段用矩阵快速幂来求
代码:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<cctype>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
struct matrix
{
double a[][];
inline void I()
{
memset(a,,sizeof(a));
a[][]=a[][]=;
}
friend inline matrix operator *(matrix A,matrix B)
{
matrix temp;memset(temp.a,,sizeof(temp.a));
for(int i=;i<=;i++)
for(int j=;j<=;j++)
for(int k=;k<=;k++)
temp.a[i][j]+=A.a[i][k]*B.a[k][j];
return temp;
}
matrix Pw(int b)
{
matrix ans,A=*this;ans.I();
for(; b; b>>= , A=A*A)
if(b&) ans=ans*A;
return ans;
}
};
int pos[],n;
double p;
int main()
{
//freopen("a.in","r",stdin);
while(scanf("%d%lf",&n,&p)!=EOF)
{
for(int i=;i<=n;i++) scanf("%d",&pos[i]);
sort(pos+,pos+n+);
if(pos[]==)
{
cout<<"0.0000000"<<endl;continue;
}
matrix P,f;P.a[][]=,P.a[][]=-p,P.a[][]=,P.a[][]=p;
f.a[][]=,f.a[][]=;pos[]=;
for(int i=;i<=n;i++)
{
f=f*P.Pw(pos[i]-pos[i-]-);
f.a[][]=f.a[][];f.a[][]=;
}
f=f*P;
printf("%0.7f\n",f.a[][]);
}
return ;
}
刷题总结—— Scout YYF I(poj3744 矩阵快速幂+概率dp)的更多相关文章
- poj 3744 矩阵快速幂+概率dp
题目大意: 输入n,代表一位童子兵要穿过一条路,路上有些地方放着n个地雷(1<=n<=10).再输入p,代表这位童子兵非常好玩,走路一蹦一跳的.每次他在 i 位置有 p 的概率走一步到 i ...
- POJ 3744 Scout YYF I(矩阵快速幂优化+概率dp)
http://poj.org/problem?id=3744 题意: 现在有个屌丝要穿越一个雷区,雷分布在一条直线上,但是分布的范围很大,现在这个屌丝从1出发,p的概率往前走1步,1-p的概率往前走2 ...
- 矩阵快速幂+概率DP poj 3744
题意:在一条不满地雷的路上,你现在的起点在1处.在N个点处布有地雷,1<=N<=10.地雷点的坐标范围:[1,100000000]. 每次前进p的概率前进一步,1-p的概率前进1-p步.问 ...
- 2018.10.23 bzoj1297: [SCOI2009]迷路(矩阵快速幂优化dp)
传送门 矩阵快速幂优化dp简单题. 考虑状态转移方程: f[time][u]=∑f[time−1][v]f[time][u]=\sum f[time-1][v]f[time][u]=∑f[time−1 ...
- 【矩阵快速幂优化DP】【校内测试】
实际上是水水题叻,先把朴素DP方程写出来,发现$dp[i]$实际上是$dp[i-k]-dp[i-1]$的和,而看数据范围,我们实际上是要快速地求得这段的和,突然就意识到是矩阵快速幂叻. 构建矩阵什么的 ...
- 【20181019T2】硬币【矩阵快速幂优化DP】
题面 [错解] 哎\(N \leq 50\)?双向搜索? 切了切-- 等下,好像要求方案数-- 好像搜不了 哎他给\(V_{i} | V_{i+1}\)干嘛? 肯定有用啊 为了体现条件的用处,我在搜下 ...
- HDU5411——CRB and Puzzle——————【矩阵快速幂优化dp】
CRB and Puzzle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- 省选模拟赛 Problem 3. count (矩阵快速幂优化DP)
Discription DarrellDarrellDarrell 在思考一道计算题. 给你一个尺寸为 1×N1 × N1×N 的长条,你可以在上面切很多刀,要求竖直地切并且且完后每块的长度都是整数. ...
- POJ 2778 DNA Sequence ( AC自动机、Trie图、矩阵快速幂、DP )
题意 : 给出一些病毒串,问你由ATGC构成的长度为 n 且不包含这些病毒串的个数有多少个 分析 : 这题搞了我真特么久啊,首先你需要知道的前置技能包括 AC自动机.构建Trie图.矩阵快速幂,其中矩 ...
随机推荐
- 漫谈 Clustering (2): k-medoids
上一次我们了解了一个最基本的 clustering 办法 k-means ,这次要说的 k-medoids 算法,其实从名字上就可以看出来,和 k-means 肯定是非常相似的.事实也确实如此,k-m ...
- Bootstrap 弹出框(Popover)插件
Bootstrap 弹出框(Popover)插件与Bootstrap 提示工具(Tooltip)插件类似,提供了一个扩展的视图,用户只需要把鼠标指针悬停到元素上面即可.弹出框的内容完全由Bootstr ...
- 自动化运维工具——ansible模板与roles(四)
一. 模板Templates 文本文件,嵌套有脚本(使用模板编程语言编写) Jinja2语言,使用字面量,有下面形式 字符串:使用单引号或双引号 数字:整数,浮点数 列表:[item1, item2, ...
- shell的条件判断
.字符串判断 str1 = str2 当两个串有相同内容.长度时为真 str1 != str2 当串str1和str2不等时为真 -n str1 当串的长度大于0时为真(串非空) -z str1 当串 ...
- Jane Austen【简·奥斯汀】
Jane Austen Jane Austen, a famous English writer, was born at Steventon, Hampshire, on December 16, ...
- DFS:Tempter of the Bone (规定时间达到规定地点)
解题心得: 1.注意审题,此题是在规定的时间达到规定的地点,不能早到也不能晚到.并不是最简单的dfs 2.在规定时间达到规定的地点有几个剪枝: 一.公式:所需的步骤 - x相差行 - y相差列 = 偶 ...
- android json 解析 kotlin
前面 写了一次 kotlin解析json 但是,真的写得太烂,直接删掉了,现在重新整理一下.顺便记录一下今天坑了我很久的小问题. 1.首先从最简单的入手吧 一个json的字符串:=====就叫做jso ...
- easyui-combogrid匹配查询
用到easyui-combogrid,数据比较少的情况,可以一页就显示完毕,然后直接下拉选择.但是对于数据量比较大的情况,一页显示全部显然不合适,好在从easyui-combogrid的数据加载方式可 ...
- oracle 迭代查询
Oracle 迭代查询, 以后台菜单作为示例 这是要准备的sql create table tbl_menu( id number primary key, parent_id , name ) no ...
- loj2065 「SDOI2016」模式字符串
ref不是太懂 #include <iostream> #include <cstring> #include <cstdio> using namespace s ...