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] ...
随机推荐
- 2019牛客暑期多校训练营(第八场)-C CDMA(递归构造)
题目链接:https://ac.nowcoder.com/acm/contest/888/C 题意:输入m(为2的n次幂,n<=10),构造一个m*m的矩阵满足任意不同的两行的元素乘积和为0. ...
- [转帖] ./demoCA/newcerts: No such file or directory openssl 生成证书时问题的解决.
接上面一篇blog 发现openssl 生成server.crt 时有问题. 找了一个网站处理了一下: http://blog.sina.com.cn/s/blog_49f8dc400100tznt. ...
- Oracle- 数据库知识回顾
数据库知识回顾: sql语句的语法规范: sql语句不区分大小写,习惯上系统的保留字.关键字.函数名称大写,表名和列名小写 使用空格和换行分隔单词效果一样,一般情况下独立的整句不换行,不同的子句需要换 ...
- python入门小结
以下划线开头的标识符是有特殊意义的.以单下划线开头(_foo)的代表不能直接访问的类属性,需通过类提供的接口进行访问,不能用"from xxx import *"而导入: 以双下划 ...
- C++学习 之 程序的组成部分(部分知识笔记)
1.预处理器编译指令#include: 预处理器是在程序编译前运行的工具.预处理器编译指令是向预处理器发送的命令,总是以#为标识,include便是其中常见的一种,用于引用文件,比如:iostream ...
- 后缀数组练习2:可重叠的k次最长重复子串
其实和上一题是差不多的,只是在二分check的时候有一些小小的改动 1468: 后缀数组2:可重叠的k次最长重复子串 poj3261 时间限制: 1 Sec 内存限制: 128 MB提交: 113 ...
- Open-falcon监控
https://book.open-falcon.org/zh_0_2/ 本文档记录了CentOS7.4下open-falcon-v2监控系统的部署流程,以及一些需要注意的地方. 环境准备 安装Red ...
- 树莓派和STM32通过USB和串口通信记录
不管怎样,为了简便开发,通信选择串口通信. 推荐文章:https://blog.csdn.net/magnetoooo/article/details/53564797 推荐测试工具:https:// ...
- nignx简单介绍
Nginx的产生 没有听过Nginx?那么一定听过它的"同行"Apache吧!Nginx同Apache一样都是一种WEB服务器.基于REST架构风格,以统一资源描述符(Unifor ...
- Spring 配置文件注入
一.Spring配置文件注入 package com.zxguan.demo; public class Person { private String name; private int age; ...