POJ-3744 Scout YYF I [概率DP][矩阵快速幂]
2017-09-18 10:34
381 查看
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
Sample Output
Source
大意:
给你n个地雷的位置,你每次有p的几率走一步,有(1 - p) 的几率走两步
问成功走完雷区的几率
这道题嘛, 递推式很好推, 但是范围太大明显要T
观察到除了地雷以外的转移都是机械式的,重复的
所以可以用矩阵快速幂优化
将转移割成由地雷分界的子段 1 ~ x1 x1 + 1 ~ x2 x2 + 1 ~ x3 .........
直接转移 算出踩到地雷的几率 P 1 - P 即为通过此段的几率 根据乘法原理将各段几率乘起来即可
Source Code
Source Code
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 9408 | Accepted: 2751 |
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
大意:
给你n个地雷的位置,你每次有p的几率走一步,有(1 - p) 的几率走两步
问成功走完雷区的几率
这道题嘛, 递推式很好推, 但是范围太大明显要T
观察到除了地雷以外的转移都是机械式的,重复的
所以可以用矩阵快速幂优化
将转移割成由地雷分界的子段 1 ~ x1 x1 + 1 ~ x2 x2 + 1 ~ x3 .........
直接转移 算出踩到地雷的几率 P 1 - P 即为通过此段的几率 根据乘法原理将各段几率乘起来即可
Source Code
Problem: 3744 | User: Sakura_ | |
Memory: 284K | Time: 0MS | |
Language: C++ | Result: Accepted |
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
struct Mat
{
double m[2][2];
Mat operator * ( const Mat &a )
{
Mat tag;
for(int i = 0; i < 2; ++i)
for(int j = 0; j < 2; ++j)
{
tag.m[i][j] = 0;
for(int k = 0; k < 2; ++k)
tag.m[i][j] += m[i][k] * a.m[k][j];
}
return tag;
}
};
Mat MFP( Mat a, int b )
{
Mat res;
memset(res.m, 0, sizeof(res.m));
res.m[0][0] = res.m[1][1] = 1;
for(; b; b >>= 1, a = a * a)
if(b & 1) res = res * a;
return res;
}
int n, bmb[15];
double p;
int main()
{
while(cin >> n >> p)
{
for(int i = 1; i <= n; ++i)
cin >> bmb[i];
sort(bmb + 1, bmb + n + 1);
Mat base;
base.m[0][0] = p, base.m[0][1] = 1.0 - p;
base.m[1][0] = 1, base.m[1][1] = 0;
Mat tmp = MFP(base, bmb[1] - 1);
double ans = 1;
ans *= (1.0 - tmp.m[0][0]);
for(int i = 2; i <= n; ++i)
{
if(bmb[i] == bmb[i - 1]) continue;
tmp = MFP(base, bmb[i] - bmb[i - 1] - 1);
ans *= (1.0 - tmp.m[0][0]);
}
printf( "%.7f\n", ans );
}
return 0;
}
/*
1 0.5 2 2 0.5 2 4
*/
相关文章推荐
- POJ 3744 Scout YYF I 矩阵快速幂+概率dp
- poj 3744 Scout YYF I(矩阵快速幂+概率dp)
- [POJ 3744] Scout YYF I · 概率DP & 矩阵快速幂
- POJ 3744 Scout YYF I(矩阵快速幂优化+概率dp)
- poj 3744 Scout YYF I (概率DP&矩阵快速幂)★
- POJ-3744 Scout YYF I (概率DP&&矩阵快速幂)
- POJ 3744 Scout YYF I(矩阵快速幂 概率dp)
- poj 3744 Scout YYF I (矩阵快速幂 优化 概率dp)
- POJ 3744 Scout YYF I (概率dp快速幂优化)
- poj3744之矩阵快速幂+概率DP
- Poj 3744 Scout YYF I (概率DP 矩阵优化)
- POJ-3744 Scout YYF I (矩阵优化概率DP)
- poj 3744 概率dp 矩阵快速幂优化
- Poj 3744 Scout YYF I(矩阵概率DP)
- poj 3744 Scout YYF I(矩阵快速幂优化dp)
- poj 3744(概率dp、矩阵快速幂)
- POJ 3744 Scout YYF I 矩阵快速幂加速dp
- POJ 3744 Scout YYF I (矩阵相乘+概率DP)
- poj 3744 Scout YYF I(概率dp,矩阵优化)
- poj 3744 概率dp 快速幂 注意排序 难度:2