Scout YYF I
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 10404 | Accepted: 3056 |
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
思路:
dp[i] = p * dp[i - 1] + (1 - p)*dp[i - 2], 用矩阵分段优化
代码:
#include<iostream>
#include<cstdio>
using namespace std;
struct node
{
double t[2][2];
}x, y;
int n;
double p;
node cheng(node a, node b)
{
node r;
for (int i = 0; i<2; i++)
{
for (int j = 0; j < 2; j++)
{
r.t[i][j] = 0;
for (int k = 0; k<2; k++)
r.t[i][j] += a.t[i][k] * b.t[k][j];
}
}
return r;
}
void work(int b)
{
y.t[0][0] = p; y.t[0][1] = 1;
y.t[1][0] = 1 - p; y.t[1][1] = 0;
x.t[0][0] = 1; x.t[0][1] = 0;
x.t[1][0] = 0; x.t[1][1] = 0;
while (b)
{
if (b & 1) x = cheng(x, y);
y = cheng(y, y);
b = b / 2;
}
}
int main()
{
while (cin >> n >> p)
{
int a[15]; a[0] = 0;
for (int i = 1; i <= n; i++)
cin >> a[i];
double ans = 1.00;
for (int i = 1; i <= n; i++)
{
work(a[i] - a[i - 1]-1);
ans = ans * (1 - x.t[0][0]);
}
printf("%.7lf\n", ans);
}
return 0;
}
ScoutYYFI:安全穿越雷区的概率计算
本文介绍了一种使用动态规划和矩阵优化的方法来计算角色YYF在敌方矿路上安全穿越的概率。通过解析输入的雷区位置和行走概率,利用dp[i]=p*dp[i-1]+(1-p)*dp[i-2]公式进行矩阵分段优化,最终输出精确到小数点后7位的安全穿越概率。
607

被折叠的 条评论
为什么被折叠?



