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. Plugging an Unplugged Pluggable Database

    1.unplug To unplug a PDB, you first close it and then generate an XML manifest file. The XML file co ...

  2. PowerShell~执行策略的介绍

    首先看一下无法加载ps1脚本的解决方法 事实上也是由于策略导致的  解决方法主是开启对应的策略 set-ExecutionPolicy RemoteSigned 执行策略更改 执行策略可以防止您执行不 ...

  3. spring mvc 通过拦截器记录请求数据和响应数据

    spring mvc 能过拦截器记录请求数据记录有很多种方式,主要有以下三种: 1:过滤器 2:HandlerInterceptor拦截器 3:Aspect接口控制器 但是就我个人所知要记录返回的数据 ...

  4. web前端工程师入门须知

    本文是写给那些想要入门web前端工程的初学者,高手请路过,也欢迎高手们拍砖. 先说下web前端工程师的价值,目前web产品交互越来越复杂,用户使用体验和网站前端性能优化这些都得靠web前端工程师去做w ...

  5. JavaScript也谈内存优化

    相对C/C++ 而言,我们所用的JavaScript 在内存这一方面的处理已经让我们在开发中更注重业务逻辑的编写.但是随着业务的不断复杂化,单页面应用.移动HTML5 应用和Node.js 程序等等的 ...

  6. uva11925 Generating Permutations

    逆序做,逆序输出 紫书上的描述有点问题 感觉很经典 ans.push_back(2); a.insert(a.begin(),a[n-1]); a.erase(a.end()-1); a.push_b ...

  7. bind的使用

    bind: 改变this的指向,返回一个新函数(它不会立即执行,需要调用新函数才能执行:apply call方法是立即执行) let obj = { name: 'jason888'} functio ...

  8. PHP20 PHP面向对象辅助

    学习要点 常用函数 命名空间 类的自动加载 常用函数 class_exists 作用:检查类是否已定义 语法格式: bool class_exists ( string $class_name [, ...

  9. 【2019.6.2】python:json操作、函数、集合、random()等

    一.json操作: json就是一个字符串,从文件中读取json,必须是json格式.j'son串中必须是双引号,不能有单引号,单引号不能转换 1.1使用: import json #使用json先引 ...

  10. 利用python库twilio来免费发送短信

    大家好,我是四毛,最近开通了个人公众号“用Python来编程”,欢迎大家“关注”,这样您就可以收到优质的文章了. 今天跟大家分享的主题是利用python库twilio来免费发送短信. 先放一张成品图 ...