poj3744(概率DP)

Scout YYF I
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3943   Accepted: 999

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

POJ Monthly Contest - 2009.08.23, Simon
 
本题是个较简单的概率问题。若不考虑mine,走到i的概率为dp[i],其状态转移方程为
                                                dp[i]=p*dp[i-1]+(1-p)*dp[i-2]
当考虑mine时,当走到mine时,其概率应该为0,我们可以根据mine把距离分成若干条线段。在线段上走就不会碰到mine,其概率就可以完全套用方程dp[i]=p*dp[i-1]+(1-p)*dp[i-2],我们认为线段的左端点不是mine,右端点是mine,跨过mine的概率为1-dp[mine]。若只想到这里,本题还是无法解决,本题坐标范围极大,开数组会爆内存,而且运行时肯定伴随超时。

于是我们用矩阵来优化一下。想想超大fibonacci数的矩阵求法,f[i]=f[i-1]+f[i-2]。f[n]等价于矩阵

| 1 1 |

| 1 0 |
的n次方以后,矩阵的第a[0,0]个元素。

同样的,我们为这个dp[i]=P*dp[i-1]+(1-P)*dp[i-2]构造一个矩阵

| P ,1-P |

| 1 , 0   |

ans[i]=p*ans[i-1]+(1-p)*ans[i-2] ,构造矩阵为

那么dp[n]就是该矩阵N次方后的第a[0,0]个元素。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;


int n;
int mine[20];
double p;
//origin存放需计算的矩阵,res存放答案矩阵
struct matrix
{
	double a[2][2];
}origin,res;

matrix multiply(matrix x,matrix y)
{
	matrix temp;
	memset(temp.a,0,sizeof(temp.a));
	for(int i=0;i<2;i++)
	{
		for(int j=0;j<2;j++)
		{
			for(int k=0;k<2;k++)
			{
				temp.a[i][j]+=x.a[i][k]*y.a[k][j];
			}
		}
	}
	return temp;
}

//将res初始化为单位矩阵,人为输入origin
void init()
{

	origin.a[0][0]=p;
	origin.a[0][1]=1-p;
	origin.a[1][0]=1;
	origin.a[1][1]=0;
	//将res.a初始化为单位矩阵 
	memset(res.a,0,sizeof(res.a));
	res.a[0][0]=res.a[1][1]=1;                  
}

//矩阵快速幂的计算
void calc(int n)
{
	while(n)
	{
		if(n&1)
			res=multiply(res,origin);
		n>>=1;
		origin=multiply(origin,origin);
	}
}

bool cmp(int a,int b)
{
	return a<b;
}

int main()
{
	int i,j;
	while(~scanf("%d%lf",&n,&p))
	{
		for(i=0;i<n;i++)
		{
			scanf("%d",&mine[i]);
		}
		
		sort(mine,mine+n,cmp);
		init();
		double ans;
		calc(mine[0]-1);
		ans=1-res.a[0][0];
		for(i=1;i<n;i++)
		{
			if(mine[i]==mine[i-1])
				continue;
			init();
			calc(mine[i]-(mine[i-1]+1));
			ans*=(1-res.a[0][0]);
		}
		printf("%.7lf\n",ans);
	}
	return 0;
}


 

你可能感兴趣的:(动态规划)