zoukankan      html  css  js  c++  java
  • poj3744 Scout YYF I[概率dp+矩阵优化]

    Scout YYF I
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 8598   Accepted: 2521

    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

    Source

    题意:一条路上,有n个炸弹,给出每个炸弹的位置,一次走一步的概率是p,走两步的概率是1-p。求安全走完的概率。

    //f[i]到达i点的概率 
    //f[i]=p*f[i-1]+(1-p)*f[i-2]
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<iostream>
    using namespace std;
    const int N=20;
    struct matrix{
        double s[2][2];
        matrix(){
            memset(s,0,sizeof s);
        }
    };
    int n,num[N];double p;
    matrix operator *(const matrix &a,const matrix &b){
        matrix c;
        for(int i=0;i<2;i++){
            for(int j=0;j<2;j++){
                for(int k=0;k<2;k++){
                    c.s[i][j]+=a.s[i][k]*b.s[k][j];
                }
            }
        }
        return c;
    }
    double fpow(matrix a,int p){
        matrix res;
        for(int i=0;i<2;i++) res.s[i][i]=1;
        for(;p;p>>=1,a=a*a) if(p&1) res=res*a;
        return res.s[0][0];
    }
    int main(){
        while(~scanf("%d%lf",&n,&p)){
            for(int i=1;i<=n;i++) scanf("%d",&num[i]);
            sort(num+1,num+n+1);
            matrix A;
            A.s[0][0]=p;A.s[0][1]=1.0;
            A.s[1][0]=1.0-p;A.s[1][1]=0;
            double ans=1;
            for(int i=1;i<=n;i++){
                ans*=(1.0-fpow(A,num[i]-num[i-1]-1));
            }
            printf("%.7f
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    mybatis-plus 相关
    Nginx 相关
    Docker 相关
    shiro & jwt
    Java GC
    C++ Q&A
    epoll ET & LT
    关于 free 命令显示内存使用情况问题
    Metaprogramming in Ruby: It’s All About the Self
    On The Value Of Fundamentals In Software Development (基础知识在软件开发中的价值)
  • 原文地址:https://www.cnblogs.com/shenben/p/6730684.html
Copyright © 2011-2022 走看看