• POJ-3744-概率dp+矩阵幂(分段)


    Scout YYF I
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 10214   Accepted: 2980

    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

        起始点在1,总共有n枚炸弹,位置位于x[i],(1<=x[i]<=1e8),每次有P的概率走一步,(1-P)的概率走两步,问安全走过所有炸弹的概率。
    设f[i]表示安全到i点的概率,那么答案就是f[ max{x[i]} +1 ] ,因为每次只能走1/2,要想走过一个炸弹xi,只能在xi-1处走两步来实现(最后一次走)。
    有f[i]=f[i-1]*P+f[i-2]*(1-P) ,但是N很大会超时。想到用矩阵优化,但是中间有炸弹的地方就不好处理了。对这个道路进行划分,根据炸弹的位置分为
    [1,x[1]] , [x[1]+1,x[2]]......[x[n-1]+1,x[n]] ; 分成了N段,假设成功通过第i段的概率是Pi,那么答案就是∏Pi ,Pi就等价于从x[i-1]+1走到x[i]+1且最后一步走的大小是2
    的概率,由于中间过程没有炸弹所以可以矩阵幂来算。
    (f(n),f(n-1)) = (f(n-1),f(n-2)) * ((P,1),(1-P,0))
      注意判断特殊情况,两个炸弹相邻或者起点是炸弹那么输出0.0000000即可。
      
     1 #include<iostream>
     2 #include<cstring>
     3 #include<queue>
     4 #include<cstdio>
     5 #include<stack>
     6 #include<set>
     7 #include<map>
     8 #include<cmath>
     9 #include<ctime>
    10 #include<time.h> 
    11 #include<algorithm>
    12 using namespace std;
    13 #define mp make_pair
    14 #define pb push_back
    15 #define debug puts("debug")
    16 #define LL long long 
    17 #define pii pair<int,int>
    18 #define eps 1e-12
    19 
    20 struct matrix{
    21     double a[2][2];
    22     matrix& operator*(matrix &tmp){
    23         matrix ans;
    24         memset(ans.a,0,sizeof(ans.a));
    25         for(int i=0;i<2;++i){
    26             for(int j=0;j<2;++j){
    27                 for(int k=0;k<2;++k){
    28                     ans.a[i][j]+=a[i][k]*tmp.a[k][j];
    29                 }
    30             }
    31         }
    32         return ans;
    33     }
    34 }A,E;
    35 double qpow(matrix H,int b){
    36     matrix ans=E;
    37     while(b){
    38         if(b&1) ans=ans*H;
    39         H=H*H;
    40         b>>=1;
    41     }
    42     return ans.a[0][0];
    43 }
    44 int main()
    45 {
    46     int n,m,i,j,k,t;
    47     int x[15];
    48     double P;
    49     E.a[0][0]=E.a[1][1]=1;
    50     E.a[0][1]=E.a[1][0]=0;
    51     while(scanf("%d%lf",&n,&P)!=EOF){
    52     A.a[0][0]=P;
    53     A.a[1][0]=1.00-P;
    54     A.a[0][1]=1;
    55     A.a[1][1]=0;
    56         for(i=1;i<=n;++i) scanf("%d",x+i);
    57         sort(x+1,x+1+n);
    58         bool ok=1;
    59         for(i=2;i<=n;++i)
    60             if(x[i]-x[i-1]==1) ok=0;
    61         if(ok==0||x[1]==1){
    62             printf("%.7f
    ",0.0);
    63             continue;
    64         }
    65         double ans=1;
    66         ans=ans*(1-P)*qpow(A,x[1]-2);
    67         for(i=2;i<=n;++i){
    68             if(x[i]==x[i-1]) continue;
    69             ans=ans*((double)1-P)*qpow(A,x[i]-x[i-1]-2);
    70         }
    71         printf("%.7f
    ",ans);
    72     }
    73     return 0; 
    74 }
     
  • 相关阅读:
    A1023 Have Fun with Numbers (20分)(大整数四则运算)
    A1096 Consecutive Factors (20分)(质数分解)
    A1078 Hashing (25分)(哈希表、平方探测法)
    A1015 Reversible Primes (20分)(素数判断,进制转换)
    A1081 Rational Sum (20分)
    A1088 Rational Arithmetic (20分)
    A1049 Counting Ones (30分)
    A1008 Elevator (20分)
    A1059 Prime Factors (25分)
    A1155 Heap Paths (30分)
  • 原文地址:https://www.cnblogs.com/zzqc/p/8977385.html
Copyright © 2020-2023  润新知