poj3744 Scout YYF I

1 篇文章 0 订阅
1 篇文章 0 订阅
Scout YYF I
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4788 Accepted: 1272

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


大致题意:有一个人要经过一片地区(可视为一条线段),该地区埋有地雷(地雷数量不超过10个),并且给出每个地雷的位置(坐标为1~1e8)。而你要穿过这片地区,你的行动模式如下:给定一个浮点数p,你有p的概率向前走一步,有1-p的概率向前跳两部(即不会猜到前面一格的地雷),问你能成功穿越这片地区的几率有多大。


这是一道很水的概率题,首先,对于一颗地雷,要穿越他,必须得先到达该地雷前方的格子,然后从该格子出发,跳至地雷后面。设dp[i]为到达区域i的概率,则只要该区域前方不是起点,则有dp[i]=dp[i-1]*p+(1-p)*dp[i-2]。该方法是可行的,但是由于数据量达到了1e8,因此直接一步一步地DP会TLE。所幸的是,地雷数只有最多10个,我们可以枚举通过一个地雷的概率,然后将这些概率相乘。然而由于数量比较大,我们需要一个更便利的工具:快速幂。根据所给出的关系dp[i]=dp[i-1]*p+(1-p)*dp[i-2],我们很容易能得到以下公式:


得出矩阵公式后,用一次快速幂就可以解决问题了


以下为AC代码:


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int EXP = 1e-11;
int n,data[20];
double p;

struct matrix
{
    double a[2][2];
    matrix()
    {
        a[0][0]=1;a[0][1]=0;
        a[1][0]=0;a[1][1]=1;
    }
    matrix(double p)
    {
        a[0][0]=p;
        a[1][0]=1;
        a[0][1]=1-p;
        a[1][1]=0;
    }
    matrix operator * (const matrix &rhs) const
    {
        matrix t;
        t.a[0][0]=t.a[1][1]=0;
        for(int i=0;i<2;i++)
            for(int j=0;j<2;j++)
                for(int k=0;k<2;k++)
                    t.a[i][j]+=a[i][k]*rhs.a[k][j];
        return t;
    }
};

double matrix_calc(int times)
{
    times--;
    int temp=1;
    matrix base(p),ans;
    while(temp<=times)
    {
        if(temp×)
            ans=base*ans;
        base=base*base;
        temp<<=1;
    }
    return ans.a[0][0];
}


int main()
{
#ifdef LOCAL
//freopen("out.in","r",stdin);
#endif // LOCAL
    while(scanf("%d%lf",&n,&p)!=EOF)
    {
        double ans=1;
        data[0]=0;
        for(int i=1;i<=n;i++)
            scanf("%d",&data[i]);
        sort(data,data+n+1);
        bool flag=false;
        for(int i=1;i<=n;i++)
            if(data[i]-data[i-1]==1)
                flag=true;
        if(flag)
        {
            printf("0.0000000\n");
            continue;
        }
        for(int i=1;i<=n;i++)
        {
            if(data[i]==data[i-1])
                continue;
            ans*=(1-p)*(matrix_calc(data[i]-data[i-1]-1));
        }
        printf("%.7f\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值