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 1 0.5 2 2 0.5 2 4 Sample Output 0.5000000 0.2500000 Source POJ Monthly Contest - 2009.08.23, Simon |
思路来源于:博博(题解推荐)
题意:
你在一条布满地雷的道路上,开始在坐标1。每次有概率P向前走一步,有概率1-P向前走两步。道中路某几个点上会有地雷,问你安全通过的概率。地雷数N<=10,坐标范围在100000000内。
思路:
第一次接触矩阵快速幂,其实就是二进制的思想吧,留个纪念。
dp[i]=dp[i-1]*p+dp[i-2]*(1-p);
构造矩阵
| P ,1-P |
| 1 , 0 |
那么有
|dp[n] | |p 1-p|^n-1 |dp[1]|
= *
|dp[n-1]| |1 0| |dp[0]|
然后将每个a[i]+1到a[i+1]看做一段单独处理就行。
代码:
#include#include #include #include #include #include #include