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

    分段的概率DP+矩阵快速幂

                           Scout YYF I
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 4180   Accepted: 1076

    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

    POJ Monthly Contest - 2009.08.23, Simon 

    如果不用快速幂(TLE的)。。。。。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 
     6 using namespace std;
     7 
     8 int n,mine[15];
     9 double  p,d1,d2,d3,ans;
    10 
    11 int main()
    12 {
    13     while(scanf("%d%lf",&n,&p)!=EOF)
    14     {
    15         for(int i=1;i<=n;i++)
    16         {
    17             scanf("%d",mine+i);
    18         }
    19         sort(mine,mine+1+n);
    20         if(mine[1]==1)
    21         {
    22             printf("0.0000000
    "); continue;
    23         }
    24         else if(n==0)
    25         {
    26             printf("1.0000000
    "); continue;
    27         }
    28         bool flag=false;
    29         for(int i=1;i<n;i++)
    30         {
    31             if(mine[i]+1==mine[i+1])
    32             {
    33                 printf("0.0000000
    "); flag=true; break;
    34             }
    35         }
    36         if(flag==true) continue;
    37         ans=1.;
    38         for(int i=1;i<=n;i++)
    39         {
    40             int st=mine[i-1]+1,ed=mine[i];
    41             d2=0.,d1=1.;
    42             for(int j=st+1;j<=ed;j++)
    43             {
    44                 d3=d1*p+d2*(1-p);
    45                 d2=d1; d1=d3;
    46             }
    47             ans*=(1-d3);
    48         }
    49         printf("%.7lf
    ",ans);
    50     }
    51     return 0;
    52 }

    快速幂的。。。。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 
     6 using namespace std;
     7 
     8 struct Matrix
     9 {
    10     double a[2][2];
    11     Matrix() {}
    12     Matrix(double A,double B,double C,double D)
    13     {
    14         a[0][0]=A;a[0][1]=B;a[1][0]=C;a[1][1]=D;
    15     }
    16     Matrix operator* (const Matrix& b) const
    17     {
    18          Matrix temp;
    19          memset(temp.a,0,sizeof(temp.a));
    20          for(int i=0;i<2;i++)
    21          {
    22              for(int j=0;j<2;j++)
    23              {
    24                  for(int k=0;k<2;k++)
    25                  {
    26                      temp.a[i][j]+=a[i][k]*b.a[k][j];
    27                  }
    28              }
    29          }
    30          return temp;
    31     }
    32     Matrix Show()
    33     {
    34         for(int i=0;i<2;putchar(10),i++) for(int j=0;j<2;putchar(' '),j++) cout<<a[i][j];
    35     }
    36 };
    37 
    38 Matrix QuickPow(Matrix m,int n)
    39 {
    40     Matrix E(1,0,0,1);
    41     while(n>1)
    42     {
    43         if(n&1) E=E*m;
    44         m=m*m;
    45         n=n>>1;
    46     }
    47     E=E*m;
    48     return E;
    49 }
    50 
    51 int n,mine[20];
    52 double p,ans;
    53 
    54 int main()
    55 {
    56     while(scanf("%d%lf",&n,&p)!=EOF)
    57     {
    58         for(int i=1;i<=n;i++)
    59             scanf("%d",mine+i);
    60         sort(mine,mine+n+1);
    61         if(mine[1]==1)
    62         {
    63             printf("0.0000000
    "); continue;
    64         }
    65         else if(n==0)
    66         {
    67             printf("1.0000000
    "); continue;
    68         }
    69         bool flag=false;
    70         for(int i=1;i<n;i++)
    71         {
    72             if(mine[i]+1==mine[i+1])
    73             {
    74                 printf("0.0000000
    "); flag=true; break;
    75             }
    76         }
    77         if(flag==true) continue;
    78         ans=1.;
    79         for(int i=1;i<=n;i++)
    80         {
    81             Matrix m(p,1-p,1,0);
    82             m=QuickPow(m,mine[i]-mine[i-1]-1);
    83             ans*=1-m.a[0][0];
    84         }
    85         printf("%.7lf
    ",ans);
    86     }
    87     return 0;
    88 }
  • 相关阅读:
    2019-07-08 L410 EST科技英语翻译
    L405 NYC-ATF4
    L403 Royal espionage
    L402 EST
    L401 哭声识别
    L400 How Trees Affect the Weather
    L398
    final, finally, finalize 的区别
    try {}里有一个 return 语句, 那么紧跟在这个 try 后的 finally {}里的 code会不会被执行,什么时候被执行,在 return 前还是后?
    下 面 这 条 语 句 一 共 创 建 了 多 少 个 对 象 : String s="a"+"b"+"c"+"d";
  • 原文地址:https://www.cnblogs.com/CKboss/p/3393646.html
Copyright © 2011-2022 走看看