zoukankan      html  css  js  c++  java
  • Scout YYF I(POJ 3744)

    Scout YYF I
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 5565   Accepted: 1553

    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

     
    此题为概率dp
    1.求概率:
    有N个地雷,分段处理,求每段踩中地雷的概率P,则未踩中的概率为1 - P,再分别相乘。
    每段踩中地雷的概率递推式为:dn = p * dn-1 + (1 - p) * dn - 2;
    2.对递推式的处理:
    由于数据太大,直接递推求解容易tle,所以将递推式转化为矩阵形式(方法http://www.cnblogs.com/sunus/p/4404273.html),再用矩阵快速幂处理。
    注意:最终得[dn; dn-1] = [p, 1-p; 1, 0]^(n - 1) * [d1; d0];
    d1 = p;
    d0 = 1;
    因此,[dn; dn-1] = [p, 1-p; 1, 0]^(n - 1) * [p; 1];
    因此,dn =  ([p, 1-p; 1, 0]^n)[0][0];
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #define N 55
    #define repu(i, a, b) for(int i = a; i < b; i++)
    using namespace std;
    #define MAXN 2
    #define ll double
    ll base[MAXN][MAXN] = {{1.0, 0.0}, {0.0, 1.0}};
    int ma[N];
    
    struct Matrix
    {
        ll m[MAXN][MAXN]; //二维数组存放矩阵
        Matrix(ll num[MAXN][MAXN])
        {
            for(int i = 0 ; i < MAXN ; i++)
                for(int j = 0 ; j < MAXN ; j++)
                    m[i][j] = num[i][j];
        }  //对数组的初始化
        Matrix() {}
    };
    
    Matrix operator * (Matrix m1, Matrix m2)
    {
        int i, j, k;
        Matrix temp;
        for (i = 0; i < MAXN; i++)
        {
            for (j = 0; j < MAXN; j++)
            {
                temp.m[i][j] = 0;
                for(k = 0 ; k < MAXN ; k++)
                    temp.m[i][j] += (m1.m[i][k] * m2.m[k][j]);// % mod;
    //temp.m[i][j] %= mod; //注意每一步都进行取模
            }
        }
        return temp;
    }
    
    Matrix quickpow(Matrix M, int n)
    {
        Matrix tempans(base);  //初始化为单位矩阵
        while(n)
        {
            if(n & 1)
                tempans = tempans * M; //已经重载了*
            n = n >> 1;
            M = M * M;
        } //快速幂(类似整数)
        return tempans;
    }
    
    
    int main()
    {
        int n;
        ll p;
        while(~scanf("%d", &n))
        {
            scanf("%lf", &p);
            Matrix M, C;
            M.m[0][0] = p;
            M.m[0][1] = 1.0 - p;
            M.m[1][0] = 1.0;
            M.m[1][1] = 0.0;
            int last, rear;
            double P = 1.0;
            ma[0] = 0;
            repu(i, 1, n + 1)
            scanf("%d", &ma[i]);
            sort(ma, ma + n + 1);
            repu(i, 1, n + 1)
            {
                C = quickpow(M, ma[i] - ma[i - 1] - 1);
                P *= (1.0 - C.m[0][0]);
            }
            if(ma[1] == 1) P = 0.0;
            printf("%.7lf
    ", P);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    存储型 XSS 原理复现
    反射型 XSS 原理复现
    HTTP 简易理解
    Markdown 流程图语法
    Dirsearch 快速开始
    sqlmap 快速开始
    SQL 注入原理
    XSS 原理
    51nod 1835 完全图
    11.5 AM 请求
  • 原文地址:https://www.cnblogs.com/sunus/p/4410326.html
Copyright © 2011-2022 走看看