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

Sample Output

  1. 0.5000000
  2. 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] ,构造矩阵为

  1. #include <iostream>
  2. #include<stdio.h>
  3. #include<string.h>
  4. #include<math.h>
  5. #include<algorithm>
  6. using namespace std;
  7. int s[20];
  8. double q,p;
  9. struct node
  10. {
  11. double dp[2][2];//矩阵
  12. };
  13. node mult(node a,node b)//矩阵乘法
  14. {
  15. int i,j,n,k;
  16. node temp;
  17. for(i=0;i<2;i++)
  18. {
  19. for(j=0;j<2;j++)
  20. {
  21. temp.dp[i][j]=0;
  22. for(k=0;k<2;k++)
  23. temp.dp[i][j]+=a.dp[i][k]*b.dp[k][j];
  24. }
  25. }
  26. return temp;
  27. }
  28. node cal(int N)//快速幂
  29. {
  30. node a,res;
  31. a.dp[0][0]=p;
  32. a.dp[0][1]=q;
  33. a.dp[1][0]=1;
  34. a.dp[1][1]=0;
  35. res.dp[0][0]=1;
  36. res.dp[0][1]=0;
  37. res.dp[1][0]=0;
  38. res.dp[1][1]=1;
  39. while(N)
  40. {
  41. if(N&1)
  42. {
  43. res=mult(res,a);
  44. }
  45. a=mult(a,a);
  46. N>>=1;
  47. }
  48. return res;
  49. }
  50. int main()
  51. {
  52. int i,j,n,m,max,flag;
  53. double tempqn;
  54. node temp,a;
  55. while(scanf("%d%lf",&n,&p)!=EOF)
  56. {
  57. q=1-p;
  58. s[0]=0;
  59. for(i=1;i<=n;i++)
  60. {
  61. cin>>s[i];
  62. }
  63. sort(s+1,s+1+n);
  64. for(i=1,flag=1;i<n;i++)
  65. {
  66. if(s[i]+1==s[i+1])
  67. flag=0;
  68. }
  69. if(!flag||s[1]==1)
  70. {
  71. puts("0.0000000");
  72. continue;
  73. }
  74. a.dp[0][0]=1;
  75. a.dp[0][1]=0;
  76. a.dp[1][0]=0;
  77. a.dp[1][1]=0;
  78. for(i=1;i<=n;i++)
  79. {
  80. temp=cal(s[i]-s[i-1]-2);
  81. a=mult(temp,a);
  82. a.dp[0][0]=a.dp[0][0]*q;
  83. a.dp[0][1]=0;
  84. a.dp[1][0]=0;
  85. a.dp[1][1]=0;
  86. }
  87. printf("%.7f\n",a.dp[0][0]);
  88. }
  89. return 0;
  90. }

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. Eclipse vs IDEA快捷键对比大全(win系统)

    花了几天时间熟悉IDEA的各种操作,将各种快捷键都试了一下,感觉很是不错! 以下为我整理了一下开发过程中经常用的一些Eclipse快捷键与IDEA的对比,方便像我一样使用Eclipse多年但想尝试些改 ...

  2. Nginx配置同一个域名http与https两种方式都可访问

    ##配置 http://test.pay.joyhj.com https://test.pay.joyhj.com 两者都可访问 # vim /usr/local/nginx/conf/vhost/t ...

  3. OC - 29.自定义布局实现瀑布流

    概述 瀑布流是电商应用展示商品通常采用的一种方式,如图示例 瀑布流的实现方式,通常有以下几种 通过UITableView实现(不常用) 通过UIScrollView实现(工作量较大) 通过UIColl ...

  4. iOS远程消息推送

    iOS 推送基础知识 Apple 使用公共密钥数字证书对来自 iOS 应用程序的推送请求进行身份验证,所以您首先需要创建身份验证密钥,并向 Apple 注册它们.我将在下一节中花相当长的篇幅来直接介绍 ...

  5. Tomcat 8熵池阻塞变慢详解(转)

    Tomcat 8熵池阻塞变慢详解 作者:chszs,转载需注明.博客主页:http://blog.csdn.net/chszs Tomcat 8启动很慢,且日志上无任何错误,在日志中查看到如下信息: ...

  6. 谈谈 jQuery 中的防冲突(noConflict)机制

    许多的 JS 框架类库都选择使用 $ 符号作为函数或变量名,jQuery 是其中最为典型的一个.在 jQuery 中,$ 符号只是 window.jQuery 对象的一个引用,因此即使 $ 被删除,w ...

  7. DEDE更改版权信息

    DEDECMSV5.7版本出现后,在前台网页底部会出现织梦版权信息 “powered by  dedecms”,很多人都不知道怎么去掉 1. 方法一: 在include/dedesql.classs. ...

  8. PHP面向对象(OOP):把对象串行化serialize()方法,__sleep()方法,__wakeup()方法

    有时候需要把一个对象在网络上传输,为了方便传输,可以把整个对象转化为二进制串,等到达另一端时,再还原为原来的对象,这个过程称之为串行化(也叫序列化), 就像我们现在想把一辆汽车通过轮船运到美国去,因为 ...

  9. Day8 面向对象(补充)

    私有字段 class Foo: def __init__(self, name): self.__name = name def f1(self): print(self.__name) class ...

  10. 【C语言】字符集和词汇

    C语言字符集和词汇 一.相关基础知识 字符是组成语言的最基本的元素 词汇,又称语汇,是一种语言里所有的(或特定范围的)词和固定短语的总和 二.具体内容 C语言字符集由:字母.数字.空格.标点和特殊字符 ...