poj 3744 Scout YYF 1 (概率DP+矩阵快速幂)

F - Scout YYF I
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit  Status

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个陷阱,每一次只能向前走一步或者两步,求安全走过这段路的改路

 

分析:

设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的概率

ans[i] = p*ans[i-1]+(1-p)*ans[i-2];

ans[0]=0,ans[1]=1;

都说G++比C++要好,但是本题最好用C++提交。

如果用G++提交,浮点型输出一定要用%f,否则会WA。

这是因为G++标准的浮点型输出用%f,而不是%lf。

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <string.h>
using namespace std;
const int maxn=15;
int cnt[maxn];
double dp[maxn];
double p;
int n;
struct matrix
{
    double data[2][2];
};
matrix I={1,0,0,1};
matrix multi(matrix a,matrix b)
{
    matrix c;
    memset(c.data,0,sizeof(c.data));
    for(int i=0;i<2;i++)
    for(int j=0;j<2;j++)
    for(int k=0;k<2;k++)
    c.data[i][j]+=a.data[i][k]*b.data[k][j];
    return c;
}
double pow(matrix a,int b)
{
    matrix ans=I;
    while(b)
    {
        if(b&1)
        {
            ans=multi(ans,a);
            b--; //减比不减更好 虽然都对 但是减了更好理解一点
        }
        b>>=1;
        a=multi(a,a);
    }
    return ans.data[0][0];
}
int main()
{
    while(cin>>n>>p)
    {
        memset(dp,0,sizeof(dp)); //该初始化的地方一定要初始化
        cnt[0]=0;  //为第一个雷做准备的
        for(int i=1;i<=n;i++)
            cin>>cnt[i];
        sort(cnt,cnt+n+1);
        dp[0]=1.0; //不是0
        matrix a={p,1-p,1,0}; //初始化矩阵,注意这个的推导过程
        for(int i=1;i<=n;i++)
        //要1减去这个概率 因为这个概率是踩上雷的
        dp[i]=dp[i-1]*(1-pow(a,cnt[i]-cnt[i-1]-1));  //注意不要丢括号
        //注意是乘法不是加法 把每段概率乘起来
        //注意是-1不是+1
        //从上一个雷的下一个起 要走cnt[i]-cnt[i-1]-1步 才能到达下一个雷
        //雷在1和5 从2走到5 需要3步 5-1-1=3
        printf("%.7f\n",dp[n]);
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值