首页 > 代码库 > POJ3744----Scout YYF I
POJ3744----Scout YYF I
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 ofp, 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].
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]
题目要求最后安全通过所有雷的概率,我们分段考虑:
在分别考虑每一个雷的时候,我们知道,本次开始走的点一定是上一个雷的下一个点,考虑到范围很大,所以根据转移方程得到一个系数矩阵,然后快速幂求解(这里真的很牛逼) 因为每次我们只考虑了一段,没有考虑能越过当前雷的前提是前面的雷都要越过,所以要把最后的答案都乘起来才对
设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; }
POJ3744----Scout YYF I
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。