BNU 4346 Scout YYF I

A. Scout YYF I

Time Limit: 1000ms

Memory Limit: 65536KB

64-bit integer IO format: %lld      Java class name: Main

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] = dp[i-1]*p+dp[i-2]*(1-p);表示抵达第i个格子的概率。转化成矩阵相乘

|  p      1-p    |  |    dp[i-1]    |          |   dp[i]     |

|  1      0       |  |    dp[i-2]    |     =   |   dp[i-1]  |

分别求出这个雷区到上一个雷区踩雷的概率,然后求对立事件,就是等于成功越过第一个雷区,成功越过第二个雷区,成功越过第三个雷区。。。。等一系列步骤而完成,根据乘法法则。相乘呗。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #define LL long long
13 #define INF 0x3f3f3f3f
14 using namespace std;
15 double p;
16 int n,d[30];
17 struct Matrix{
18     double m[2][2];
19 };
20 Matrix multi(Matrix a,Matrix b){
21     Matrix c;
22     for(int i = 0; i < 2; i++){
23         for(int j = 0; j < 2; j++){
24             c.m[i][j] = 0.0;
25             for(int k = 0; k < 2; k++)
26                 c.m[i][j] += a.m[i][k]*b.m[k][j];
27         }
28     }
29     return c;
30 }
31 Matrix fast_pow(Matrix base,int index) {
32     Matrix temp;
33     temp.m[0][1] = temp.m[1][0] = 0;
34     temp.m[0][0] = temp.m[1][1] = 1;
35     while(index) {
36         if(index&1) temp = multi(base,temp);
37         index >>= 1;
38         base = multi(base,base);
39     }
40     return temp;
41 }
42 int main() {
43     double ans;
44     int i;
45     while(~scanf("%d %lf",&n,&p)){//double的读入一定要用lf%
46         Matrix temp;
47         temp.m[0][0] = p;
48         temp.m[1][1] = 0;
49         temp.m[1][0] = 1;
50         temp.m[0][1] = 1-p;
51         ans = 1;
52         for(i = 0; i < n; i++)
53             scanf("%d",d+i);
54         sort(d,d+n);
55         Matrix temp2 = fast_pow(temp,d[0]-1);
56         ans *= 1-temp2.m[0][0];
57         for(i = 1; i < n; i++){
58             if(d[i] == d[i-1]) continue;
59             temp2 = fast_pow(temp,d[i]-d[i-1]-1);
60             ans *= 1-temp2.m[0][0];
61         }
62         printf("%.7f\n",ans);
63     }
64     return 0;
65 }

时间: 2024-10-06 12:48:11

BNU 4346 Scout YYF I的相关文章

poj 3744 Scout YYF I(概率dp,矩阵优化)

Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5020   Accepted: 1355 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,

POJ 3744 Scout YYF I 矩阵快速幂

Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4452   Accepted: 1159 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,

POJ 3744:Scout YYF I 概率DP+特征方程+快速幂

Scout YYF I 题目连接: http://poj.org/problem?id=3744 题意: 有个人要到一个叫“mine road”的地方,路线是一条直线,起点在1,路上有N个地雷,坐标在[1, 100000000]之间,这人每次有p(0.25≤p≤0.75)的概率向前走一步,且有1-p的概率向前走两步,问这货安全通过雷区到达"mine road"的概率 题解: 利用特征方程求出通项表达式,要走过有雷的地方f(n+1)=f(n-1)*(1-p).   PS:也可以用矩阵快速

poj 3744 Scout YYF I(矩阵优化概率DP)

Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5153   Accepted: 1404 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,

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)

题目链接: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"

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 n

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 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