POJ 3744 Scout yyf I (Matrix fast Power)

Source: Internet
Author: User
Tags mul

Test instructions: There are n mines on one road, tell you the location of the N thunder, a person initially in position 1, the person can walk 1 units at a time or jump 2 units, the probability of P and 1-p, respectively, now require this person safe passage of the probability of the road

Idea: To find out the probability of safety through each ray, and then all the ride up can be. To safely pass a ray (assuming it is in the place[i] position), only two cells jump from place[i]-1 to pass safely. This only requires that the probability of place[i-1]+1 to Place[i]-1 is multiplied (1-p) to obtain the probability of passing the first ray.

Because of the large data, the matrix must be quickly idempotent. In fact, this is a variant of the Fibonacci sequence: F (i) =f (i-1) *p+f (i-2) * (1-p); to quickly find F (i), use the matrix to quickly power.

#include <iostream>#include<cstdio>#include<algorithm>#include<cstring>using namespacestd;intplace[1010] ;Doubleep[1010], p, F1, F2;structmat{Doublemat[Ten][Ten] ;};    Mat Mul (Mat A,mat b) {mat ret;  for(intI=0;i<2; i++)       for(intj=0;j<2; j + +) {Ret.mat[i][j]=0;  for(intk=0;k<2; k++) Ret.mat[i][j]+=a.mat[i][k]*B.mat[k][j]; }    returnret;} Mat Pow_m (Mat A,intN)    {MAT ret; memset (Ret.mat,0,sizeof(Ret.mat));  for(intI=0;i<2; i++) ret.mat[i][i]=1; Mat Temp=A;  while(n) {if(n&1) ret=Mul (ret,temp); Temp=Mul (temp,temp); N>>=1; }    returnret;}DoubleQ_mul (intk)    {Mat A; a.mat[0][0]=p, a.mat[0][1]=1.0-P, a.mat[1][0]=1.0, a.mat[1][1]=0; MAT ret=pow_m (a,k); return(f2*ret.mat[1][0]+f1*ret.mat[1][1])*(1.0-p);}intMain () {intN;  while(cin>>n>>p) {memset (place,0,sizeof(place));  for(intI=0; i<n;i++) {scanf ("%d",&Place[i]); } sort (Place,place+N); intnow=1; Doubleans=1.0; if(place[0]==1) {printf ("0.0000000\n") ; Continue ; }         for(intI=0; I<n; i++){            //cout<<i<< "<<place[i]<<" "<<now<<" "<<endl;            if(Place[i] = =Now ) { Now= -1;  Break; }            Else{F1=1.0; if(Now +1==Place[i]) {                    //cout<<1<<endl;Ep[i] = f1* (1.0-p); }                Else{F2=f1* (1.0*p); Ep[i]=q_mul (place[i]-(now+1)) ; //cout<<place[i]-(now+1) <<endl;} Now=place[i] +1 ; }        }        if(now==-1) {printf ("0.0000000\n") ; }        Else{             for(intI=0; I <n; i++){                //cout<<ep[i]<<endl;ans*=Ep[i]; } printf ("%.7f\n", ans); }     }    return 0;}

POJ 3744 Scout yyf I (Matrix fast Power)

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.