"poj3744" Scout yyf I

Source: Internet
Author: User

http://poj.org/problem?id=3744 (Topic link)

Test instructions

Give N Thunder, respectively in A[1]...a[n], take one step probability for P, walk two steps probability for 1-p, a start in position 1th, ask the probability of safe to reach the end.

Solution

It is obvious that dp:f[i]=p*f[i-1]+ (1-p) *f[i-2]. Consider the a[i] position of thunder, then the probability of safe passage is to reach f[a[i]+1] The probability is: f[a[i]-1]* (1-p).

Because A[i] is very large, so we want to segment with a matrix fast power.

Details

The power of the code is down ... Inexplicable wa up can go to see discuss, good pit.

Code
poj3744#include<algorithm> #include <iostream> #include <cstdlib> #include <cstring># include<cstdio> #include <cmath> #define LL long long#define inf 1<<30#define EPS 1e-8#define Pi acos (- 1.0) #define FREE (a) freopen (a ".", "R", stdin), Freopen (a ". Out", "w", stdout); using namespace std;double tmp[2][2],f[2] [2],t[2][2],p;int a[20],n;void Power (int k) {T[0][0]=p,t[0][1]=1,t[1][0]=1-p,t[1][1]=0;while (k) {if (k&1) {for ( int i=0;i<=1;i++) for (int j=0;j<=1;j++) {tmp[i][j]=0;for (int k=0;k<=1;k++) tmp[i][j]+=f[i][k]*t[k][j];} memcpy (F,tmp,sizeof (f)); k>>=1;for (int i=0;i<=1;i++) for (int j=0;j<=1;j++) {tmp[i][j]=0;for (int k=0;k<=1;k++) tmp[i][j]+=t[i][ K]*T[K][J];} memcpy (t,tmp,sizeof (t));}} int main () {while (scanf ("%d%lf", &n,&p)!=eof) {for (int i=1;i<=n;i++) scanf ("%d", &a[i]); Sort (a+1,a+1+n ); a[0]=0;f[1][1]=1;for (int i=1;i<=n;i++) {f[1][0]=f[1][1]*p;f[0][1]=f[1][0]*p+f[1][1]* (1-p); f[0][0]=f[0][1]*p +f[1][0]* (1-p); if (a[i]-a[i-1]==1) {f[1][1]=0;break;} else if (a[i]-a[i-1]==2) f[1][1]=f[1][1]* (1-p), else if (a[i]-a[i-1]==3) f[1][1]=f[1][0]* (1-p), else if (a[i]-a[i-1]==4) f[1][1]=f[0][1]* (1-p); else power (a[i]-a[i-1]-5), f[1][1]= (1-p) *f[0][0];} if (Fabs (f[1][1]) <eps) puts ("0.0000000"), Else printf ("%.7lf\n", F[1][1]);} return 0;}

"poj3744" Scout yyf I

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.