POJ 3744 概率dp+矩阵快速幂

本文介绍了一个经典的概率计算问题——YYF如何安全穿越敌方布满地雷的道路。利用矩阵快速幂的方法来高效计算YYF成功穿越的概率,并通过分段处理含有地雷的位置,最终得出解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Language:
Scout YYF I
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9685 Accepted: 2826
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

分析:不难看出,f[i]=p* f[i-1]+(1-p)*f[i-2],这个地推加上1e9的范围立马应该想到矩阵快速幂,于是问题就简单化了。但我们可以发现有些位置有地雷是不合法的,于是考虑分段,把地雷分成1~mines1,mines1~mines2……这样分别可以算出每一段的概率,而最后通过一段的概率就等于到达1-xi,xi等于到达xi的概率。

# include <iostream>
# include <cstdio>
# include <cmath>
# include <list>
# include <cstring>
# include <map>
# include <ctime>
# include <algorithm>
# include <queue>
using namespace std;
typedef long long ll;
int read(){
    int f=1,i=0;char ch=getchar();
    while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') {i=(i<<3)+(i<<1)+ch-'0';ch=getchar();}
    return f*i;
}
double p,a[5][5],b[5][5],res[5][5],tmp[5][5],Ans;
int mines[15],n;
inline void MUL(double a[][5],double b[][5])
{
    memset(tmp,0,sizeof(tmp));
    for(int i=0;i<=1;++i)
        for(int j=0;j<=1;++j)
            for(int k=0;k<=1;++k)
                tmp[i][j]=(tmp[i][j]+a[i][k]*b[k][j]);
    for(int i=0;i<=1;++i)
        for(int j=0;j<=1;++j) a[i][j]=tmp[i][j];
}
inline void ksm(int t)
{
    memset(res,0,sizeof(res));
    for(int i=0;i<=1;++i) res[i][i]=1;
    memcpy(a,b,sizeof(a));
    while(t)
    {
        if(t&1) MUL(res,a);
        t>>=1;
        MUL(a,a);
    }
    Ans*=(1-res[0][0]);
}
int main()
{
    freopen("lx.in","r",stdin);
    while(scanf("%d%lf",&n,&p)!=EOF)
    {
        for(int i=1;i<=n;++i) mines[i]=read();
        sort(mines+1,mines+n+1);
        a[1][1]=0,a[0][1]=1-p,a[1][0]=1,a[0][0]=p;
        b[1][1]=0,b[0][1]=1-p,b[1][0]=1,b[0][0]=p;
        Ans=1;ksm(mines[1]-1);
        for(int i=2;i<=n;++i)
        {
            if(mines[i]==mines[i-1]) continue;
            ksm(mines[i]-mines[i-1]-1);
        }
        printf("%0.7f\n",Ans);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值