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

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. 1 0.5
  2. 2
  3. 2 0.5
  4. 2 4

Sample Output

  1. 0.5000000
  2. 0.2500000
题意:在一条不满地雷的路上,你现在的起点在1处。在N个点处布有地雷,1<=N<=10。地雷点的坐标范围:[1,100000000].
每次前进p的概率前进一步,1-p的概率前进1-p步。问顺利通过这条路的概率。就是不要走到有地雷的地方。
 
设dp[i]表示到达i点的概率,则 初始值 dp[1]=1.
很容易想到转移方程: dp[i]=p*dp[i-1]+(1-p)*dp[i-2];
但是由于坐标的范围很大,直接这样求是不行的,而且当中的某些点还存在地雷。
 
N个有地雷的点的坐标为 x[1],x[2],x[3]```````x[N].
我们把道路分成N段:
1~x[1];
x[1]+1~x[2];
x[2]+1~x[3];
`
`
`
x[N-1]+1~x[N].
 
这样每一段只有一个地雷。我们只要求得通过每一段的概率。乘法原理相乘就是答案。

具体来说,对于某一段长度为nk的线段k,设a是线段k的开头,b是线段k的结尾,nk=a-b-1,

到达a的概率设为1,到达a+1概率是p,到达a+2的概率就是Pa*(1-p)+Pa+1*p,这样就可以递推了。

由于Pam=Pam-1*p+Pam-2*(1-p),即推的公式都是一样的,可以用矩阵乘法+快速幂来做。

问题的解可以看做Pn1*(1-p)*Pn2*(1-p)*....Pnn*(1-p),因为最后一个陷阱要跳过去才安全。

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<vector>
  5. #include<algorithm>
  6. #include<cmath>
  7. #define M(a,b) memset(a,b,sizeof(a))
  8. typedef long long LL;
  9.  
  10. using namespace std;
  11.  
  12. int n;
  13. double p;
  14. int num[];
  15.  
  16. struct matrix
  17. {
  18. double mat[][];
  19. void init()
  20. {
  21. mat[][] = p;
  22. mat[][] = -p;
  23. mat[][] = ;
  24. mat[][] = ;
  25. }
  26. };
  27.  
  28. matrix mamul(matrix aa,matrix bb)
  29. {
  30. matrix c;
  31. for(int i = ;i<;i++)
  32. {
  33. for(int j = ;j<;j++)
  34. {
  35. c.mat[i][j] = ;
  36. for(int k = ;k<;k++)
  37. c.mat[i][j]+=(aa.mat[i][k]*bb.mat[k][j]);
  38. }
  39. }
  40. return c;
  41. }
  42.  
  43. matrix mul(matrix s, int k)
  44. {
  45. matrix ans;
  46. ans.init();
  47. while(k>=)
  48. {
  49. if(k&)
  50. ans = mamul(ans,s);
  51. k = k>>;
  52. s = mamul(s,s);
  53. }
  54. return ans;
  55. }
  56.  
  57. int main()
  58. {
  59. while(scanf("%d%lf",&n,&p)==)
  60. {
  61. for(int i = ;i<=n;i++)
  62. scanf("%d",&num[i]);
  63. sort(num+,num+n+);
  64. num[] = ;
  65. if(num[]==) {puts("0.0000000"); continue;}
  66. matrix ans;
  67. ans.init();
  68. double out = ;
  69. matrix tem;
  70. tem.mat[][] = (-p)+p*p;
  71. tem.mat[][] = p;
  72. tem.mat[][] = p;
  73. tem.mat[][] = ;
  74. int flag = ;
  75. for(int i = ;i<=n;i++)
  76. {
  77. if(num[i]-num[i-]<)
  78. {puts("0.0000000"); flag = ; break;}
  79. if(num[i]-num[i-]-==)
  80. out*=tem.mat[][];
  81. else
  82. {
  83. ans.init();
  84. ans = mul(ans,num[i]-num[i-]-);
  85. matrix c;
  86. for(int i = ;i<;i++)
  87. {
  88. for(int j = ;j<;j++)
  89. {
  90. c.mat[i][j] = ;
  91. for(int k = ;k<;k++)
  92. c.mat[i][j]+=(ans.mat[i][k]*tem.mat[k][j]);
  93. }
  94. }
  95. //cout<<c.mat[1][1]<<'!'<<endl;
  96. out*=c.mat[][];
  97. }
  98. out*=(-p);//cout<<out<<endl;
  99. tem.mat[][] = (-p)+p*p;
  100. tem.mat[][] = p;
  101. tem.mat[][] = p;
  102. tem.mat[][] = ;
  103. }
  104. if(!flag)
  105. printf("%.7f\n",out);
  106. }
  107. return ;
  108. }

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. poj3744 Scout YYF I[概率dp+矩阵优化]

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

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

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

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

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

  6. POJ 3744 Scout YYF I

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

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

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

  8. POJ-3744 Scout YYF I 概率DP

    题目链接:http://poj.org/problem?id=3744 简单的概率DP,分段处理,遇到mine特殊处理.f[i]=f[i-1]*p+f[i-2]*(1-p),i!=w+1,w为mine ...

  9. hdu 4576(简单概率dp | 矩阵优化)

    艰难的一道题,体现出菜菜的我... 首先,先吐槽下. 这题到底出题人是怎么想的,用普通概率dp水过??? 那为什么我概率dp写的稍微烂点就一直tle?  感觉很不公平.大家算法都一致,因为我程序没有那 ...

随机推荐

  1. c#自适应窗体的实现

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...

  2. GridView控件RowDataBound事件的一个实例

    实现点击两个按钮,跳转到同一个界面,HyperLink显示不同的东西,主要代码段如下 前台代码: <asp:TemplateField HeaderText="操作"> ...

  3. 如何判断ios设备中是否安装了某款应用

    URL Schemes关键字研究一下即可 常见得URL Schemes见http://www.cnblogs.com/huangzs/p/4491286.html if ([[UIApplicatio ...

  4. 高可用与负载均衡(8)之聊聊 LVS重要参数和优化以及监控

    preface 在明白LVS-DR模式的部署之后,我们看看LVS的几个重要参数: 如有问题,请联系我18500777133@sina.cn [root@localhost ~]# ls /proc/s ...

  5. 真机调试之android手机+chrome

    真机调试之android手机+chrome 虽然chrome上的移动设备模拟器很强大,但是在真机运行的时候,总会遇到一些小问题,这时就需要使用真机调试了. 第一步:准备一台android手机,并在手机 ...

  6. MVC过滤器之 OnActionExcuted

    Controller里 [SendMessage] public Action SendSmsMessage() { var resultExtendInfo=new ResultExtendInfo ...

  7. 利用百度开发者中心的api实现地图及周边的搜索

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  8. Android项目结构 以及体系结构

    学习Android平台的人一般对Android的平台的应该有点认识 其它的就不多讲了 Android项目一般由以下几个部分构成 以上是一个简单的Android项目结构目录图 1. src  主要是 源 ...

  9. MySQL学习笔记——安装及配置环境

    1.安装的版本为mysql-5.6.24-win32.1432006610压缩版 查看教程http://jingyan.baidu.com/article/f3ad7d0ffc061a09c3345b ...

  10. Cache-Aside Pattern解析

    使用这种模式,可以帮助我们维护Cache中的数据. 使用Cache容易遇到的问题: 使用缓存,主要是为了将一些重复访问的数据存到缓存,开发者希望缓存中的数据和数据源中的保持一致,这就需要程序中有相应的 ...