题目链接:http://poj.org/problem?id=3744

题意:

  有n个地雷,位置为pos[i]。

  在每个位置,你向前走一步的概率为p,向前走两步的概率为1-p。

  你的初始位置为1。

  问你通过雷区的概率。

题解:

  表示状态:

    dp[i] = probability moving to i

    表示走到i的概率

  找出答案:

    ans = dp[last_mine+1]

    last_mine:最右边一颗雷的位置

  如何转移:

    dp[i] = dp[i-1] * p + dp[i-2] * (1-p)

    if(i is a mine) dp[i] = 0

    对于位置i,有可能是从i-1走来的,也有可能是从i-2走来的。

    加法原理。

  边界条件:

    dp[1] = 1

    初始位置为1。

  优化:

    矩阵快速幂。

    对于某一段没有地雷的区间,是满足矩阵快速幂的(只用到递推式,dp不改为0)。

    所以分段进行矩阵快速幂。

    

    将雷区划分为n段:

    1~pos[1], pos[1]+1~pos[2], pos[2]+1~pos[3]...

    

    容斥原理:P(通过某一段雷区) = 1 - P(踩到最右边的雷)

    乘法原理:P(通过总雷区) = ∏ P(通过每一段雷区)

    矩阵格式:

    

    初始矩阵:

    

    特殊矩阵:

    

AC Code:

  1. // state expression:
  2. // dp[i] = probability moving to i
  3. //
  4. // find the answer:
  5. // dp[last mine + 1]
  6. //
  7. // transferring:
  8. // dp[i] = dp[i-1] * p + dp[i-2] * (1-p)
  9. //
  10. // boundary:
  11. // dp[1] = 1
  12. // others = 0
  13. //
  14. // optimization:
  15. // quick pow for matrix
  16. // from x to y
  17. // res = start * special ^ (y-x)
  18. // dp[i] = res.val[0][0]
  19. #include <iostream>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <algorithm>
  23. #define MAX_N 15
  24. #define MAX_L 5
  25.  
  26. using namespace std;
  27.  
  28. struct Mat
  29. {
  30. int n;
  31. int m;
  32. double val[MAX_L][MAX_L];
  33. Mat()
  34. {
  35. n=;
  36. m=;
  37. memset(val,,sizeof(val));
  38. }
  39. void print_mat()
  40. {
  41. for(int i=;i<n;i++)
  42. {
  43. for(int j=;j<m;j++)
  44. {
  45. cout<<val[i][j]<<" ";
  46. }
  47. cout<<endl;
  48. }
  49. cout<<endl;
  50. }
  51. };
  52.  
  53. int n;
  54. int pos[MAX_N];
  55. double p;
  56. double ans;
  57.  
  58. Mat make_unit(int k)
  59. {
  60. Mat mat;
  61. mat.n=k;
  62. mat.m=k;
  63. for(int i=;i<k;i++)
  64. {
  65. mat.val[i][i]=;
  66. }
  67. return mat;
  68. }
  69.  
  70. Mat make_start()
  71. {
  72. Mat mat;
  73. mat.n=;
  74. mat.m=;
  75. mat.val[][]=;
  76. mat.val[][]=;
  77. return mat;
  78. }
  79.  
  80. Mat make_special()
  81. {
  82. Mat mat;
  83. mat.n=;
  84. mat.m=;
  85. mat.val[][]=;
  86. mat.val[][]=-p;
  87. mat.val[][]=;
  88. mat.val[][]=p;
  89. return mat;
  90. }
  91.  
  92. Mat mul_mat(const Mat &a,const Mat &b)
  93. {
  94. Mat c;
  95. if(a.m!=b.n)
  96. {
  97. cout<<"Error: mul_mat"<<endl;
  98. return c;
  99. }
  100. c.n=a.n;
  101. c.m=b.m;
  102. for(int i=;i<a.n;i++)
  103. {
  104. for(int j=;j<b.m;j++)
  105. {
  106. for(int k=;k<a.m;k++)
  107. {
  108. c.val[i][j]+=a.val[i][k]*b.val[k][j];
  109. }
  110. }
  111. }
  112. return c;
  113. }
  114.  
  115. Mat quick_pow_mat(Mat mat,long long k)
  116. {
  117. Mat ans;
  118. if(mat.n!=mat.m)
  119. {
  120. cout<<"Error: quick_pow_mat"<<endl;
  121. return ans;
  122. }
  123. ans=make_unit(mat.n);
  124. while(k)
  125. {
  126. if(k&)
  127. {
  128. ans=mul_mat(ans,mat);
  129. }
  130. mat=mul_mat(mat,mat);
  131. k>>=;
  132. }
  133. return ans;
  134. }
  135.  
  136. void read()
  137. {
  138. pos[]=;
  139. for(int i=;i<=n;i++)
  140. {
  141. cin>>pos[i];
  142. }
  143. }
  144.  
  145. void solve()
  146. {
  147. sort(pos+,pos++n);
  148. Mat special=make_special();
  149. ans=;
  150. for(int i=;i<=n;i++)
  151. {
  152. Mat start=make_start();
  153. Mat res=mul_mat(start,quick_pow_mat(special,pos[i]-pos[i-]));
  154. ans*=(-res.val[][]);
  155. }
  156. }
  157.  
  158. void print()
  159. {
  160. printf("%.7f\n",ans);
  161. }
  162.  
  163. int main()
  164. {
  165. while(cin>>n>>p)
  166. {
  167. read();
  168. solve();
  169. print();
  170. }
  171. }

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 I(概率dp,矩阵优化)

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

  3. poj 3744 Scout YYF 1 (概率DP+矩阵快速幂)

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

  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. 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 ...

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

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

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

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

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

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

随机推荐

  1. CentOS下配置iptables防火墙 linux NAT(iptables)配置

    CentOS下配置防火墙 配置nat转发服务CentOS下配置iptables防火墙 linux NAT(iptables)配置 CentOS下配置iptables 1,vim /etc/syscon ...

  2. jquery相冊图片来回选择

    <!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <script sr ...

  3. HDU 4355 Party All the Time(三分|二分)

    题意:n个人,都要去參加活动,每一个人都有所在位置xi和Wi,每一个人没走S km,就会产生S^3*Wi的"不舒适度",求在何位置举办活动才干使全部人的"不舒适度&quo ...

  4. 【Python】ModuleNotFoundError: No module named 'matplotlib.pyplot'

    安装好matplotlib后,很激动的建立了一个文件夹matplotlib,并且在其下面建立了,mpl_squraes.py文件,代码编辑完成以后,点击运行,报错如下: 仔细分析了之后,发现是文件夹名 ...

  5. .net 网站登录

    如何实现,按回车键,自动登录,在相应控件上添加onkeypress事件 function CheckCodePress(e){ var e = e||window.event if (e.keyCod ...

  6. UIView属性的动画

    //标记着动画块的开始,第一个参数表示动画的名字,起到标识作用 [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDurat ...

  7. QTreeWidget 的用法

    Qt QTreeWidget 新建一个Qt Widgets Application,拖拽一个Tree Widget 到 ui 界面上,最后实现的效果如下: 添加代码 //test.h //在头文件里添 ...

  8. request获取数据的几种方法

    1.request.getparameter(); String value=request.getparameter("key"); 2.request.getParameter ...

  9. 多通道 移位寄存器 verilog

    // Quartus II Verilog Template // Basic 64-stage shift register with multiple taps module basic_shif ...

  10. Windows 10 1703创意者更新官方ISO镜像大全

    2017年04月07日 20:00 19867 次阅读 稿源:快科技 12 条评论 Windows 10 Creators Update创意者更新正式版已经发布,目前只能通过易生.MCT工具或者ISO ...