POJ 3744
矩阵快速乘求概率,不难。但有注意的一点是,一定要注意地雷连着的情况,一旦出现两个雷相邻,就必定为0了。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std; int pos[15]; struct matrix{
double m[3][3];
};
matrix per,pt,ps; matrix operator *(matrix a,matrix b){
matrix c;
for(int i=1;i<=2;i++){
for(int j=1;j<=2;j++){
c.m[i][j]=0;
for(int k=1;k<=2;k++)
c.m[i][j]+=(a.m[i][k]*b.m[k][j]);
}
}
return c;
} matrix quick(int k){
matrix ans=per,p=pt;
while(k){
if(k&1) ans=ans*p;
k>>=1;
p=p*p;
}
return ans;
} int main(){
int n; double p;
while(scanf("%d%lf",&n,&p)!=EOF){
memset(per.m,0,sizeof(per.m));
per.m[1][1]=per.m[2][2]=1;
memset(pt.m,0,sizeof(pt.m));
pt.m[1][1]=p;pt.m[2][1]=1; pt.m[1][2]=1-p;
memset(ps.m,0,sizeof(ps.m));
ps.m[1][1]=p; ps.m[2][1]=1;
for(int i=1;i<=n;i++)
scanf("%d",&pos[i]);
sort(pos+1,pos+n+1);
if(pos[1]==1){
printf("%.7lf\n",0);
continue;
}
pos[0]=0;
matrix tmp;
double ans=1;
for(int i=1;i<=n;i++){
int g=pos[i]-1-pos[i-1]-1;
if(g>0){
tmp=quick(g-1);
tmp=tmp*ps;
ans=ans*tmp.m[1][1];
}
if(pos[i+1]==pos[i]+1){
ans=0;
break;
}
ans*=(1-p);
}
printf("%.7lf\n",ans);
}
return 0;
}
POJ 3744的更多相关文章
- poj 3744 Scout YYF I(递推求期望)
poj 3744 Scout YYF I(递推求期望) 题链 题意:给出n个坑,一个人可能以p的概率一步一步地走,或者以1-p的概率跳过前面一步,问这个人安全通过的概率 解法: 递推式: 对于每个坑, ...
- 矩阵快速幂+概率DP poj 3744
题意:在一条不满地雷的路上,你现在的起点在1处.在N个点处布有地雷,1<=N<=10.地雷点的坐标范围:[1,100000000]. 每次前进p的概率前进一步,1-p的概率前进1-p步.问 ...
- poj 3744 Scout (Another) YYF I - 概率与期望 - 动态规划 - 矩阵快速幂
(Another) YYF is a couragous scout. Now he is on a dangerous mission which is to penetrate into th ...
- POJ 3744 Scout YYF I(矩阵快速幂优化+概率dp)
http://poj.org/problem?id=3744 题意: 现在有个屌丝要穿越一个雷区,雷分布在一条直线上,但是分布的范围很大,现在这个屌丝从1出发,p的概率往前走1步,1-p的概率往前走2 ...
- POJ 3744 Scout YYF I 概率dp+矩阵快速幂
题目链接: http://poj.org/problem?id=3744 Scout YYF I Time Limit: 1000MSMemory Limit: 65536K 问题描述 YYF is ...
- POJ 3744 Scout YYF I:概率dp
题目链接:http://poj.org/problem?id=3744 题意: 有n个地雷,位置为pos[i]. 在每个位置,你向前走一步的概率为p,向前走两步的概率为1-p. 你的初始位置为1. 问 ...
- 【POJ 3744】 Scout YYF I
[题目链接] http://poj.org/problem?id=3744 [算法] 概率DP + 矩阵乘法 [代码] #include <algorithm> #include < ...
- POJ 3744 Scout YYF I
分段的概率DP+矩阵快速幂 Scout YYF I Time Limit: 1000MS Memory Limit: 65536K Total Sub ...
- Scout YYF I(POJ 3744)
Scout YYF I Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5565 Accepted: 1553 Descr ...
- poj 3744 Scout YYF I (矩阵)
Description YYF -p. Here is the task, given the place of each mine, please calculate the probality t ...
随机推荐
- Android开发之使用BroadcastReceiver实现开机自己主动启动(源码分享)
上一节已经介绍过BroadcastReceiver实现实时监听电量的功能,这节就来介绍一下假设实现开机自己主动启动的功能.这个比监听电量还简单不少 (1)在清单文件注冊权限 <uses-perm ...
- NYOJ 298
利用矩阵来做变换,参考Max大神的思想的,虽然不是同一道题. ----------- 给定n个点,m个操作,构造O(m+n)的算法输出m个操作后各点的位置.操作有平移.缩放.翻转和旋转 这里的操 ...
- [HTML 5] aria-hidden
You want to use aria-hidden to prevent screen reader to access some content should be hidden from us ...
- 建议53:用状态模式美化代码,关于python-state工具包的理解
在<编写高质量代码:改善python程序的91个建议>的建议53:用状态模式美化代码小节中,介绍了状态模式例如以下:就是当一个对象的内在状态改变时,同意改变其行为,但这个对象看起来 ...
- Loadrunner得到server參数
首先你得确定你所监视的server与你的測试机是在同一个局域网内, 监控windows系统: 1.监视连接前的准备工作 1)进入被监视windows系统.开启下面二个服务Remote ...
- Android 开发最佳实践
原文地址:https://github.com/futurice/android-best-practices/blob/master/translations/Chinese/README.cn.m ...
- java-类生命周期(二)
上文介绍了java-类生命周期(一),理论性比較强.认为太复杂的同学,瞟一下本文的样例加深理解. 先给道题目,看看答对没. /** * 类载入试验基类 * * @author peter_wang * ...
- Ural 1152 False Mirrors(状压DP)
题目地址:space=1&num=1152">Ural 1152 初学状压DP,原来状压仅仅是用到了个位运算.. 非常水的状压DP.注意四则运算的优先级是高于位运算的..也就是 ...
- Codeforces 558B Amr and The Large Array
B. Amr and The Large Array time limit per test 1 second memory limit per test 256 megabytes input st ...
- Vmware VM共享
打开虚拟机,设置,选项点击共享文件夹 点击启用,将电脑windows的目录添加过来 进入终端即可以进入