Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5062   Accepted: 1370

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

题意:人人从1開始走,p的概率走1步,1-p的概率走2步,求不踩雷的概率。地雷数N<=10,坐标范围在100000000内。

思路:人走到第i位置的概率为dp[i]=dp[i-1]*p+dp[i-2]*(1-p),因为数据范围较大,可有矩阵高速幂高速求出答案;

构造矩阵     | P ,  1 |   即   | dp[n+1] , dp[n] |  =

| dp[n] , dp[n-1] |    *  | P ,  1 |

| 1-P , 0 |                                                          |
1-P , 0 |

然后将每一个a[i]+1到a[i+1]看做一段单独处理即可。

#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn=100000050; struct node
{
double mat[2][2];
}A,B,C; double dp[maxn],p;
int n,a[15]; void input()
{
for(int i=0;i<n;i++) scanf("%d",&a[i]);
sort(a,a+n);
A.mat[0][0]=p,A.mat[0][1]=1.0;
A.mat[1][0]=1.0-p,A.mat[1][1]=0.0;
} node mul(node p,node q) // 矩阵相乘
{
node ans;
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
{
ans.mat[i][j]=0.0;
for(int k=0;k<2;k++)
ans.mat[i][j]+=p.mat[i][k]*q.mat[k][j];
}
return ans;
} node pow(node w,int num) // 高速幂
{
node ret=B,c=w;
int coun=num;
while(coun)
{
if(coun & 1) ret=mul(ret,c);
coun>>=1;
c=mul(c,c);
}
return ret;
} void solve()
{
int now=1;
double f1=0.0,f2=1.0;
for(int i=0;i<n;i++)
{
if(a[i]-now>=1)
{
node tmp=pow(A,a[i]-1-now);
f2=(f2*tmp.mat[0][0]+f1*tmp.mat[1][0])*(1.0-p);
f1=0.0;
now=a[i]+1;
}
else
{
printf("0.0000000\n");
return ;
}
}
printf("%.7f\n",f2);
} int main()
{
B.mat[0][0]=1.0,B.mat[0][1]=0.0;
B.mat[1][0]=0.0,B.mat[1][1]=1.0;
while(scanf("%d %lf",&n,&p)!=EOF)
{
input();
solve();
}
return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

poj 3744 Scout YYF I (可能性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. poj 3744 Scout YYF 1 (概率DP+矩阵快速幂)

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

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

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

  4. poj 3744 Scout YYF I(递推求期望)

    poj 3744 Scout YYF I(递推求期望) 题链 题意:给出n个坑,一个人可能以p的概率一步一步地走,或者以1-p的概率跳过前面一步,问这个人安全通过的概率 解法: 递推式: 对于每个坑, ...

  5. POJ 3744 Scout YYF I

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

  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 I(矩阵快速幂优化+概率dp)

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

  8. poj 3744 Scout YYF I (矩阵快速幂 优化 概率dp)

    题目链接 分析&&题意来自 : http://www.cnblogs.com/kuangbin/archive/2012/10/02/2710586.html 题意: 在一条不满地雷的 ...

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

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

随机推荐

  1. android生成apk包出现Unable to add &quot;XXX&quot; Zip add failed问题

    最近试图整合umeng至cocos2d-x围棋项目,一切好工作,准备生成apk当出现了大量的数据包 [2014-06-03 20:02:52 - MyApp] Unable to add 'G:\co ...

  2. hdu N!

    Problem Description Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N! Input One N in o ...

  3. 完全背包(南阳oj311)(完全背包)

    全然背包 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描写叙述 直接说题意,全然背包定义有N种物品和一个容量为V的背包.每种物品都有无限件可用. 第i种物品的体积是c.价值 ...

  4. Jafka来源分析——Processor

    Jafka Acceptor接受client而建立后的连接请求,Acceptor会将Socket连接交给Processor进行处理.Processor通过下面的处理步骤进行client请求的处理: 1 ...

  5. poj 2762 Going from u to v or from v to u? (推断它是否是一个薄弱环节图)

    意甲冠军:给定一个有向图有m单向边缘.免费推断是否两点起来(a可以b要么b可以a或最多彼此),该请求 弱联通重量. 算法: 缩点求强连通分量.然后又一次建图.推断新图是否是一条单链,即不能分叉,假设分 ...

  6. LeetCode之Sort List

    称号:Sort a linked list in O(n log n) time using constant space complexity. 对一个单链表进行排序,要求时间复杂度为O(n log ...

  7. Base64编码和解码

    Base64这是一个二进制编码方法转换成可打印字符.主要用于邮件传输. Base64将64人物(A-Z,a-z,0-9,+,/)由于基本字符集.把所有的符号转换成字符集. 编码: 编码每次3节转为4字 ...

  8. 父类中可继承方法在处理private的一个demo

    public abstract class AbstractParent { public AbstractParent() { System.out.println("Hello,pare ...

  9. QR代码简单

    QR代码(Quick Response Code, 高速响应码)属于二维矩阵码在一个.由DENSO(日本电装)公司开发,由JIS和ISO将其标准化. QR码分为两种模式:模式1.模式2.当中.模式1相 ...

  10. 理解Android虚拟机体系结构(转)

    1 什么是Dalvik虚拟机 Dalvik是Google公司自己设计用于Android平台的Java虚拟机,它是Android平台的重要组成部分,支持dex格式(Dalvik Executable)的 ...