BZOJ 4031: [HEOI2015]小Z的房间 (矩阵树定理 板题)
背结论 : 度-邻
CODE1
O(n3logn)O(n^3logn)O(n3logn)
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
template<class T>inline void read(T &num) {
register char ch; register int flg = 1;
while(!isdigit(ch=getchar()))if(ch=='-')flg=-flg;
for(num=0; isdigit(ch); num=num*10+ch-'0', ch=getchar());
num *= flg;
}
const int MAXN = 85;
const int mod = 1e9;
int n, m, tot, id[10][10];
int a[MAXN][MAXN], d[MAXN][MAXN], g[MAXN][MAXN];
char s[10];
inline void link(int u, int v) {
++d[u][u], ++d[v][v];
++g[u][v], ++g[v][u];
}
inline int Gauss(int N) { //高斯消元成上三角
int ans = 1;
for(int i = 1; i <= N; ++i) {
for(int k = i+1; k <= N; ++k)
while(a[k][i]) { //这里有一个log
int d = a[i][i] / a[k][i];
for(int j = i; j <= N; ++j)
a[i][j] = ((a[i][j] - 1ll * d * a[k][j] % mod) % mod + mod) % mod;
swap(a[i], a[k]), ans = -ans; //注意每交换两行都要取反
}
ans = (1ll * ans * a[i][i] % mod + mod) % mod;
}
return ans;
}
int main() {
read(n), read(m);
for(int i = 1; i <= n; ++i) {
scanf("%s", s+1);
for(int j = 1; j <= m; ++j)
if(s[j] != '*') {
id[i][j] = ++tot;
if(id[i-1][j]) link(tot, id[i-1][j]);
if(id[i][j-1]) link(tot, id[i][j-1]);
}
}
for(int i = 1; i < tot; ++i)
for(int j = 1; j < tot; ++j)
a[i][j] = d[i][j] - g[i][j];
printf("%d\n", (Gauss(tot-1) + mod) % mod);
}
CODE2
O(n3+n2logn)→O(n3)O(n^3+n^2logn)\to O(n^3)O(n3+n2logn)→O(n3)
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
template<class T>inline void read(T &num) {
register char ch; register int flg = 1;
while(!isdigit(ch=getchar()))if(ch=='-')flg=-flg;
for(num=0; isdigit(ch); num=num*10+ch-'0', ch=getchar());
num *= flg;
}
const int MAXN = 85;
const int mod = 1e9;
int n, m, tot, id[10][10];
int a[MAXN][MAXN], d[MAXN][MAXN], g[MAXN][MAXN];
char s[10];
inline void link(int u, int v) {
++d[u][u], ++d[v][v];
++g[u][v], ++g[v][u];
}
inline void kill(int a, int b, int &ii, int &ik, int &ki, int &kk, int &sign) {
ii = 1, ik = 0;
ki = 0, kk = 1;
sign = 1;
while(b) {
(ii -= a/b * ki) %= mod;
(ik -= a/b * kk) %= mod;
a -= a/b * b;
swap(a, b);
swap(ii, ki);
swap(ik, kk);
sign = -sign;
}
}
inline int Gauss(int N) {
int ans = 1, ii, ik, ki, kk, sign;
for(int i = 1; i <= N; ++i) {
for(int k = i+1; k <= N; ++k) if(a[k][i]) {
kill(a[i][i], a[k][i], ii, ik, ki, kk, sign); //先把系数算出来
ans *= sign;
for(int j = i; j <= N; ++j) {
int _aij = (1ll * a[i][j] * ii + 1ll * a[k][j] * ik) % mod;
int _akj = (1ll * a[i][j] * ki + 1ll * a[k][j] * kk) % mod;
a[i][j] = _aij, a[k][j] = _akj;
}
}
ans = 1ll * ans * a[i][i] % mod;
}
return ans;
}
int main() {
read(n), read(m);
for(int i = 1; i <= n; ++i) {
scanf("%s", s+1);
for(int j = 1; j <= m; ++j)
if(s[j] != '*') {
id[i][j] = ++tot;
if(id[i-1][j]) link(tot, id[i-1][j]);
if(id[i][j-1]) link(tot, id[i][j-1]);
}
}
for(int i = 1; i < tot; ++i)
for(int j = 1; j < tot; ++j)
a[i][j] = d[i][j] - g[i][j];
printf("%d\n", (Gauss(tot-1) + mod) % mod);
}
BZOJ 4031: [HEOI2015]小Z的房间 (矩阵树定理 板题)的更多相关文章
- BZOJ 4031: [HEOI2015]小Z的房间 [矩阵树定理 行列式取模]
http://www.lydsy.com/JudgeOnline/problem.php?id=4031 裸题........ 问题在于模数是$10^9$ 我们发现消元的目的是让一个地方为0 辗转相除 ...
- 【bzoj4031】[HEOI2015]小Z的房间 矩阵树定理
题目描述 你突然有了一个大房子,房子里面有一些房间.事实上,你的房子可以看做是一个包含n*m个格子的格状矩形,每个格子是一个房间或者是一个柱子.在一开始的时候,相邻的格子之间都有墙隔着. 你想要打通一 ...
- bzoj4031 [HEOI2015]小Z的房间——矩阵树定理
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4031 矩阵树定理的模板题(第一次的矩阵树定理~): 有点细节,放在注释里了. 代码如下: # ...
- BZOJ 4031 [HEOI2015]小Z的房间(Matrix-Tree定理)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4031 [题目大意] 你突然有了一个大房子,房子里面有一些房间. 事实上,你的房子可以看 ...
- BZOJ.4031.[HEOI2015]小Z的房间(Matrix Tree定理 辗转相除)
题目链接 辗转相除解行列式的具体实现? 行列式的基本性质. //864kb 64ms //裸的Matrix Tree定理.练习一下用辗转相除解行列式.(因为模数不是质数,所以不能直接乘逆元来高斯消元. ...
- [HEOI2015]小Z的房间(矩阵树定理学习笔记)
题目描述 你突然有了一个大房子,房子里面有一些房间.事实上,你的房子可以看做是一个包含n*m个格子的格状矩形,每个格子是一个房间或者是一个柱子.在一开始的时候,相邻的格子之间都有墙隔着. 你想要打通一 ...
- [HEOI2015] 小Z的房间 - 矩阵树定理
#include <bits/stdc++.h> using namespace std; #define int long long const int N = 105; const i ...
- bzoj 4031: 小Z的房间 矩阵树定理
bzoj 4031: 小Z的房间 矩阵树定理 题目: 你突然有了一个大房子,房子里面有一些房间.事实上,你的房子可以看做是一个包含n*m个格子的格状矩形,每个格子是一个房间或者是一个柱子.在一开始的时 ...
- bzoj 4031: [HEOI2015]小Z的房间 轮廓线dp
4031: [HEOI2015]小Z的房间 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 98 Solved: 29[Submit][Status] ...
随机推荐
- SaltStack安装部署
SaltStack安装部署 安装: 一. master: 1. 配置yum源安装 # rpm --import https://repo.saltstack.com/yum/redhat/6/x86_ ...
- MySQL优化——索引
内容来自:https://yq.aliyun.com/articles/214494?utm_content=m_31338 对此我们来详细分析下(也就是大家在面试时需要说的): 场景一,数据表规模不 ...
- 项目四:Java秒杀系统方案优化-高性能高并发实战
技术栈 前端:Thymeleaf.Bootstrap.JQuery 后端:SpringBoot.JSR303.MyBatis 中间件:RabbitMQ.Redis.Druid 功能模块 分布式会话,商 ...
- Photon Server初识(二) ---通过NHibernate 映射数据库
一.下载 NHibernate.dill 官网:https://nhibernate.info 或者通过NuGet下载(详情看上一节) 二.新建一个项目,并引入包 引入包 三.配置(重点) 1.配置x ...
- JavaScript的几种循环方式
JavaScript提供了许多通过LOOPS迭代的方法.本教程解释了现代JAVASCRIPT中各种各样的循环可能性 目录: for forEach do...while while for...in ...
- Java 线程控制
一.线程控制 和线程相关的操作都定义在Thread类中,但在运行时可以获得线程执行环境的信息.比如查看可用的处理器数目(这也行?): public class RunTimeTest { public ...
- jq之display:none与visible:hidden
http://www.cnblogs.com/linxiong945/p/4075146.html 今天学习到jquery的hide()部分时,突然有一个想法,jquery中的隐藏/显示部分的实现是给 ...
- webpack权限控制
const type= "a"; const menusConfig = { a: ["activity"], b: ["activity" ...
- 服务端相关知识学习(五)之Zookeeper leader选举
在上一篇文章中我们大致浏览了zookeeper的启动过程,并且提到在Zookeeper的启动过程中leader选举是非常重要而且最复杂的一个环节.那么什么是leader选举呢?zookeeper为什么 ...
- 你不知道的css各类布局(二)之流体布局、液体布局、栅格布局
流体布局 什么是流 在谈论流体布局之前我们需要知道一件事情就是何为“流”,所谓“流”就是“文档流”,是css中的一种基本定位和布局 概念 流体布局(Liquid/Fluid Layout)指的是利用元 ...