现在的位置: 首页 > 综合 > 正文

POJ3744—-Scout YYF I

2019年02月17日 ⁄ 综合 ⁄ 共 2456字 ⁄ 字号 评论关闭
Scout YYF I
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5381   Accepted: 1475

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

设dp[i]表示走到i的概率
在不考虑雷的影响下,
dp[i] = p * dp[i - 1] + (1 - p) * dp[i - 2]
题目要求最后安全通过所有雷的概率,我们分段考虑:
在分别考虑每一个雷的时候,我们知道,本次开始走的点一定是上一个雷的下一个点,考虑到范围很大,所以根据转移方程得到一个系数矩阵,然后快速幂求解(这里真的很牛逼) 因为每次我们只考虑了一段,没有考虑能越过当前雷的前提是前面的雷都要越过,所以要把最后的答案都乘起来才对

/*************************************************************************
    > File Name: POJ3744.cpp
    > Author: ALex
    > Mail: 405045132@qq.com 
    > Created Time: 2014年12月24日 星期三 21时20分51秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

int pos[15];

struct martix
{
	double mat[2][2];
};

martix multiply(martix a, martix b)
{
	martix c;
	c.mat[0][0] = c.mat[0][1] = c.mat[1][0] = c.mat[1][1] = 0;
	for (int i = 0; i < 2; ++i)
	{
		for (int j = 0; j < 2; ++j)
		{
			for (int k = 0; k < 2; ++k)
			{
				c.mat[i][j] += a.mat[i][k] * b.mat[k][j];
			}
		}
	}
	return c;
}

martix fast_pow(martix a, int b)
{
	martix c;
	c.mat[0][0] = 1;
	c.mat[0][1] = c.mat[1][0] = 0;
	c.mat[1][1] = 1;
	while (b)
	{
		if (b & 1)
		{
			c = multiply(c, a);
		}
		a = multiply(a, a);
		b >>= 1;
	}
	return c;
}

int main()
{
	int n;
	double p;
	while (~scanf("%d%lf", &n, &p))
	{
		for (int i = 1; i <= n; ++i)
		{
			scanf("%d", &pos[i]);
		}
		double ans = 1;
		sort (pos + 1, pos + n + 1);
		martix x;
		x.mat[0][0] = p;
		x.mat[0][1] = 1 - p;
		x.mat[1][0] = 1;
		x.mat[1][1] = 0;
		martix tmp;
		tmp = fast_pow(x, pos[1] - 1);
		ans *= (1 - tmp.mat[0][0]);
		for (int i = 2; i <= n; ++i)
		{
			if (pos[i] == pos[i - 1])
			{
				continue;
			}
			tmp = fast_pow(x, pos[i] - pos[i - 1] - 1);
			ans *= (1 - tmp.mat[0][0]);
		}
		printf("%.7f\n", ans);
	}
	return 0;
}

抱歉!评论已关闭.