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;
    }
  • 相关阅读:
    并查集分析+总结
    poj 3083 Children of the Candy Corn(bfs+dfs 数组模拟方向)
    poj 1094 Sorting It All Out (拓扑排序)
    poj 2632 Crashing Robots(模拟)
    poj 1068 Parencodings (模拟)
    poj 1273 Drainage Ditches ( 最大流Edmonds_karp算法)
    poj 3278 Catch That Cow (BFS)
    Codeforces Round #109 (Div. 2) 总结
    poj 2299 UltraQuickSort(归并排序)
    poj 1035 Spell checker(字符串)
  • 原文地址:https://www.cnblogs.com/AseanA/p/7759780.html
Copyright © 2011-2022 走看看