Scout YYF I (概率+矩阵快速幂)

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

题意:一条长路有 N (1 ≤ N ≤ 10)颗地雷,一个人走一步的概率是 p ,走两步的概率是 (1-p) ,然后给出 N 颗地雷的位置 ,问这个人安全走过所有地雷的概率是多少

题解:对于一个位置x,设能走到的概率是 P(x) ,那么 P(x) = P(x-1)*p + P(x-2)*(1-p) 这个数x可能很大,所以需要矩阵快速幂然后将整个的路看成由地雷分割的 N 段路[0 -- x1][x1+1 -- x2][x2+1 -- x3]... ...所以,他能安全过去的概率就是 N 段都能过去的连乘

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <algorithm>
 4 using namespace std;
 5 #define MAXN 12
 6
 7 int n;
 8 double p;
 9 int bomb[MAXN];
10
11 double base[2][2];
12 double res[2][2];
13
14 //[ p(x)   ]  =  [ p , 1-p ]^(x-1)  * [ 1 ]
15 //[ p(x-1) ]     [ 1 , 0   ]          [ 0 ]
16 void quick_mi(int x)
17 {
18     double tp[2][2];
19     while (x)
20     {
21         if (x%2==1)
22         {
23             for (int i=0;i<2;i++)
24                  for (int j=0;j<2;j++)
25                 {
26                     tp[i][j]=0;
27                     for (int k=0;k<2;k++)
28                         tp[i][j]+=res[i][k]*base[k][j];
29                 }
30             for (int i=0;i<2;i++)
31                 for (int j=0;j<2;j++)
32                 res[i][j]=tp[i][j];
33         }
34         for (int i=0;i<2;i++)
35             for (int j=0;j<2;j++)
36             {
37                 tp[i][j]=0;
38                 for (int k=0;k<2;k++)
39                     tp[i][j]+=base[i][k]*base[k][j];
40             }
41         for (int i=0;i<2;i++)
42             for (int j=0;j<2;j++)
43                 base[i][j]=tp[i][j];
44         x/=2;
45     }
46 }
47
48 double Mi(int x)//处于位置1踩到位置 x 的概率
49 {
50     if (x==0) return 0;
51     base[0][0]=p,base[0][1]=1.0-p;
52     base[1][0]=1,base[1][1]=0;
53     res[0][0]=1;res[0][1]=0;
54     res[1][0]=0;res[1][1]=1;
55     quick_mi(x-1);
56     return res[0][0];
57 }
58
59 int main()
60 {
61     while (scanf("%d%lf",&n,&p)!=EOF)
62     {
63         for (int i=0;i<n;i++)
64             scanf("%d",&bomb[i]);
65         sort(bomb,bomb+n);
66
67         double xxx=Mi(bomb[0]);     //死了的概率
68         double ans = 1.0-xxx;       //没死
69         for (int i=1;i<n;i++)
70         {
71             xxx =Mi(bomb[i]-bomb[i-1]); //化简后
72             ans *= (1.0-xxx);
73         }
74         printf("%.7lf\n",ans);
75     }
76     return 0;
77 }

				
时间: 2024-08-10 17:11:07

Scout YYF I (概率+矩阵快速幂)的相关文章

POJ 3744 Scout YYF I(矩阵快速幂 概率dp)

题目链接:http://poj.org/problem?id=3744 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"

017 ACM-ICPC 亚洲区(西安赛区)网络赛 Coin 概率+矩阵快速幂

题目链接: https://nanti.jisuanke.com/t/17115 题意: 询问硬币K次,正面朝上次数为偶数. 思路: dp[i][0] = 下* dp[i-1][0] + 上*dp[i-1][1] (满足条件的) dp[i][1]= 上*dp[i-1][0] + 下*dp[i-1][1] (不满足条件的) 矩阵优化这个DP #include <bits/stdc++.h> using namespace std; typedef long long LL; const LL m

poj 3744 Scout YYF I (概率DP+矩阵快速幂)

Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5062   Accepted: 1370 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

poj 3744 Scout (Another) YYF I - 概率与期望 - 动态规划 - 矩阵快速幂

(Another) 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, (Another) YYF is now at the start of enemy's famous "mine road". This is a very long road,

POJ 3744 Scout YYF I 矩阵快速幂优化--概率dp

点击打开链接 Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5416   Accepted: 1491 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 diffic

POJ 3744 Scout YYF I (概率DP+矩阵快速幂)

题意:小明要从1走过一段直线雷区,给定n个地雷的坐标,他走一步的概率是p,两步的概率为1-p,问你他能安全通过雷区的概率. 析:很明显这是一个概率DP,用d(i)表示到达 i 时他安全的概率,那么d[i] = p * d[i-1] + (1-p) * d[i-2];这个状态转移方程很好理解, 就是说要想到达 i 要么从第 i-1 走一步,要么从 i-2 走两步,最后加起来,然后问题来了,这个数可能达到 1E8,那么时间空间复杂度都受不了, 观察这个状态转移方程,是不是很像Fibnacci数列,所

[poj3744]Scout YYF I(概率dp+矩阵快速幂)

题意:在一维空间上存在一些雷,求安全通过的概率.其中人有$p$的概率前进一步,$1-p$的概率前进两步. 解题关键:若不考虑雷,则有转移方程:$dp[i] = p*dp[i - 1] + (1 - p)*dp[i - 2]$ 由于雷的数量很少,所以可以以雷为界,将区域分开,在每个区域中,通过该段的概率等于1-踩到该段终点的地雷的概率.然后用矩阵快速幂优化一下即可 1 #include<cstdio> 2 #include<cstring> 3 #include<algorit

Scout YYF I POJ - 3744(概率dp + 矩阵快速幂)

题意: 一条路上有n个地雷,你从1开始走,单位时间内有p的概率走一步,1-p的概率走两步,问安全通过这条路的概率 解析: 很容易想到 dp[i] = p * dp[i-1] + (1 - p) * dp[i]; 然而...t,但这个式子明显可以用矩阵快速幂加个氮气一下加速一下... 把所有的点输入之后 sort一下,那么就能把这条路分成很多段 每一段以地雷为分界线 1 - x[0]  x[0]+1 - x[1]  x[1]+1 - x[2] ````````` 然后求出安全通过每一段的概率  

POJ3744——概率DP 矩阵快速幂优化——Scout YYF I

http://poj.org/problem?id=3744 矩阵快速幂: 利用DP的递推式 就本题来说 dp[i] = p*dp[i-1] + (1-p)*dp[i-2] 由于x非常大最大1亿,这样的话复杂度就为1亿 所以这里可以用矩阵的思想 [dp[i]   dp[i-1] ] = [ dp[i-1]  dp[i-2] ] | p   1 - p| | 1      0  | 递推得到 n - 1 [dp[n]   dp[n-1]] = [dp[1]   dp[2] ] |p   1 - p