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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值