F - Scout YYF I

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit Status

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

题意:

有一段路,路上有n个陷阱,每一次只能向前走一步或者两步,求安全走过这段路的改路

分析:

设dp[i]表示安全走过第i个陷阱的概率

那么dp[i+1]=dp[i]*(1-p(走到第i+1个陷阱))

因为每次只能走一步或者两步,所有安全走过第i个陷阱后的位置一定在a[i]+1;

其中a[i]表示第i个陷阱的位置

求从a[i]+1,走到a[i+1]的概率的时候我们需要用到矩阵来经行优化

ans[i]表示走到位置i的概率

ans[i] = p*ans[i-1]+(1-p)*ans[i-2];

ans[0]=1;

都说G++比C++要好,但是本题最好用C++提交。

如果用G++提交,浮点型输出一定要用%f,否则会WA。

这是因为G++标准的浮点型输出用%f,而不是%lf。

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <string.h>
using namespace std;
const int maxn=;
int cnt[maxn];
double dp[maxn];
double p;
int n;
struct matrix
{
double data[][];
};//定义成封装结构体 二维数组无法return
matrix I={,,,};
matrix multi(matrix a,matrix b)
{
matrix c;
memset(c.data,,sizeof(c.data));
for(int i=;i<;i++)
for(int j=;j<;j++)
for(int k=;k<;k++)
c.data[i][j]+=a.data[i][k]*b.data[k][j];
return c;
}
double pow1(matrix a,int b)
{
matrix ans=I;
while(b)
{
if(b&)
ans=multi(ans,a);
b>>=;
a=multi(a,a);
}
return ans.data[][];
}
int main()
{
while(cin>>n>>p)
{
memset(dp,,sizeof(dp)); //该初始化的地方一定要初始化
cnt[]=; //为第一个雷做准备的
for(int i=;i<=n;i++)
cin>>cnt[i];
sort(cnt,cnt+n+);
dp[]=1.0; //不是0
matrix a={p,-p,,}; //初始化矩阵,注意这个的推导过程
for(int i=;i<=n;i++)
//要1减去这个概率 因为这个概率是踩上雷的
dp[i]=dp[i-]*(-pow1(a,cnt[i]-cnt[i-]-)); //注意不要丢括号
//注意是乘法不是加法 把每段概率乘起来
//注意是-1不是+1
//从上一个雷的下一个起 要走cnt[i]-cnt[i-1]-1步 才能到达下一个雷
//雷在1和5 从2走到5 需要3步 5-1-1=3
printf("%.7f\n",dp[n]);
}
return ;
}

poj 3744 Scout YYF 1 (概率DP+矩阵快速幂)的更多相关文章

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

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

  2. POJ3744 Scout YYF I 概率DP+矩阵快速幂

    http://poj.org/problem?id=3744 题意:一条路,起点为1,有概率p走一步,概率1-p跳过一格(不走中间格的走两步),有n个点不能走,问到达终点(即最后一个坏点后)不踩坏点的 ...

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

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

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

    题意: 一条路上有n个地雷,你从1开始走,单位时间内有p的概率走一步,1-p的概率走两步,问安全通过这条路的概率 解析: 很容易想到 dp[i] = p * dp[i-1] + (1 - p) * d ...

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

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

  6. 刷题总结—— Scout YYF I(poj3744 矩阵快速幂+概率dp)

    题目: Description YYF is a couragous scout. Now he is on a dangerous mission which is to penetrate int ...

  7. poj3744 (概率DP+矩阵快速幂)

    http://poj.org/problem?id=3744 题意:在一条铺满地雷的路上,你现在的起点在1处.在N个点处布有地雷,1<=N<=10.地雷点的坐标范围:[1,10000000 ...

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

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

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

    题意: 一条路上,给出n地雷的位置,人起始位置在1,向前走一步的概率p,走两步的概率1-p,踩到地雷就死了,求安全通过这条路的概率. 分析: 如果不考虑地雷的情况,dp[i],表示到达i位置的概率,d ...

随机推荐

  1. phpstorm 安装XeDbug

    第一步:根据phpinfo()下载相对应的Xdebug插件,Xdebug下载路径https://xdebug.org/download.php 第二步:将下载好的Xdebug放到 G:\Service ...

  2. 解答室内定位技术新方向:蓝牙AoA定位,值得了解 ——概念了解

    转载搜狐 室内定位一直被炒的非常火的黑科技,也是近年资本追逐的热点,市场上一直有众多宣称可以做到厘米级,米级精度定位的公司,但问题很多,无法大规模商用.近些年有很多人尝试使用蓝牙beacon方式做定位 ...

  3. [Link-Cut-Tree][BZOJ2631]Tree

    题面 Description: 一棵\(n\)个点的树,每个点的初始权值为\(1\).对于这棵树有\(q\)个操作,每个操作为以下四种操作之一: + u v c:将\(u\)到\(v\)的路径上的点的 ...

  4. P1282

    题目描述 多米诺骨牌有上下2个方块组成,每个方块中有1~6个点.现有排成行的 上方块中点数之和记为S1,下方块中点数之和记为S2,它们的差为|S1-S2|.例如在图8-1中,S1=6+1+1+1=9, ...

  5. Spark 源码阅读——任务提交过程

    当我们在使用spark编写mr作业是,最后都要涉及到调用reduce,foreach或者是count这类action来触发作业的提交,所以,当我们查看这些方法的源码时,发现底层都调用了SparkCon ...

  6. Spark机器学习之推荐引擎

    一. 最小二乘法建立模型 关于最小二乘法矩阵分解,我们可以参阅: 一.矩阵分解模型. 用户对物品的打分行为可以表示成一个评分矩阵A(m*n),表示m个用户对n各物品的打分情况.如下图所示: 其中,A( ...

  7. 简易版AI英文问答程序解决

    第四章的作业和实践题要论印象深刻无疑就是AI的那道题了.不得不说一开始看到题目的时候,我真的蒙了很久. 本题要求你实现一个简易版的 AI 英文问答程序,规则是: 1.无论用户说什么,首先把对方说的话在 ...

  8. ElasticSearch学习笔记(三)-- 查询

    1. URISearch详解与演示 2. QueryDSL简介 3. 字段类查询简介及match-query 4. 相关性算分 5. match-phrase-query 6. query-strin ...

  9. QQ空间相册展示特效

    <!doctype html> <html lang="en"> <head> <title>QQ空间相册展示特效<title ...

  10. CodeIgniter学习笔记一:基本结构、控制器、视图、超级对象、数据库

    一.基本结构 CodeIgniter3.0.0解压后有8个文件,分别是: application:项目文件 system:系统(框架)文件,为方便升级,不建议修改 user_guid:用户手册,不需要 ...