poj 3744 Scout YYF I(概率dp,矩阵优化)

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 0.5
2
2 0.5
2 4

Sample Output

0.5000000
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[20];
 15
 16 struct matrix
 17 {
 18     double mat[2][2];
 19     void init()
 20     {
 21         mat[0][0] = p;
 22         mat[0][1] = 1-p;
 23         mat[1][0] = 1;
 24         mat[1][1] = 0;
 25     }
 26 };
 27
 28 matrix mamul(matrix aa,matrix bb)
 29 {
 30     matrix c;
 31     for(int i = 0;i<2;i++)
 32     {
 33         for(int j = 0;j<2;j++)
 34         {
 35             c.mat[i][j] = 0;
 36             for(int k = 0;k<2;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>=1)
 48     {
 49         if(k&1)
 50             ans = mamul(ans,s);
 51         k = k>>1;
 52         s = mamul(s,s);
 53     }
 54     return ans;
 55 }
 56
 57 int main()
 58 {
 59     while(scanf("%d%lf",&n,&p)==2)
 60     {
 61             for(int i = 1;i<=n;i++)
 62                 scanf("%d",&num[i]);
 63             sort(num+1,num+n+1);
 64             num[0] = 0;
 65             if(num[1]==1) {puts("0.0000000"); continue;}
 66             matrix ans;
 67             ans.init();
 68             double out = 1;
 69             matrix tem;
 70             tem.mat[0][0] = (1-p)+p*p;
 71             tem.mat[0][1] = p;
 72             tem.mat[1][0] = p;
 73             tem.mat[1][1] = 1;
 74             int flag = 0;
 75             for(int i = 1;i<=n;i++)
 76             {
 77                 if(num[i]-num[i-1]<2)
 78                    {puts("0.0000000"); flag = 1; break;}
 79                 if(num[i]-num[i-1]-2==0)
 80                     out*=tem.mat[1][1];
 81                 else
 82                 {
 83                     ans.init();
 84                     ans = mul(ans,num[i]-num[i-1]-3);
 85                     matrix c;
 86                     for(int i = 0;i<2;i++)
 87                     {
 88                        for(int j = 0;j<2;j++)
 89                        {
 90                           c.mat[i][j] = 0;
 91                           for(int k = 0;k<2;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[1][1];
 97                 }
 98                 out*=(1-p);//cout<<out<<endl;
 99                 tem.mat[0][0] = (1-p)+p*p;
100                 tem.mat[0][1] = p;
101                 tem.mat[1][0] = p;
102                 tem.mat[1][1] = 1;
103             }
104             if(!flag)
105             printf("%.7f\n",out);
106     }
107     return 0;
108 }
时间: 2024-11-02 23:32:08

poj 3744 Scout YYF I(概率dp,矩阵优化)的相关文章

POJ 3744 Scout YYF I (概率DP+矩阵快速幂)

题意:小明要从1走过一段直线雷区,给定n个地雷的坐标,他走一步的概率是p,两步的概率为1-p,问你他能安全通过雷区的概率. 析:很明显这是一个概率DP,用d(i)表示到达 i 时他安全的概率,那么d[i] = p * d[i-1] + (1-p) * d[i-2];这个状态转移方程很好理解, 就是说要想到达 i 要么从第 i-1 走一步,要么从 i-2 走两步,最后加起来,然后问题来了,这个数可能达到 1E8,那么时间空间复杂度都受不了, 观察这个状态转移方程,是不是很像Fibnacci数列,所

poj 3744 Scout YYF I 概率dp+矩阵乘法

分析: dis(k,v1,v2)函数求到当前位置概率为v1,到当前位置之前一步的概率为v2,前进k步到达位置的概率,然后矩阵加速. 代码: //poj 3744 //sep9 #include <iostream> #include <algorithm> using namespace std; int pos[12]; double p,mat[4][4]; double ans[4][4]; void mul1() { double c[4][4]; c[0][1]=c[1]

POJ3744 Scout YYF I (概率DP + 矩阵优化)

题目链接: http://poj.org/problem?id=3744 题意: 有一段路,路上有n个陷阱,每一次只能向前走一步或者两步,求安全走过这段路的改路 分析: 设dp[i]表示安全走过第i个陷阱的概率 那么dp[i+1]=dp[i]*(1-p(走到第i+1个陷阱)) 因为每次只能走一步或者两步,所有安全走过第i个陷阱后的位置一定在a[i]+1;\ 其中a[i]表示第i个陷阱的位置 求从a[i]+1,走到a[i+1]的概率的时候我们需要用到矩阵来经行优化 ans[i]表示走到位置i的概率

[poj3744]Scout YYF I(概率dp+矩阵快速幂)

题意:在一维空间上存在一些雷,求安全通过的概率.其中人有$p$的概率前进一步,$1-p$的概率前进两步. 解题关键:若不考虑雷,则有转移方程:$dp[i] = p*dp[i - 1] + (1 - p)*dp[i - 2]$ 由于雷的数量很少,所以可以以雷为界,将区域分开,在每个区域中,通过该段的概率等于1-踩到该段终点的地雷的概率.然后用矩阵快速幂优化一下即可 1 #include<cstdio> 2 #include<cstring> 3 #include<algorit

POJ 3744 Scout YYF I 矩阵快速幂

Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4452   Accepted: 1159 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,

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

题目链接:http://poj.org/problem?id=3744 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"

POJ 3744 Scout YYF I 矩阵快速幂优化--概率dp

点击打开链接 Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5416   Accepted: 1491 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 diffic

poj 3744 Scout YYF I(矩阵优化概率DP)

Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5153   Accepted: 1404 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,

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

Scout YYF I 题目连接: http://poj.org/problem?id=3744 题意: 有个人要到一个叫“mine road”的地方,路线是一条直线,起点在1,路上有N个地雷,坐标在[1, 100000000]之间,这人每次有p(0.25≤p≤0.75)的概率向前走一步,且有1-p的概率向前走两步,问这货安全通过雷区到达"mine road"的概率 题解: 利用特征方程求出通项表达式,要走过有雷的地方f(n+1)=f(n-1)*(1-p).   PS:也可以用矩阵快速