题目:

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

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)的更多相关文章

  1. poj 3744 矩阵快速幂+概率dp

    题目大意: 输入n,代表一位童子兵要穿过一条路,路上有些地方放着n个地雷(1<=n<=10).再输入p,代表这位童子兵非常好玩,走路一蹦一跳的.每次他在 i 位置有 p 的概率走一步到 i ...

  2. POJ 3744 Scout YYF I(矩阵快速幂优化+概率dp)

    http://poj.org/problem?id=3744 题意: 现在有个屌丝要穿越一个雷区,雷分布在一条直线上,但是分布的范围很大,现在这个屌丝从1出发,p的概率往前走1步,1-p的概率往前走2 ...

  3. 矩阵快速幂+概率DP poj 3744

    题意:在一条不满地雷的路上,你现在的起点在1处.在N个点处布有地雷,1<=N<=10.地雷点的坐标范围:[1,100000000]. 每次前进p的概率前进一步,1-p的概率前进1-p步.问 ...

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

  5. 【矩阵快速幂优化DP】【校内测试】

    实际上是水水题叻,先把朴素DP方程写出来,发现$dp[i]$实际上是$dp[i-k]-dp[i-1]$的和,而看数据范围,我们实际上是要快速地求得这段的和,突然就意识到是矩阵快速幂叻. 构建矩阵什么的 ...

  6. 【20181019T2】硬币【矩阵快速幂优化DP】

    题面 [错解] 哎\(N \leq 50\)?双向搜索? 切了切-- 等下,好像要求方案数-- 好像搜不了 哎他给\(V_{i} | V_{i+1}\)干嘛? 肯定有用啊 为了体现条件的用处,我在搜下 ...

  7. HDU5411——CRB and Puzzle——————【矩阵快速幂优化dp】

    CRB and Puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  8. 省选模拟赛 Problem 3. count (矩阵快速幂优化DP)

    Discription DarrellDarrellDarrell 在思考一道计算题. 给你一个尺寸为 1×N1 × N1×N 的长条,你可以在上面切很多刀,要求竖直地切并且且完后每块的长度都是整数. ...

  9. POJ 2778 DNA Sequence ( AC自动机、Trie图、矩阵快速幂、DP )

    题意 : 给出一些病毒串,问你由ATGC构成的长度为 n 且不包含这些病毒串的个数有多少个 分析 : 这题搞了我真特么久啊,首先你需要知道的前置技能包括 AC自动机.构建Trie图.矩阵快速幂,其中矩 ...

随机推荐

  1. 表格和网页ico图标

    表格: 表格格式: <table> <tr> 表格的行 <th >表头</th> <th>表头 </th> </tr> ...

  2. Xcode Warning: “no rule to process file

    警告⚠️: warning: no rule to process file '/Users/Kingdev/Desktop/Git/finance_iOS/finance/Library/MBpro ...

  3. Http请求 GET和POST,405错误

    我就简单说吧,在用SringMVC时,我们通常会用到 @RequestMapping(value="/test",method=RequestMethod.GET) public ...

  4. 爬虫 xpath etree自动补全页面

    aa = etree.HTML(response.content) bb = etree.tostring(aa) doc = etree.HTML(bb)

  5. 微信小游戏 demo 飞机大战 代码分析 (一)(game.js, main.js)

    微信小游戏 demo 飞机大战 代码分析(一)(main.js) 微信小游戏 demo 飞机大战 代码分析(二)(databus.js) 微信小游戏 demo 飞机大战 代码分析(三)(spirit. ...

  6. 背景透明度处理 兼容IE

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. linux普通文件权限和系统目录权限的实践及结论

    测试结论:linux普通文件的读.写.执行权限说明 1.可读r:表示具有读取\阅读文件内容的权限 2.可写w:表示具有新增.修改文件内容的权限 1)如果没有r配合,那么vi编辑文件会提示无法编辑(但可 ...

  8. 科学计算库Numpy——运算

    np.multiply(array1,array2) 该函数用于数组中对应位置上的数相乘. 一维向量 二维数组 np.dot(array1,array2) 两个数组都是一维向量 数组中对应位置上的数相 ...

  9. Android 服务入门

    前言:硬着头皮把数据库SQLite看完了,接下来就是android服务了,因为自己本身就是菜鸟,所以呢,也只是做做笔记,技术上的东西就别指望我了. 1.什么是服务呢?举个例子,百度地图,美团外卖,OF ...

  10. OpenCV学习笔记(三) 访问像素

    转自:OpenCV如何扫描图像.利用查找表和计时 测试代码:opencv\samples\cpp\tutorial_code\core\how_to_scan_images 测试函数耗时 cv::ge ...