Poj 3744 Scout yyf I probability dp + Matrix Multiplication

Source: Internet
Author: User

Poj 3744 Scout yyf I probability dp + Matrix Multiplication

Analysis:

The dis (k, v1, v2) function calculates the probability of reaching the current position as v1, the probability of one step prior to the current position as v2, the probability of arrival of the k step forward, and then accelerates the matrix.

Code:

 

//poj 3744//sep9#include 
 
  #include using namespace std;int pos[12];double p,mat[4][4];double ans[4][4];void mul1(){double c[4][4];c[0][1]=c[1][0]=c[0][0]=c[1][1]=0.0;for(int i=0;i<2;++i)for(int j=0;j<2;++j)for(int k=0;k<2;++k)c[i][j]+=ans[i][k]*mat[k][j];for(int i=0;i<2;++i)for(int j=0;j<2;++j)ans[i][j]=c[i][j];}void mul2(){double c[4][4];c[0][1]=c[1][0]=c[0][0]=c[1][1]=0.0;for(int i=0;i<2;++i)for(int j=0;j<2;++j)for(int k=0;k<2;++k)c[i][j]+=mat[i][k]*mat[k][j];for(int i=0;i<2;++i)for(int j=0;j<2;++j)mat[i][j]=c[i][j];}double dis(int len,double val,double val1){mat[1][0]=1.0;mat[0][1]=1-p;mat[0][0]=p;mat[1][1]=0;ans[1][0]=ans[0][1]=0;ans[0][0]=ans[1][1]=1.0;while(len){if(len&1) mul1();mul2();len>>=1;}return ans[0][0]*val+ans[0][1]*val1;}int main(){int n;while(scanf("%d%lf",&n,&p)==2){pos[0]=0;for(int i=1;i<=n;++i)scanf("%d",&pos[i]);sort(pos+1,pos+1+n);double ans=1,a1=1,a0=0;for(int i=1;i<=n;++i){if(pos[i]==pos[i-1]) continue;ans=ans-dis(pos[i]-pos[i-1]-1,a1,0);a1=ans;}printf("%.7lf\n",ans);}return 0;} 
 


 

Related Article

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.