解题报告 之 POJ 3744 Scout YYF I

解题报告 之 POJ 3744 Scout YYF I


Description

YYF is a couragous scout. Now he is on a dangerous mission which is to penetrate into the enemy's base. After overcoming a series difficulties, YYF is now at the start of enemy's famous "mine road". This is a very long road, on which there are numbers of mines. At first, YYF is at step one. For each step after that, YYF will walk one step with a probability of  p, or jump two step with a probality of 1-  p. Here is the task, given the place of each 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 case contains two lines. 
The First line of each test case is  N (1 ≤  N ≤ 10) and  p (0.25 ≤  p ≤ 0.75) seperated by a single blank, standing for the number of mines and the probability to walk one step. 
The Second line of each test case is N integer standing for the place of N mines. Each integer is in the range of [1, 100000000].

Output

For each test case, output the probabilty in a single line with 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


题目大意:你现在在一条格子线上的第1格,每次你可能走一格( 概率为P ),或者走两格( 1-P )。这条线上有N个地雷,给你每个地雷的位置[1,1 00000000]。问安全通过这条格子线的概率?

分析:设dp[i]表示到达i点的概率,则 初始值 dp[1]=1。很容易想到转移方程: dp[i]=p*dp[i-1]+(1-p)*dp[i-2];但是由于坐标的范围很大,直接这样求是不行的,而且当中的某些点还存在地雷,这些点不能转移。

所以这道题必须转化思路,注意到可以以每个地雷分段,要通过整个雷区只需要通过每个雷即可。
所以我们将雷区分为N个段,分别是:
[ 1, x[1] ]
[ x[1]+1, x[2] ]
[ x[2]+1, x[3] ]
... ...
对于每一段,要么踩中x[i],要么没踩中x[i](从而到达x[i]+1)。然后根据乘法原则,通过雷区的概率就是把通过每段的概率相乘即可。
而通过第 i 段的概率 = 1 - 踩中 x[i] 的概率。那么问题转化为踩中 x[i] 的概率。
这里其实我不知道别人是怎么想到的,可能由于是线性的概率题而且状态的转移是单向的。所以才考虑到矩阵快速幂来求踩中x[i]的概率。

首先构造状态矩阵[ P(i-1), P(i-2) ],分别是走到某段第 i-1 个格子和 第 i-2 个格子的概率。然后构造转移矩阵 
那么这两个矩阵相乘就得到了[ P(i-1)*p+P(i-2)*(1-p), P(i-1)  ] = [ P(i), P(i-1) ],类似斐波那契数列。
走一格的状态是[ p,0 ],走两格的状态是[p^2+(1-p), p]。由于走一格的时候并不知道P(i-2),所以这时候不能另它为0,否则后面就会少一项,所以走一格的状态应该是[ p,1 ],又注意到这个状态和转移矩阵第一行是一样的,所以索性让状态矩阵=转移矩阵。因为第二行的值对于状态矩阵来说并不重要。这样统一之后就可以不分情况讨论。 

所以比如说 x[1]=5。那么就需要用走一格的状态矩阵*构造矩阵^3,也就是构造矩阵^4。

上代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

struct Mat
{
	double m[2][2];
	Mat()
	{
		m[0][0] = m[0][1] = m[1][0] = m[1][1] = 0.0;
	}
};

Mat mul( Mat a, Mat b )
{
	Mat res;
	for(int i = 0; i < 2; i++)
		for(int j = 0; j < 2; j++)
			for(int k = 0; k < 2; k++)
				res.m[i][j] += a.m[i][k] * b.m[k][j];
	return res;
}

Mat pow( Mat a, int b )
{
	Mat res;
	res.m[0][0] = res.m[1][1] = 1.0;
	while(b)
	{
		if(b & 1)
		{
			res = mul( res, a );
		}
		b >>= 1;
		a = mul( a, a );
	}
	return res;
}

int x[15];

int main()
{
	int n;
	double p;

	while(scanf( "%d%lf", &n, &p ) == 2)
	{

		Mat base, ans;
		double pos = 1.0;
		base.m[0][0] = p;
		base.m[0][1] = 1;
		base.m[1][0] = 1 - p;
		base.m[1][1] = 0;
		
		for(int i = 1; i <= n; i++)
		{
			scanf( "%d", &x[i] );
		}
		sort( x + 1, x + 1 + n );
		ans = pow( base, x[1] - 1 );
		pos *= (1-ans.m[0][0]);

		for(int i = 2; i <= n; i++)
		{
			if(x[i] == x[i - 1])continue;
			ans = pow( base, x[i] - x[i - 1] - 1 );
			pos *= (1-ans.m[0][0]);
		}
		printf( "%.7lf\n", pos );
	}
	return 0;
}

还是觉得概率DP很难的样子,,,,要哭。



猜你喜欢

转载自blog.csdn.net/maxichu/article/details/48886005