分段的概率DP+矩阵快速幂

                       Scout YYF I
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4180   Accepted: 1076

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

POJ Monthly Contest - 2009.08.23, Simon

如果不用快速幂(TLE的)。。。。。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5
 6 using namespace std;
 7
 8 int n,mine[15];
 9 double  p,d1,d2,d3,ans;
10
11 int main()
12 {
13     while(scanf("%d%lf",&n,&p)!=EOF)
14     {
15         for(int i=1;i<=n;i++)
16         {
17             scanf("%d",mine+i);
18         }
19         sort(mine,mine+1+n);
20         if(mine[1]==1)
21         {
22             printf("0.0000000\n"); continue;
23         }
24         else if(n==0)
25         {
26             printf("1.0000000\n"); continue;
27         }
28         bool flag=false;
29         for(int i=1;i<n;i++)
30         {
31             if(mine[i]+1==mine[i+1])
32             {
33                 printf("0.0000000\n"); flag=true; break;
34             }
35         }
36         if(flag==true) continue;
37         ans=1.;
38         for(int i=1;i<=n;i++)
39         {
40             int st=mine[i-1]+1,ed=mine[i];
41             d2=0.,d1=1.;
42             for(int j=st+1;j<=ed;j++)
43             {
44                 d3=d1*p+d2*(1-p);
45                 d2=d1; d1=d3;
46             }
47             ans*=(1-d3);
48         }
49         printf("%.7lf\n",ans);
50     }
51     return 0;
52 }

快速幂的。。。。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5
 6 using namespace std;
 7
 8 struct Matrix
 9 {
10     double a[2][2];
11     Matrix() {}
12     Matrix(double A,double B,double C,double D)
13     {
14         a[0][0]=A;a[0][1]=B;a[1][0]=C;a[1][1]=D;
15     }
16     Matrix operator* (const Matrix& b) const
17     {
18          Matrix temp;
19          memset(temp.a,0,sizeof(temp.a));
20          for(int i=0;i<2;i++)
21          {
22              for(int j=0;j<2;j++)
23              {
24                  for(int k=0;k<2;k++)
25                  {
26                      temp.a[i][j]+=a[i][k]*b.a[k][j];
27                  }
28              }
29          }
30          return temp;
31     }
32     Matrix Show()
33     {
34         for(int i=0;i<2;putchar(10),i++) for(int j=0;j<2;putchar(' '),j++) cout<<a[i][j];
35     }
36 };
37
38 Matrix QuickPow(Matrix m,int n)
39 {
40     Matrix E(1,0,0,1);
41     while(n>1)
42     {
43         if(n&1) E=E*m;
44         m=m*m;
45         n=n>>1;
46     }
47     E=E*m;
48     return E;
49 }
50
51 int n,mine[20];
52 double p,ans;
53
54 int main()
55 {
56     while(scanf("%d%lf",&n,&p)!=EOF)
57     {
58         for(int i=1;i<=n;i++)
59             scanf("%d",mine+i);
60         sort(mine,mine+n+1);
61         if(mine[1]==1)
62         {
63             printf("0.0000000\n"); continue;
64         }
65         else if(n==0)
66         {
67             printf("1.0000000\n"); continue;
68         }
69         bool flag=false;
70         for(int i=1;i<n;i++)
71         {
72             if(mine[i]+1==mine[i+1])
73             {
74                 printf("0.0000000\n"); flag=true; break;
75             }
76         }
77         if(flag==true) continue;
78         ans=1.;
79         for(int i=1;i<=n;i++)
80         {
81             Matrix m(p,1-p,1,0);
82             m=QuickPow(m,mine[i]-mine[i-1]-1);
83             ans*=1-m.a[0][0];
84         }
85         printf("%.7lf\n",ans);
86     }
87     return 0;
88 }

POJ 3744 Scout YYF I相关推荐

  1. POJ 3744(Scout YYF I )

    题意: 从1开始每次有p的概率往前跳一步,1-p的概率跳两步.给定n个点以及它们的坐标,若跳到这些点上则算失败,求安全经过这些点的概率. 分析:容易推出 dp[i] = dp[i-1]*p +  dp ...

  2. POJ 3744 Scout YYF I 期望dp

    给出n≤10n\leq10n≤10个地雷,它们的位置在[1,1e8][1,1e8][1,1e8]之间.然后一个人从111出发,有PPP的概率走一步,有1−P1-P1−P的概率走两步.求问安全离开的雷区 ...

  3. POJ 3744:Scout YYF I 概率DP+特征方程+快速幂

    Scout YYF I 题目链接: http://poj.org/problem?id=3744 题意: 有个人要到一个叫"mine road"的地方,路线是一条直线,起点在1,路 ...

  4. 【POJ - 3744】Scout YYF I(概率dp,矩阵快速幂优化dp)

    题干: 题目大意: 在一条不满地雷的路上(无限长),你现在的起点在1处.在N个点处布有地雷,1<=N<=10.地雷点的可能坐标范围:[1,100000000]. 每次前进p的概率前进一步, ...

  5. poj3744 Scout YYF I

    http://www.elijahqi.win/archives/3628 Description YYF is a couragous scout. Now he is on a dangerous ...

  6. 【POJ3744】Scout YYF I

    Description YYF是一个英勇的侦查员.现在他正在执行打入到敌方内部的危险任务.在解决了一系列的险情后,YYF到达了敌方著名的"地雷路"起始点.这条路非常长,上面被精心排 ...

  7. Scout YYF I

    /* 题意:一条路上有n个地雷,从1出发,每次可走一步或两步,走一步的概率为p,走两步的概率为1-p 思路:可以想到dp[i]=dp[i-2]*(1-p)+dp[i-1]*p,但是由于数据可能达到1e ...

  8. POJ3744 Scout YYF 题解

    题目链接 分析: 显然是一道概率DP 令fif_ifi​表示安全到达第iii格的概率,在不考虑地雷的情况下,显然有: f1=1,f2=p,fi=pfi−1+(1−p)fi−2(i>=3)f_1 ...

  9. 【原创】概率DP总结 by kuangbin

    概率DP主要用于求解期望.概率等题目. 转移方程有时候比较灵活. 一般求概率是正推,求期望是逆推.通过题目可以体会到这点. 首先先推荐几篇参考的论文: <信息学竞赛中概率问题求解初探> & ...

  10. 关于概率dp的个人理解与总结

    原文来自:http://blog.csdn.net/wdcjdtc/article/details/38424029 首先,概率dp主要解决的是关于概率问题和期望问题的求解. 难点和普通dp一样在于d ...

最新文章

  1. java旅游系统项目经验_谁能跟我介绍一下Java 项目经验,刚进入这个行业。
  2. cf英文名字格式好看的_cf英文名字大全_Michael、
  3. 专家:“十三五”中国应建立覆盖城乡的超级WIFI
  4. win10 如何配置 java jdk1.8环境变量(2017.8.17 )jdk1.8.0_144
  5. 分享实用监控脚本:使用Shell检查进程是否存在
  6. 编程语言的发展趋势及未来方向(1):历史回顾及趋势概述
  7. Oracle入门(十三A2)之单行函数
  8. java 读取list文本_【java基础】读取本地文件赋给Bean或list、Map
  9. 塞尔达传说gba_1986版塞尔达 回顾34年经典系列历代作品 满分最多系列游戏
  10. baseline_如何安装和使用Microsoft Baseline Security Analyzer(MBSA)
  11. velocity 时间显示 时间格式化 时间转化
  12. Tensor的合并与分割
  13. gis怎么通过水库划分子流域_分布式水文模型子流域划分方法
  14. Matlab图像练习程序:imrotate功能实现
  15. UOS统信系统任务栏不见解决方案
  16. SFP+光纤模块使用
  17. TypeError: not all arguments converted during string formatting
  18. 【原创】JQWidgets-TreeGrid 2、初探源码
  19. Lazy evaluation
  20. FFMPEG的像素格式

热门文章

  1. linux下chm文件制作,自己动手将在线文档制作成CHM文件
  2. java 遍历json串_Java遍历Json数据
  3. 搭建自己的wiki系统
  4. 论文复现:土卫六(Titan)大气参数计算
  5. php字符串分割函数,PHP字符串分割函数explode,strtok,str_split的用法
  6. 车型数据导入excel
  7. android screenshot流程,APP中,Screenshot的设计要领和各发布渠道的要求
  8. 闲谈安全测试之IAST
  9. 利用AD13设计PCB的问题总结11-20
  10. Matlab实现图像识别(十)