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

31 篇文章 0 订阅

Scout YYF I
http://poj.org/problem?id=3744
Time Limit: 1000MS Memory Limit: 65536K
   

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


题目大意:在一条很长的路上,有n个雷,给出每个雷的坐标,以及一个人每次走一步的概率p,走两步的概率为1-p,初始在点1处,求安全走过这段路的概率?


大致思路:设dp[i]表示安全到达点i处的概率,则状态转移方程为:

①点i处有雷,dp[i]=0;

②点i出无雷,dp[i]=dp[i-1]*p+dp[i-2]*(1-p);

由于雷坐标的范围很大,而雷的数量很少,所以需要用矩阵快速幂对无雷的路段进行优化


#include <cstdio>
#include <algorithm>

using namespace std;

const int MAXN=2;

int n,index[13];
double p,ans;

struct Matrix{
    double  m[MAXN][MAXN];
}ORI;

Matrix I = {1,0,
            0,1};

Matrix matrixmul(const Matrix& a,const Matrix& b) {//矩阵乘法
    int i,j,k;
    Matrix c;
    for (i = 0 ; i < MAXN; i++)
        for (j = 0; j < MAXN;j++) {
            c.m[i][j] = 0;
            for (k = 0; k < MAXN; k++)
                c.m[i][j] += a.m[i][k] * b.m[k][j];
        }
    return c;
}

Matrix quickpow(int n) {
    Matrix m = ORI, b = I;
    while (n >= 1) {
        if (n & 1)
            b = matrixmul(b,m);
        n = n >> 1;
        m = matrixmul(m,m);
    }
    return b;
}

int main() {
    while(2==scanf("%d%lf",&n,&p)) {
        ORI.m[0][0]=p;
        ORI.m[0][1]=1;
        ORI.m[1][0]=1-p;
        ORI.m[1][1]=0;
        for(int i=1;i<=n;++i) {
            scanf("%d",index+i);
        }
        index[0]=0;
        sort(index,index+n+1);
        ans=1;//ans表示到达index[i]+1处的概率
        for(int i=1;i<=n;++i) {
            if(index[i]!=index[i-1]) {//如果两个雷不在同一点
                if(index[i]-index[i-1]==1) {//如果两个雷相邻,则无法安全通过
                    ans=0.00000001;
                    break;
                }
                ans*=quickpow(index[i]-index[i-1]-2).m[0][0];//算出安全到达index[i]-1的概率
                ans*=1-p;//算出安全到达index[i]+1的概率
            }
        }
        printf("%.7lf\n",ans);
    }
    return 0;
}


但是看到别人的题解基本都这样写:

        for(int i=1;i<=n;++i) {
            if(index[i]!=index[i-1]) {//如果两个雷不在同一点
                ans*=(1-quickpow(index[i]-index[i-1]-1).m[0][0]);//直接算出安全到达index[i]+1的概率
            }
        }

表示不明白如何推出来(只会将p带入方程,计算推出n较小时成立),估计很简单,大家都没有说明是怎么来的...
终于还是找到一个差不多的解释:将 index[i-1]+1~index[i]-1看成一个点,则下一步只可能到达index[i]或index[i]+1,则:dp[index[i]+1]=1-dp[index[i]]

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值