POJ 3744 Scout yyf I (Matrix)

Source: Internet
Author: User
Tags mul

Description

Yyf isA couragous Scout. Now he isOn a dangerous mission which isTo penetrate into the enemy's base. After overcoming a series difficulties, YYF are now at the start of enemy'S famous"Mine Road". This isA veryLongRoad, on which there is numbers of mines. At first, YYF isAt step one. For each step after that, Yyf'll walk one step with a probability of p, or the jump in step with a probality of1-P. Here isThe task, given the 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 Casecontains, lines. the first line of each test Case  isN1≤n≤Ten) and P (0.25≤p≤0.75) seperated by a single blank, standing forThe number of mines and the probability to walk one step. The Second line of each test Case  isN Integer Standing forThe place of N mines. Each integer is inchThe range of [1,100000000].

Output

 Case inch 7 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

is also a matrix multiplication ...

This question is very obvious, N Thunder, respectively in A[1]...a[n], take one step probability for P, walk two steps probability for 1-p, start at position 1th, ask the probability of safe reaching the end point.

Obviously, if the K-bit has a ray, then the safe passage through this ray is only possible in the k-1 number of bits chosen to walk two steps to the k+1 number. Therefore, the following conclusions can be obtained: the probability of being disposed of in the first ray is the probability of the number of bits from a[i-1]+1 to A[i]. So, you can use 1 minus can be found safe through the first ray of the probability, the last ride up can, compared to the tragic data is very large, so need to use the Matrix fast power ...

Like the Fibonacci sequence, there are ans[i]=p*ans[i-1]+ (1-p) *ans[i-2], and the tectonic matrix is

|p 1-p | ANS[I-1] Ans[i]

| 0 | Ans[i-2] Ans[i-1]

1 //ans[i]=p*ans[i-1]+ (1-p) *ans[i-2]2#include <iostream>3#include <cstdio>4#include <cstring>5#include <algorithm>6#include <cmath>7#include <stdlib.h>8 using namespacestd;9 #defineN 16Ten intN; One Doublep; A intA[n]; -  - structMatrix the { -     Doublem[3][3]; - Matrix () -     { +Memset (M,0,sizeof(m)); -          for(intI=0;i<2; i++) +m[i][i]=1; A     } at }; -  - Matrix Mul (Matrix A,matrix B) - { - Matrix Res; -     inti,j,k; in      for(intI=0;i<2; i++) -     { to          for(intj=0;j<2; j + +) +         { -res.m[i][j]=0; the              for(intk=0;k<2; k++) *             { $res.m[i][j]=res.m[i][j]+ (a.m[i][k]*b.m[k][j]);Panax Notoginseng             } -         } the     } +     returnRes; A } theMatrix Fastm (Matrix A,intb) + { - Matrix Res; $      while(b) $     { -         if(b&1) -res=Mul (res,a); theA=Mul (a,a); -b>>=1;Wuyi     } the     returnRes; - } Wu  - intMain () About { $      while(SCANF ("%D%LF", &n,&p)! =EOF) -     { -          for(intI=1; i<=n;i++) scanf ("%d",&a[i]); -  ASort (A +1, a+n+1); +         Doubleans=1; the Matrix tmp; -tmp.m[0][0]=p; $tmp.m[0][1]=1-p; thetmp.m[1][0]=1; thetmp.m[1][1]=0; the  the Matrix CNT; -Cnt=fastm (tmp,a[1]-1); inAns*= (1-cnt.m[0][0]); the          for(intI=2; i<=n;i++) the         { About             if(a[i]==a[i-1])Continue; theCnt=fastm (tmp,a[i]-a[i-1]-1); theAns*= (1-cnt.m[0][0]); the         } +  -printf"%.7lf\n", ans); the Bayi     } the     return 0; the}
View Code

POJ 3744 Scout yyf I (Matrix)

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.