Poj 3744-Scout YYF I (概率dp+矩阵优化)
来源:互联网 发布:淘宝什么叫大v 编辑:程序博客网 时间:2024/05/16 05:13
题目链接:
http://poj.org/problem?id=3744
题目大意:
一个人,一开始处于位置1,在n个位置上有地雷,不能接触,每次人有
分析:
dp[i]表示走到i处的概率的话,可以很快推出
即
可以发现在每段中,情况几乎都相同,而且dp转移式很自然的能想到斐波那契
注意:
- 地雷位置可以为1,即起点
- 地雷读入为乱序
- 地雷可能有重合
- 地雷之间可能连续,即出现2,3连续位置的地雷
代码:
#include<stdio.h>#include<algorithm>#include<iostream>#include<string.h>#include<math.h>using namespace std;typedef long long ll;const int maxn = 207;const int mod = 1000000007;const int Mod = 1000;struct mat{ int r,c; double m[10][10]; mat(){} mat(int _r,int _c):r(_r),c(_c){};};void init(mat &a){ memset(a.m,0,sizeof(a.m));}mat mul(mat a, mat b){ mat tmp(a.r,b.c); for (int i = 1 ; i <= tmp.r; i ++) { for (int j = 1 ; j <= tmp.c ;j ++){ tmp.m[i][j] = 0; for (int k =1 ; k <= a.c ; k ++){ tmp.m[i][j] = (tmp.m[i][j]+(a.m[i][k]*b.m[k][j])); } } } return tmp;}mat QP(mat a ,int n){ mat ans(a.r,a.r),tmp(a.r,a.r); memcpy(tmp.m,a.m,sizeof(tmp.m)); init(ans); for (int i = 1 ; i <= ans.r ; i ++) { ans.m[i][i] = 1; } while (n){ if (n&1) ans = mul(ans,tmp); n >>= 1; tmp = mul(tmp,tmp); } return ans;}void print(mat a){ for (int i = 1 ; i <= a.r ;++ i){ for (int j = 1 ; j <= a.c ; ++j){ printf("%f",a.m[i][j]); if (j==a.c) putchar('\n'); else putchar(' '); } }}int n;double p;int arr[maxn];double dp[100000009];int main(){ while (~scanf("%d%lf",&n,&p)) { mat a(2,2); a.m[1][1] = p; a.m[1][2] = 1; a.m[2][1] = 1-p; a.m[2][2] = 0; for (int i =1 ; i <= n ; i ++) scanf("%d",&arr[i]); sort(arr+1,arr+n+1); if (arr[1] == 1) printf("%.7f\n",0); else { double t = 1; mat ans = QP(a,arr[1]-1); t *= (1-ans.m[1][1]); int f = 1; for (int i = 2 ; i <= n ; i ++) { if (arr[i]==arr[i-1]) continue; if (arr[i]-arr[i-1]==1) { f = 0; printf("%.7f\n",0); break; } ans = QP(a,arr[i]-arr[i-1]-1); t *= (1-ans.m[1][1]); } if (f) printf("%.7f\n",t); } } return 0;}
阅读全文
0 0
- 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+矩阵优化
- 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
- mysql:sql alter (增加表的列属性)
- MATLAB第二课
- 要理解__doPostback函数的用法,首先我们要理解Asp.net 中服务端控件事件是如何触发的。 Asp.net 中在客户端触发服务端事件分为三种情况: 一. WebControls中
- mysql中判断记录是否存在方法比较
- 使用Unity制作GTA武器菜单的简单思路
- Poj 3744-Scout YYF I (概率dp+矩阵优化)
- vue-cli脚手架生成Vue项目
- 2254字母旋转方阵
- 各种连接池介绍及配置详解
- Java 集合系列04之 fail-fast总结(通过ArrayList来说明fail-fast的原理、解决办法)
- Linux下简单部分命令
- "回车"查询事件
- 1038. Recover the Smallest Number (30)
- leetcode 4 Median of Two Sorted Arrays c语言实现