分段的概率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. HttpClient + Jsoup模拟登录教务处并获取课表

    1.概述 最近想做一个校园助手类的APP,由于第一次做,所以打算先把每个功能单独实现,防止乱了阵脚.利用教务处登录获取课表和成绩等是一个基本功能,所以以获取课表为例实现了这个功能.完整代码点这里,尝试 ...

  2. [iOS dispatch_once创建单例]

    自苹果引入了Grand Central Dispatch (GCD)(Mac OS 10.6和iOS4.0)后,创建单例又有了新的方法,那就是使用dispatch_once函数,当然,随着演进的进行, ...

  3. C++ 动态数组实例

    一维动态数组的实例: #include <iostream> using namespace std; int main() { int *arr; int n; cout<< ...

  4. Beta版本冲刺第六天 12.12

    一.站立式会议照片: 二.项目燃尽图: Android端 后台 三.项目进展: 成 员 昨天完成任务 今天完成任务 明天要做任务 问题困难 心得体会 胡泽善 邮箱验证和用户评价的填写 用户评价的查看以 ...

  5. BZOJ1915: [Usaco2010 Open]奶牛的跳格子游戏

    权限题,没有传送门. 这很显然是一道DP题,刚看完题目可能会比较懵逼.这道题如果不要求回去,那么就是一道很裸的DP题.但是本题要求回去而且回去的格子的前一个格必须是之前经过的. 先不考虑回去的路程,对 ...

  6. 卸载自己编译的程序(ubuntu14.04)

    cd 源代码目录make clean./configuremake uninstall

  7. CSS3-transform变形功能

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  8. sql自带函数语句

    --取数值表达式的绝对值select abs(-41)      41select abs(41)       41select abs(-41.12)   41.12select abs(41.12 ...

  9. MySQL------MySQL与SQLServer数据类型的转换

    MySQL SQLServer

  10. iocp还是select

    上一个项目libevent应该是select,现在libuv是iocp,都知道Windows下iocp比select效率高,boost asio 也是iocp,但具体使用select和iocp发现没有 ...