首页 > 代码库 > BNU 4346 Scout YYF I
BNU 4346 Scout YYF I
A. Scout YYF I
Time Limit: 1000ms
Memory Limit: 65536KB
64-bit integer IO format: %lld Java class name: MainYYF 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.522 0.52 4
Sample Output
0.50000000.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 long13 #define INF 0x3f3f3f3f14 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 }
BNU 4346 Scout YYF I
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。