题目链接:POJ3374
Scout YYF I
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 4626 | Accepted: 1214 |
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].
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个地雷区,YYF最开始在1处,每次可以向前走动一步或者两步,走一步的概率是p,走两步的概率是1-p,问YYF能够安全度过雷区的概率。
思路:假设dp[i]表示到达i点的概率,很容易可以得到,到任意一点的概率dp[i] = p*dp[i-1]+(1-p)*dp[i-2]. 然而这道题的数据到达10^8,就考虑用矩阵快速幂计算:
| dp[i] dp[i-1] | * | p 1 | = | dp[i+1] dp[i] |
| dp[i-1] dp[i-2] | | 1-p 0 | | dp[i] dp[i-1] |
更为关键的是处理 在每一个雷区都不能踩中 才能安全度过雷区。
可以将雷区分段处理,一次处理一个雷区,从1到第一个雷区为一段,从第一个到第二个雷区……分段处理。
每次处理后面的雷区时,起点就是上一个雷区。
详见代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iostream>
using namespace std;
struct Martix{
double t[2][2];
}d;
int map[12];
int n;
double p;
Martix mul(Martix a,Martix b){
Martix c;
double tmp;
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
{
tmp = 0;
for (int k = 0; k < 2; k++)
{
tmp += a.t[i][k]*b.t[k][j];
}
c.t[i][j] = tmp;
}
return c;
}
Martix quick_pow(Martix a,int m){
Martix ans;
ans.t[0][0] = 1.0; ans.t[0][1] = 0.0;
ans.t[1][0] = 0.0; ans.t[1][1] = 1.0;
while(m){
if(m&1){
ans = mul(ans,a);
}
a = mul(a,a);
m>>=1;
}
return ans;
}
int main(){
while(scanf("%d %lf",&n,&p)!=EOF){
d.t[0][0] = p; d.t[0][1] = 1.0;
d.t[1][0] = 1.0-p; d.t[1][1] = 0.0;
for (int i = 0; i < n; i++){
scanf("%d",&map[i]);
}
sort(map,map+n);
double ans = 1.0;
Martix res; res = quick_pow(d,map[0]-1);
ans *= (1.0 - res.t[0][0]);
for (int i = 1; i < n; i++)
{
if(map[i] == map[i-1])continue;
res = quick_pow(d,map[i]-map[i-1]-1);//求后面概率的时候要从前一个雷区里出发。
ans *= (1.0 - res.t[0][0]);
}
printf("%.7lf\n",ans);
}
return 0;
}