poj 3744 Scout YYF I

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

[Submit]   [Go Back]   [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

[Submit]   [Go Back]   [Status]

/*

题意:

说一个士兵进入雷区可以走一步(概率p)也可走两步(概率1-p)雷区里有很多的雷,问一下能安全通过雷区的概率

分析:

这道题不好做,一直没思路,后来看到网上的解题报告,醍醐灌顶啊。。(http://blog.csdn.net/yangliuy/article/details/6069420

首先

f[i] = p*f[i-1] + (1-p)*f[i-2];
f[i]表示到达位置i的概率(不是安全到达那些很复杂之类的概率,就只是简单的到达的概率),p为题目给的概率
由于有炸弹的原因,就不能直接算了。
这里我用 x[i]表示第i颗炸弹的位置
但如果把整个过程按照炸弹来分阶段,起点为x[i-1]+1,终点为x[i],这样每个阶段就只有终点有炸弹了(这就很容易算可达概率了)
所以我们可先算在阶段 i ,从起点x[i-1]+1到x[i]的概率qi,这个概率就表示踩到炸弹x[i+1]的概率了,那么1-qi就表示这一阶段安全的概率了(我有点不是很明白的就是它具体跳到哪里去了,不过如果按照这样划分阶段的模型,很容易知道结果是正确的)
所以把每个阶段安全可达的概率乘起来就是总的概率了(1-q1)(1-q2)...(1-qn)

*/

Source Code

Problem: C   User: sdau_09_zys
Memory: 184 KB   Time: 0 MS
Language: C++   Result: Accepted
Public:  

#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const double esp = 1e-15;
struct Matrix{
    double matrix[2][2];
    Matrix operator*(const Matrix & m){       //重载运算符*,矩阵乘法
        Matrix ans = {0,0,0,0};
        for (int i = 0;i < 2;i++)
            for (int k =0 ;k < 2 ;k ++)
                if (matrix[i][k]>esp)
                    for (int j = 0;j < 2;j++)
                    {
                        ans.matrix[i][j]+=matrix[i][k]*m.matrix[k][j];
                    }
                return ans;
    }
}unit = {1,0,0,1};
Matrix pow(Matrix a,int n)
{
    //矩阵求幂
    Matrix p = a,ans = unit;
    while(n)
    {
        if (n&1)
        {
            ans = ans*p;
        }
        p = p*p;
        n>>=1;
    }
    return ans;
}
double p;
double cal(int i,int j){    //计算踩雷的概率
    if (j == i)return 1;
    if (j - i == 1)return p;
    Matrix A={p,1,1-p,0};
    A = pow(A,j-i-1);
    return p*A.matrix[0][0] + A.matrix[1][0];
}
int main(){
    int n,i,x[100]={0};
    while(scanf("%d%lf",&n,&p) != EOF){
        for (i = 1;i <= n;i++)
            scanf("%d",&x[i]);
        sort(x+1,x+1+n);
        if (x[1]==1)
        {
            printf("%.7lf\n",0);
            continue;
        }
        double ans = 1;
        for ( i = 1;i <= n;i++)
        {
            //
            ans*=(1-cal(x[i-1]+1,x[i]));
        }
        printf("%.7lf\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值