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

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

第一次接触矩阵快速幂,矩阵要专门看,快速幂单独看看

显然,如果k 号位有雷,那么安全通过这个雷只可能是在 k-1 号位选择走两步到 k+1 号位。因此,可以得到如下结论:在第 i 个雷的安全通过的概率就是从 a[i-1]+1 号位到 a[i]+1 号位的概率。于是,可以用 1 减去就可以求出安全通过第 i 个雷的概率,最后乘起来即可,比较悲剧的是数据很大,所以需要用到矩阵快速幂……

类似斐波那契数列,ans[i]=p*ans[i-1]+(1-p)*ans[i-2] ,构造矩阵为

#include <iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
int s[20];
double q,p;
struct node
{
double dp[2][2];//矩阵
};
node mult(node a,node b)//矩阵乘法
{
int i,j,n,k;
node temp;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
temp.dp[i][j]=0;
for(k=0;k<2;k++)
temp.dp[i][j]+=a.dp[i][k]*b.dp[k][j];
}
}
return temp;
}
node cal(int N)//快速幂
{
node a,res;
a.dp[0][0]=p;
a.dp[0][1]=q;
a.dp[1][0]=1;
a.dp[1][1]=0;
res.dp[0][0]=1;
res.dp[0][1]=0;
res.dp[1][0]=0;
res.dp[1][1]=1;
while(N)
{
if(N&1)
{
res=mult(res,a);
}
a=mult(a,a);
N>>=1;
}
return res;
}
int main()
{
int i,j,n,m,max,flag;
double tempqn;
node temp,a;
while(scanf("%d%lf",&n,&p)!=EOF)
{
q=1-p;
s[0]=0;
for(i=1;i<=n;i++)
{
cin>>s[i];
}
sort(s+1,s+1+n);
for(i=1,flag=1;i<n;i++)
{
if(s[i]+1==s[i+1])
flag=0;
}
if(!flag||s[1]==1)
{
puts("0.0000000");
continue;
}
a.dp[0][0]=1;
a.dp[0][1]=0;
a.dp[1][0]=0;
a.dp[1][1]=0;
for(i=1;i<=n;i++)
{
temp=cal(s[i]-s[i-1]-2);
a=mult(temp,a);
a.dp[0][0]=a.dp[0][0]*q;
a.dp[0][1]=0;
a.dp[1][0]=0;
a.dp[1][1]=0;
}
printf("%.7f\n",a.dp[0][0]);
}
return 0;
}

poj4474 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. POJ3744 Scout YYF I 概率DP+矩阵快速幂

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

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

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

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

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

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

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

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

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

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

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

  9. poj 3744 概率dp+矩阵快速幂

    题意:在一条布满地雷的路上,你现在的起点在1处.在N个点处布有地雷,1<=N<=10.地雷点的坐标范围:[1,100000000]. 每次前进p的概率前进一步,1-p的概率前进1-p步.问 ...

随机推荐

  1. SGU 146.The Runner

    时间限制:0.25s 空间限制:4M 题意: 一个人在一个周长为L的圆上跑,每个时间段(Ti)的速度(Vi)不一样,问最后他离起点的圆弧距离,周长是个有四位小数的浮点数,其它全是整数. Solutio ...

  2. oc 怎么接收NSSting字符的方法

    ]; //使用一个缓冲区 NSLog(@"请输入一个字符串:"); scanf("%s",buffer); NSString * str = [NSString ...

  3. MySQL中删除重复数据只保留一条

    用SQL语句,删除掉重复项只保留一条 在几千条记录里,存在着些相同的记录,如何能用SQL语句,删除掉重复的呢 1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 SELECT ...

  4. 使用jQuery动态加载js脚本文件的方法

    动态加载Javascript是一项非常强大且有用的技术.这方面的主题在网上已经讨论了不少,我也经常会在一些个人项目上使用RequireJS和Dojo加载js 它们很强大,但有时候也会得不偿失.如果你使 ...

  5. nodebb在阿里云主机部署过程

    1.在centos上安装nodejswget http://nodejs.org/dist/v0.8.9/node-v0.8.9.tar.gztar zxvf node-v0.8.9.tar.gzcd ...

  6. [原创]用python写了一个简单的markdown编辑器

    以前我常用openoffice记录东西,最喜欢它的当然是在linux上能用了,还有里面的公式输入,前几天才了解markdown这个东东,初步了解发现它正是我需要的东西,可以用它随心所欲地记录些东西,而 ...

  7. awk里的各种坑

    今天又遇到一个,一旦需要定义一个局部数组(awk通过把局部变量定义在函数参数列表来实现局部这一特征)那么这个数组可以正常的操作,但是无法对他取长度,一旦使用length(tempArr)会得到这么一个 ...

  8. [BZOJ 1068] [SCOI2007] 压缩 【记忆化搜索】

    题目链接:BZOJ - 1068 题目分析 这种记忆化搜索(区间 DP) 之前就做过类似的,也是字符串压缩问题,不过这道题稍微复杂一些. 需要注意如果某一段是 S1S1 重复,那么可以变成 M + S ...

  9. 修改weblogic jvm启动参数

    进入: D:\Oracle\Middleware\user_projects\domains\base_domain\startWebLogic.cmd 在call 上一行增加: set USER_M ...

  10. JavaScript Client-Side

    JavaScript Client-Side GET, POST Events, Elements Template, Event, Data(MVC) XMLHttpRequest Logic (f ...