POJ 3744 Scout YYF I probability dp

Source: Internet
Author: User

Scout YYF I

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9564 Accepted: 2799

Description Yyf is a couragous scout. Now he's on a dangerous mission which are to penetrate into the enemy ' s base. After overcoming a series difficulties, YYF are now at the start of enemy ' s famous "Mine Road". This was a very long road, on which there is numbers of mines. At first, YYF are at 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 of 1-p. Here's the task, given the place's mine, please calculate the probality that YYF can go through the "Mine Road" SAF Ely.

Input the input contains many test cases ended with EOF.
Each test case contains the lines.
The first line of all test case was N (1≤n≤10) and P (0.25≤p≤0.75) seperated by a single blank, standing for T He number of mines and the probability to walk one step.
The Second line of all test case was n integer standing for the place of N mines. Each integer was in the range of [1, 100000000].

Output for each test case, output of the probabilty in A, a, and the precision to 7 digits after the 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


A one-dimensional axis, in which n places there is a trap. Now a person starts from position No. 0, walk one lattice probability is P, walk two lattice probability is 1-p. Ask this person to avoid the probability of all traps.


Cut the journey in the same place as the trap, and multiply the probability of avoiding the trap as the answer.

Because n is large, it is necessary to solve the matrix fast power.


#include <cstdio> #include <iostream> #include <string.h> #include <string> #include <map > #include <queue> #include <deque> #include <vector> #include <set> #include <algorithm&
Gt #include <math.h> #include <cmath> #include <stack> #include <iomanip> #define MEM0 (a) memset (a)
0,sizeof (a)) #define Meminf (a) memset (A,0x3f,sizeof (a)) #define size 2 using namespace std;
typedef long Long LL;
typedef long double LD;
typedef double DB;  
const int inf=0x3f3f3f3f;   
const LL LLINF=0X3F3F3F3F3F3F3F3F;
Const LD Pi=acos ( -1.0L);

int a[15];

struct Matrix {db a[size][size];};
	Matrix operator* (const matrix &x,const matrix &y) {int i,j,k;
	Matrix ans;
			for (i=0;i<size;i++) {for (j=0;j<size;j++) {ans.a[i][j]=0.0;
	for (k=0;k<size;k++) {ans.a[i][j]+=x.a[i][k]*y.a[k][j];
			Ans.a[i][j]%=m;
}}} return ans;
	} Matrix Fastpower (Matrix Base,ll index) {matrix ans,now; IntI,j;
		for (i=0;i<size;i++) {for (j=0;j<size;j++) {if (i==j) ans.a[i][j]=1; else ans.a[i][j]=0;
	}} if (index<0) return ans;
	Now=base;
	ll K=index;
		while (k) {if (k%2) Ans=ans*now;
		Now=now*now;
	k/=2;
} return ans;
	} int main () {int n,i;
	DB p;
		while (scanf ("%d%lf", &n,&p)!=eof) {Matrix L;
		for (i=1;i<=n;i++) {scanf ("%d", &a[i]);
		} sort (a+1,a+n+1);
		a[0]=0;
		L.a[0][0]=p;l.a[0][1]=1.0-p;
		l.a[1][0]=1.0;l.a[1][1]=0.0;
		DB tot=1.0;
			for (i=1;i<=n;i++) {Matrix fp=fastpower (l,a[i]-a[i-1]-1);
		TOT*=1-FP.A[0][0];
	} printf ("%.7lf\n", tot);
} return 0;
 }


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.