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

Scout YYF I POJ - 3744

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个炸弹在不同的位置,你在1点,往前跳一格的概率是p,跳两格的概率是1-p,你能跳出这些炸弹的概率是多少,

  • 可以得出递推公式dp[i] = dp[i-1]* p+dp[i-2] * (1-p),dp[炸弹位置] = 0,结果求dp[最后一个炸弹位置+1] = dp[最后一个炸弹位置-1]*(1-p),可是最后一个炸弹位置太大,用矩阵快速幂加速
    [ d p [ i ] d p [ i − 1 ] ] = [ p 1 − p 1 0 ] [ d p [ i ] d p [ i − 1 ] ] \left[ \begin{matrix} dp[i] \\ dp[i-1] \end{matrix} \right]= \left[ \begin{matrix} p & 1-p \\ 1 & 0 \end{matrix} \right] \left[ \begin{matrix} dp[i] \\ dp[i-1] \end{matrix} \right] [dp[i]dp[i1]]=[p11p0][dp[i]dp[i1]]
  • 因为矩阵乘法满足a*(b*c) = a * b *c,所以 [ d p [ n + 1 ] d p [ n ] ] = [ p 1 − p 1 0 ] 的 n 次 方 ∗ [ d p [ 1 ] d p [ 0 ] ] \left[ \begin{matrix} dp[n+1] \\ dp[n] \end{matrix} \right]= \left[ \begin{matrix} p & 1-p \\ 1 & 0 \end{matrix} \right] 的n次方* \left[ \begin{matrix} dp[1] \\ dp[0] \end{matrix} \right] [dp[n+1]dp[n]]=[p11p0]n[dp[1]dp[0]]
  • 每次用快速幂计算到下一个有炸弹的位置,然后让dp[当前位置] = 0,再算下一个位置
#include<iostream>
#include<cstdio>
#include<iomanip>
#include<cstring>
#include<algorithm>
using namespace std;
class node
{
public:

    double mar[3][3];
    node()
    {
        memset(mar,0,sizeof(mar));
    }
};
long long a[120];

node mul(node a,node b)
{
    node c;

    for(long long i = 0; i<2; i++)
    {
        for(long long j = 0; j<2; j++)
        {
            for(long long k = 0; k<2; k++)
            {
                c.mar[i][j] += a.mar[i][k]*b.mar[k][j];
            }
        }
    }
    return c;
}
node qui_mul(node a,long long num)
{
    node ans;
    ans.mar[0][0] = ans.mar[1][1] = 1;
    while(num)
    {
        if(num&1)
        {
            ans = mul(ans,a);
        }
        num>>=1;
        a = mul(a,a);
    }
    return ans;
}

int main()
{
    ios::sync_with_stdio(false);
    long long i,j,m,n,now;
    double p;
    while(scanf("%lld %lf",&n,&p)!=EOF)
    {
        node aa,ans,bb;
        for(i = 0; i<n; i++)
        {
            cin>>a[i];
        }
        sort(a,a+n);
        aa.mar[0][0] = p;
        aa.mar[0][1] = 1-p;
        aa.mar[1][0] = 1;

        ans.mar[0][0] =1;
        now = 1;
        for(i = 0; i<n; i++)
        {
            long long num = a[i]-now;
            bb = qui_mul(aa,num);
            ans = mul(bb,ans);
            ans.mar[0][0] = 0;
            now = a[i];
        }
        cout<<fixed<<setprecision(7)<<ans.mar[1][0]*(1-p)<<endl;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值