分段的概率DP+矩阵快速幂

                       Scout YYF I
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4180   Accepted: 1076

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

Source

POJ Monthly Contest - 2009.08.23, Simon

如果不用快速幂(TLE的)。。。。。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; int n,mine[];
double p,d1,d2,d3,ans; int main()
{
while(scanf("%d%lf",&n,&p)!=EOF)
{
for(int i=;i<=n;i++)
{
scanf("%d",mine+i);
}
sort(mine,mine++n);
if(mine[]==)
{
printf("0.0000000\n"); continue;
}
else if(n==)
{
printf("1.0000000\n"); continue;
}
bool flag=false;
for(int i=;i<n;i++)
{
if(mine[i]+==mine[i+])
{
printf("0.0000000\n"); flag=true; break;
}
}
if(flag==true) continue;
ans=.;
for(int i=;i<=n;i++)
{
int st=mine[i-]+,ed=mine[i];
d2=.,d1=.;
for(int j=st+;j<=ed;j++)
{
d3=d1*p+d2*(-p);
d2=d1; d1=d3;
}
ans*=(-d3);
}
printf("%.7lf\n",ans);
}
return ;
}

快速幂的。。。。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; struct Matrix
{
double a[][];
Matrix() {}
Matrix(double A,double B,double C,double D)
{
a[][]=A;a[][]=B;a[][]=C;a[][]=D;
}
Matrix operator* (const Matrix& b) const
{
Matrix temp;
memset(temp.a,,sizeof(temp.a));
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
for(int k=;k<;k++)
{
temp.a[i][j]+=a[i][k]*b.a[k][j];
}
}
}
return temp;
}
Matrix Show()
{
for(int i=;i<;putchar(),i++) for(int j=;j<;putchar(' '),j++) cout<<a[i][j];
}
}; Matrix QuickPow(Matrix m,int n)
{
Matrix E(,,,);
while(n>)
{
if(n&) E=E*m;
m=m*m;
n=n>>;
}
E=E*m;
return E;
} int n,mine[];
double p,ans; int main()
{
while(scanf("%d%lf",&n,&p)!=EOF)
{
for(int i=;i<=n;i++)
scanf("%d",mine+i);
sort(mine,mine+n+);
if(mine[]==)
{
printf("0.0000000\n"); continue;
}
else if(n==)
{
printf("1.0000000\n"); continue;
}
bool flag=false;
for(int i=;i<n;i++)
{
if(mine[i]+==mine[i+])
{
printf("0.0000000\n"); flag=true; break;
}
}
if(flag==true) continue;
ans=.;
for(int i=;i<=n;i++)
{
Matrix m(p,-p,,);
m=QuickPow(m,mine[i]-mine[i-]-);
ans*=-m.a[][];
}
printf("%.7lf\n",ans);
}
return ;
}

POJ 3744 Scout YYF I的更多相关文章

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

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

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

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

  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 1 (概率DP+矩阵快速幂)

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

  5. poj 3744 Scout YYF I (矩阵)

    Description YYF -p. Here is the task, given the place of each mine, please calculate the probality t ...

  6. poj 3744 Scout YYF I (可能性DP+矩阵高速功率)

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5062   Accepted: 1370 Description YYF i ...

  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://poj.org/problem?id=3744 题意: 有n个地雷,位置为pos[i]. 在每个位置,你向前走一步的概率为p,向前走两步的概率为1-p. 你的初始位置为1. 问 ...

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

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

随机推荐

  1. nginx实现http反向代理+负载均衡

    原理 反向代理:反向代理(reverse proxy)方式是指以代理来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客 ...

  2. Ubuntu 使用笔记

    持续更新 从前使用sudo apt update更新时, 发现软件源是cn.archive.ubuntu.com. 最近在system setting 中将软件源改成了archive.ubuntu.c ...

  3. UVa 10720 - Graph Construction(Havel-Hakimi定理)

    题目链接: 传送门 Graph Construction Time Limit: 3000MS     Memory Limit: 65536K Description Graph is a coll ...

  4. Java Servlet完全教程

    Servlet 是一些遵从Java Servlet API的Java类,这些Java类可以响应请求.尽管Servlet可以响应任意类型的请求,但是它们使用最广泛的是响应web方面的请求. Servle ...

  5. POJ 1038 Bugs Integrated, Inc.

    AC通道 神坑的一道题,写了三遍. 两点半开始写的, 第一遍是直接维护两行的二进制.理论上是没问题的,看POJ discuss 上也有人实现了,但是我敲完后准备开始调了.然后就莫名其妙的以为会超时,就 ...

  6. js005-引用类型

    js005-引用类型 数据类型分为基本类型和引用类型:基本类型值如下:Undefined.Null.Bollean.Number.String 本章内容: 1.使用对象 2.创建并操作数组 3.理解基 ...

  7. telnet连接ip

    win7 telnet命令无法开启的解决方案(不是内部命令或外部命令) 出处:西西整理 作者:西西 日期:2012-12-21 14:22:06 [大 中 小] 评论: 0 | 我要发表看法 如果你想 ...

  8. JavaWeb学习笔记——DAO设计模式

  9. Android学习笔记——CheckBox

    该工程的功能实现在一个activity中显示一个单选框和一个多选框 以下代码是MainActivity.java文件中的代码 package com.example.checkbox; import ...

  10. header的安全配置指南

    0x00 背景 在统计了Alexa top 100万网站的header安全分析之后(2012年11月 - 2013年3月 - 2013年11月),我们发现其实如何正确的设置一个header并不是一件容 ...