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. spark序列化及MapOutputTracker解析

    本文主要打算对spark内部的序列化机制以及在shuffle map中起衔接作用的MapOutputTracker做一下剖析.主要涉及具体实现原理以及宏观设计的一些思路. 1,spark序列化 任何一 ...

  2. JavaScript星级评分,仿百度,增强版

    JavaScript星级评分,仿百度,增强版 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" & ...

  3. Json和序列化总结

    一.序言 遇到问题,就经常逛园,不知你是否曾有,曾经遇到的问题,或者在园子里看到问题的方案,过一段时间,有可能还会摔跤,哈哈...大神请勿喷,小弟记忆不太好,还过来找资料,如果自己写把问题或某个知识点 ...

  4. 4. iOS测试常用方法

    1.    [XCUIElement exists]方法只能确定这个View是否存在,即使不在当前屏幕上也返回True.如果要确定View是否在屏幕可见范围内,可以判断其Frame是否在Window的 ...

  5. 生成器的send方法

    send 和next区别 next:唤醒并继续执行 send:唤醒并继续执行 发送信息到生成器内部. def fib(max): n,a,b = 0,0,1 while n < max: msg ...

  6. 项目适配iOS9遇到的一些问题及解决办法 ,以及URL 白名单配置方法

    1.网络请求报错.升级Xcode 7.0发现网络访问失败.输出错误信息 The resource could not be loaded because the App Transport Secur ...

  7. uva1627 Team them up!

    注意这题要求互相认识不认识的人之间连一条线一个人在组1,那么不认识(互相认识)的人就在组0:同时这些人不认识的人就在组1.每个联通分量都可以独立推导,遇到矛盾则无解一个联通分量有一个核心,其他的点是分 ...

  8. 解决VSCode中使用vetur插件格式化vue文件时,js代码会自动加上冒号和分号

    解决VSCode中使用vetur插件格式化vue文件时,js代码会自动加上冒号和分号 在设置中把"vetur.format.defaultFormatter.js": " ...

  9. 按名字寻找文件和文件夹 find命令

    find <指定目录> <指定条件> <指定动作> find /home/bnrc/py-faster-rcnn/caffe-fast-rcnn/ -name 'd ...

  10. python之bool (布尔值)

    用途: ​ 判断真假 识记: 空的字符串是False,非空的就是True 0 是False,非0的都是True 格式: True False 布尔值转换成字符串: print(type(str(Tru ...