Poj 3744 scout yyf I (probability DP, matrix optimization)

Source: Internet
Author: User
Scout yyf I
Time limit:1000 ms   Memory limit:65536 K
Total submissions:5020   Accepted:1355

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 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 into test cases ended EOF.
Each test case contains two lines.
The first line of each test case is N(1 ≤ N≤ 10) and P(Less than or equal to 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.522 0.52 4

Sample output

0.50000000.2500000

Question: on the road to being dissatisfied with mines, you are now starting at one place. Mines are deployed at N points, 1 <= n <= 10. The coordinate range of a mine point is [1,]. The probability of moving forward P is one step forward, and the probability of Moving Forward 1-P is one step forward. Ask the probability of passing this path. That is, do not go to a place with mines. If DP [I] is set to indicate the probability of reaching the I point, the initial value DP [1] = 1. it is easy to think of the transfer equation: DP [I] = p * DP [I-1] + (1-p) * DP [I-2]; but because of the large range of coordinates, directly This is not acceptable, in addition, some points still have mines. The coordinates of N mine points are X [1], X [2], X [3] ''' X [N]. we divide the road into N segments: 1 ~ X [1]; X [1] + 1 ~ X [2]; X [2] + 1 ~ X [3]; ''' X [N-1] + 1 ~ X [N]. In this way, each segment has only one mine. We only need to obtain the probability of passing through each segment. Multiplication is the answer.

Specifically, for a line segment whose length is NK, set a to the beginning of the Line Segment K, B is the end of the line segment K, nk = a-b-1,

The probability of reaching a is set to 1, and the probability of reaching a + 1 is P. The probability of reaching a + 2 is Pa * (1-p) + pa + 1 * P, this allows recurrence.

Since Pam = Pam-1 * P + Pam-2 * (1-p), that is, the push formula is the same, can be done with matrix multiplication + Rapid power.

The solution to the problem can be seen as pn1 * (1-p) * pn2 * (1-p) *... PNN * (1-p), because the last trap must jump over to ensure security.

  1 #include<iostream>  2 #include<cstdio>  3 #include<cstring>  4 #include<vector>  5 #include<algorithm>  6 #include<cmath>  7 #define M(a,b) memset(a,b,sizeof(a))  8 typedef long long LL;  9  10 using namespace std; 11  12 int n; 13 double p; 14 int num[20]; 15  16 struct matrix 17 { 18     double mat[2][2]; 19     void init() 20     { 21         mat[0][0] = p; 22         mat[0][1] = 1-p; 23         mat[1][0] = 1; 24         mat[1][1] = 0; 25     } 26 }; 27  28 matrix mamul(matrix aa,matrix bb) 29 { 30     matrix c; 31     for(int i = 0;i<2;i++) 32     { 33         for(int j = 0;j<2;j++) 34         { 35             c.mat[i][j] = 0; 36             for(int k = 0;k<2;k++) 37                 c.mat[i][j]+=(aa.mat[i][k]*bb.mat[k][j]); 38         } 39     } 40     return c; 41 } 42  43 matrix mul(matrix s, int k) 44 { 45     matrix ans; 46     ans.init(); 47     while(k>=1) 48     { 49         if(k&1) 50             ans = mamul(ans,s); 51         k = k>>1; 52         s = mamul(s,s); 53     } 54     return ans; 55 } 56  57 int main() 58 { 59     while(scanf("%d%lf",&n,&p)==2) 60     { 61             for(int i = 1;i<=n;i++) 62                 scanf("%d",&num[i]); 63             sort(num+1,num+n+1); 64             num[0] = 0; 65             if(num[1]==1) {puts("0.0000000"); continue;} 66             matrix ans; 67             ans.init(); 68             double out = 1; 69             matrix tem; 70             tem.mat[0][0] = (1-p)+p*p; 71             tem.mat[0][1] = p; 72             tem.mat[1][0] = p; 73             tem.mat[1][1] = 1; 74             int flag = 0; 75             for(int i = 1;i<=n;i++) 76             { 77                 if(num[i]-num[i-1]<2) 78                    {puts("0.0000000"); flag = 1; break;} 79                 if(num[i]-num[i-1]-2==0) 80                     out*=tem.mat[1][1]; 81                 else 82                 { 83                     ans.init(); 84                     ans = mul(ans,num[i]-num[i-1]-3); 85                     matrix c; 86                     for(int i = 0;i<2;i++) 87                     { 88                        for(int j = 0;j<2;j++) 89                        { 90                           c.mat[i][j] = 0; 91                           for(int k = 0;k<2;k++) 92                               c.mat[i][j]+=(ans.mat[i][k]*tem.mat[k][j]); 93                        } 94                     } 95                     //cout<<c.mat[1][1]<<‘!‘<<endl; 96                     out*=c.mat[1][1]; 97                 } 98                 out*=(1-p);//cout<<out<<endl; 99                 tem.mat[0][0] = (1-p)+p*p;100                 tem.mat[0][1] = p;101                 tem.mat[1][0] = p;102                 tem.mat[1][1] = 1;103             }104             if(!flag)105             printf("%.7f\n",out);106     }107     return 0;108 }

 

 

Poj 3744 scout yyf I (probability DP, matrix optimization)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.