POJ 3744 Scout YYF I (矩阵相乘+概率DP)

£神魔★判官ぃ 2022-08-21 12:00 227阅读 0赞

POJ 3744 Scout YYF I (矩阵相乘+概率DP):http://poj.org/problem?id=3744

题面:














Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7254   Accepted: 2118

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

题目大意:

一条路上有n个雷,走一步的可能性是p,走两步的可能性是1-p,起点为1,求安全抵达n的概率。

题目分析:

矩阵相乘的题目,

如果k号位有雷,那么安全通过这个雷只可能是在k-1号位选择走两步到k+1号位。因此,可以得到如下结论:在第i个雷的被处理掉的概率就是从a[i-1]+1号位到a[i]号位的概率。于是,可以用1减去就可以求出安全通过第i个雷的概率,最后乘起来即可,但是由于数据很大,所以需要用到矩阵快速幂,类似斐波那契数列,有ans[i]=p*ans[i-1]+(1-p)*ans[i-2],构造矩阵为:

0_1299141758q3uo.gif

代码实现:

  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <algorithm>
  4. using namespace std;
  5. int n;
  6. double p;
  7. int a[20];
  8. double b[5][5];
  9. double s[5];
  10. double tmp[5][5];
  11. double temp[5];
  12. double Count(int t)
  13. {
  14. b[0][0]=p;
  15. b[0][1]=1-p;
  16. b[1][0]=1;
  17. b[1][1]=0;
  18. s[0]=1;
  19. s[1]=0;
  20. while(t!=0)
  21. {
  22. if(t%2==1)
  23. {
  24. for(int i=0;i<2;i++)
  25. {
  26. temp[i]=0;
  27. for(int j=0;j<2;j++)
  28. {
  29. temp[i]+=b[i][j]*s[j];
  30. }
  31. }
  32. for(int i=0;i<2;i++)
  33. {
  34. s[i]=temp[i];
  35. }
  36. }
  37. t/=2;
  38. for(int i=0;i<2;i++)
  39. {
  40. for(int j=0;j<2;j++)
  41. {
  42. tmp[i][j]=0;
  43. for(int k=0;k<2;k++)
  44. {
  45. tmp[i][j]+=b[i][k]*b[k][j];
  46. }
  47. }
  48. }
  49. for(int i=0;i<2;i++)
  50. {
  51. for(int j=0;j<2;j++)
  52. {
  53. b[i][j]=tmp[i][j];
  54. }
  55. }
  56. }
  57. return s[0];
  58. }
  59. int main()
  60. {
  61. double ans=0;
  62. while(scanf("%d%lf",&n,&p)!=EOF)
  63. {
  64. for(int i=1;i<=n;i++)
  65. {
  66. scanf("%d",&a[i]);
  67. }
  68. a[0]=0;
  69. sort(a,a+n+1);
  70. ans=1;
  71. for(int i=0;i<n;i++)
  72. {
  73. ans=ans*(1-Count(a[i+1]-a[i]-1));
  74. }
  75. printf("%.7lf\n",ans);
  76. }
  77. return 0;
  78. }

发表评论

表情:
评论列表 (有 0 条评论,227人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Poj 3071 Football (比赛对阵 概率DP)

    题意:2^n个队进行足球赛,给出队伍间比赛获胜概率的矩阵。求最后胜利的概率最大的是哪只球队。 思路:这题最关键的是找出每轮对阵队伍标号间的关系。 设i表示比赛正在进行第几轮

    相关 poj2096 概率dp入门

    题意: 一个系统有s个子系统,一共会产生n中bug。某人一天可以发现一个bug,这个bug属于一个子系统,属于一个种类,每个bug属于某个子系统的概率是1/s,属于某个分类的