poj 3744 Scout YYF I (矩阵乘法+概率与期望DP)
来源:互联网 发布:js对象定义 编辑:程序博客网 时间:2024/04/29 16:26
Scout YYF I
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 8237 Accepted: 2428
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.522 0.52 4
Sample Output
0.50000000.2500000
Source
POJ Monthly Contest - 2009.08.23, Simon
[Submit] [Go Back] [Status] [Discuss]
题解:矩阵乘法+概率与期望DP
对于不是地雷的位置,我们用矩阵乘法来优化DP
f[i]=f[i-1]*p+f[i-2]*p
对于是地雷的位置x,
f[x+1]=f[x-1]*(1-p)
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespace std;int n,a[20]; double p;struct data{double a[3][3];}e,c,f;void clear(data &a){for (int i=1;i<=2;i++) for (int j=1;j<=2;j++) a.a[i][j]=e.a[i][j];}data mul(data a,data b){data c;for (int i=1;i<=2;i++) for (int j=1;j<=2;j++) { c.a[i][j]=0; c.a[i][j]+=a.a[i][1]*b.a[1][j]; c.a[i][j]+=a.a[i][2]*b.a[2][j]; }return c;}data quickpow(data num,int x){data ans; clear(ans);data base; base=num;while (x) {if (x&1) ans=mul(ans,base);x>>=1;base=mul(base,base);}return ans;}int main(){freopen("a.in","r",stdin);//freopen("my.out","w",stdout);for (int i=1;i<=2;i++) e.a[i][i]=1;while (scanf("%d%lf",&n,&p)!=EOF) { c.a[1][1]=0; c.a[1][2]=(1.0-p); c.a[2][1]=1; c.a[2][2]=p; f.a[1][1]=0; f.a[1][2]=1; for (int i=1;i<=n;i++) scanf("%d",&a[i]); sort(a+1,a+n+1); if (a[1]==1) { printf("%.7lf\n",0); continue; } data ans; a[0]=0; bool pd=false; for (int i=1;i<=n;i++) { if (a[i-1]+1==a[i]) { pd=true; break; } data ans=quickpow(c,a[i]-a[i-1]-2); ans=mul(f,ans); f.a[1][1]=0; f.a[1][2]=ans.a[1][2]*(1.0-p); } if (!pd) printf("%.7lf\n",f.a[1][2]); else printf("%.7lf\n",0);}}
0 0
- poj 3744 Scout YYF I (矩阵乘法+概率与期望DP)
- poj 3744 Scout YYF I 概率dp+矩阵乘法
- [概率dp 矩阵乘法] poj 3744 Scout YYF I
- poj 3744 Scout YYF I(矩阵优化概率DP)
- 【POJ】3744 Scout YYF I (概率DP+矩阵优化)
- POJ 3744 Scout YYF I (概率dp+矩阵优化)
- POJ 3744 Scout YYF I (矩阵相乘+概率DP)
- poj 3744 Scout YYF I(概率DP&矩阵快速幂)
- poj 3744 Scout YYF I(概率dp+矩阵快速幂)
- POJ 3744 Scout YYF I 概率DP + 矩阵快速幂
- poj 3744 Scout YYF I(概率dp+矩阵快速幂)
- Poj 3744 Scout YYF I (概率DP 矩阵优化)
- POJ 3744 Scout YYF I 概率dp+矩阵快速幂
- POJ 3744 Scout YYF I 概率dp 矩阵快速幂
- Poj 3744 Scout YYF I(矩阵概率DP)
- POJ 3744 Scout YYF I (概率DP+矩阵快速幂)
- POJ 3744 Scout YYF I 概率dp+矩阵加速
- POJ 3744Scout YYF I 概率DP+矩阵优化
- 正确处理内存出错故障
- 维护嵌入式 Linux 内核——So Easy
- 骨牌铺方格 (sdut oj)
- 欢迎使用CSDN-markdown编辑器
- CAMERA数据的分析(二)------- 数据的向上传输
- poj 3744 Scout YYF I (矩阵乘法+概率与期望DP)
- JavaSE_43th_异常的捕获和处理(try-catch-finally)
- 百度富文本编辑器的上传图片的路径问题
- 通过简单的处理去除WinRAR中文版的广告
- 如何在linux的home目录里面装上另一个linux操作系统?
- 牛客模拟一:序列和
- free()函数具体做了什么
- 校园O2O数据统计
- iOS 带箭头的弹出框