zoukankan      html  css  js  c++  java
  • 刷题总结—— Scout YYF I(poj3744 矩阵快速幂+概率dp)

    题目:

    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

    题解

      设dp[i]为走到i位置的概率··容易得出dp方程f[i]=f[i-1]*p+f[]i-2]*[1-p],但题目中的路径长度太大··然而地雷数量却很少··因此我们可以将地雷与地雷间的路程分段··分段用矩阵快速幂来求

    代码:

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cmath>
    #include<ctime>
    #include<cctype>
    #include<string>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    struct matrix
    {
        double a[3][3];
          inline void I()
        {
            memset(a,0,sizeof(a));
            a[1][1]=a[2][2]=1;
        }
        friend inline matrix operator *(matrix A,matrix B)    
        {  
            matrix temp;memset(temp.a,0,sizeof(temp.a));
            for(int i=1;i<=2;i++)
                for(int j=1;j<=2;j++)
                      for(int k=1;k<=2;k++)  
                        temp.a[i][j]+=A.a[i][k]*B.a[k][j];
            return temp;
        }
        matrix Pw(int b)
        {
            matrix ans,A=*this;ans.I();
            for(; b; b>>= 1, A=A*A)
                if(b&1) ans=ans*A;
            return ans;
        }
    };
    int pos[15],n;
    double p;
    int main()
    {
        //freopen("a.in","r",stdin);
        while(scanf("%d%lf",&n,&p)!=EOF)
        {
            for(int i=1;i<=n;i++)  scanf("%d",&pos[i]);
            sort(pos+1,pos+n+1);
            if(pos[1]==1)
            {
                cout<<"0.0000000"<<endl;continue;
            }
            matrix P,f;P.a[1][1]=0,P.a[1][2]=1-p,P.a[2][1]=1,P.a[2][2]=p;
            f.a[1][1]=0,f.a[1][2]=1;pos[0]=1;
            for(int i=1;i<=n;i++)
            {
                f=f*P.Pw(pos[i]-pos[i-1]-1);
                f.a[1][1]=f.a[1][2];f.a[1][2]=0;
            }
            f=f*P;
            printf("%0.7f
    ",f.a[1][2]);
        }
        return 0;
    }
  • 相关阅读:
    c#正则表达式应用实例
    C# 中堆栈,堆,值类型,引用类型的理解 (摘抄)
    c#用正则表达式获得指定开始和结束字符串中间的一段文本
    asp.net c#截取指定字符串函数
    <收藏>提高Web性能的14条法则(详细版)
    利用Anthem.net 实现前台javascript调用服务器端c#函数 及流程分析
    Anthem.net
    jQuery animate(滑块滑动)
    .NET使用母版页后,控件名称自动生成导致js无法正常操作.net控件的问题
    Cocos2dx跨平台Android环境配置
  • 原文地址:https://www.cnblogs.com/AseanA/p/7759780.html
Copyright © 2011-2022 走看看