A. Scout YYF I

Time Limit: 1000ms
Memory Limit: 65536KB

64-bit integer IO format: %lld      Java class name: Main

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

  1. POJ 3744 Scout YYF I

    分段的概率DP+矩阵快速幂                        Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Sub ...

  2. poj 3744 Scout YYF I(概率dp,矩阵优化)

    Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5020   Accepted: 1355 Descr ...

  3. Scout YYF I(POJ 3744)

    Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5565   Accepted: 1553 Descr ...

  4. poj4474 Scout YYF I(概率dp+矩阵快速幂)

    Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4100   Accepted: 1051 Descr ...

  5. POJ 3744 Scout YYF I 概率dp+矩阵快速幂

    题目链接: http://poj.org/problem?id=3744 Scout YYF I Time Limit: 1000MSMemory Limit: 65536K 问题描述 YYF is ...

  6. poj3744 Scout YYF I[概率dp+矩阵优化]

    Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8598   Accepted: 2521 Descr ...

  7. poj 3744 Scout YYF 1 (概率DP+矩阵快速幂)

    F - Scout YYF I Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub ...

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

  9. [Poj3744]Scout YYF I (概率dp + 矩阵乘法)

    Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9552   Accepted: 2793 Descr ...

随机推荐

  1. 接口测试_RESTClient基本使用

    火狐浏览器插件RESTClient基本使用. 消息头: Content-Type : application/x-www-form-urlencoded

  2. 拦截@RequestBody的请求数据

    要拦截首先想到的是拦截器,@RequestBody只能以流的方式读取,流被读过一次后,就不在存在了,会导致会续无法处理,因此不能直接读流 为了解决这个问题,思路如下: 1.读取流前先把流保存一下 2. ...

  3. 前端的百度地图的api的使用

    1.打开百度地图官方api网页 http://lbsyun.baidu.com/ 2.点击开发文档 3.选择对应的api 4.点击DEMO详情 5.得到源码复制到你的代码中 <!DOCTYPE ...

  4. canvas防画图工具

    <style> body {   background: black;   text-align: center; } #cans {   background: white; } < ...

  5. mac自带终端安装完ohmyZsh后显示乱码

    修改描述文件-添加 选择新导入的 Meslo LG M Regular for Powerline

  6. 7z解压参数

    7z.exe x D:/test/dwpath/xxx.zip -oD:/test/dwpath/ -aoa

  7. TFS2010升级至TFS2013完全指南(更换服务器)

    一.背景:         公司已使用tfs2010很长时间,目前随着公司的发展,项目越来越少,而产品越来越多,采用的开发模式,也逐渐从瀑布式.迭代式转向敏捷开发.为了更好的支持产品研发,决定将tfs ...

  8. IE8 window.open 不支持此接口 的问题解决

    在使用vs2010调试代码时,突然出现 window.open 不支持此接口的提示,开始认为是不是vs的问题,后来上网查询说是系统问题.我不想重装系统,之后发现是IE的问题,使用其他浏览器浏览系统不会 ...

  9. 简单修改BOOK主题样式

    body{ font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; margin: 0px; word ...

  10. RSA2

    进行签名的加密 package com.goboosoft.common.pay.util; import java.io.ByteArrayInputStream; import java.io.I ...