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] ...
随机推荐
- makefile中=、:=和+=的区别
经常有人分不清= .:=和+=的区别 这里我总结下做下详细的分析: 首先你得清楚makefile的运行环境,因为我是linux系统,那么我得运行环境是shell 在Linux的shell里,shel ...
- docker运行报错docker0: iptables: No chain/target/match by that name.
转自:https://blog.csdn.net/wohaqiyi/article/details/84450562 docker运行报错docker0: iptables: No chain/tar ...
- Java更新Oracle的clob类型字段
Java更新Oracle的clob类型字段 1.查询该clob字段 2.处理该clob字段查询结果 3.更新该clob字段查询结果 1.查询该clob字段 <select id="se ...
- 南昌网络赛C.Angry FFF Party
南昌网络赛C.Angry FFF Party Describe In ACM labs, there are only few members who have girlfriends. And th ...
- easyswoole报错:failed: Error during WebSocket handshake: Unexpected response code: 200
WebSocket connection to 'ws://www.xxxx.com/xxx/xx' failed: Error during WebSocket handshake: Unexpec ...
- PHP读取xml并写入数据库示例
<?php $xml = simplexml_load_file('books.xml'); // print_r($xml); // echo "<br/>"; ...
- 【记忆化搜索】Navy maneuvers
[来源]: 2008年哈尔滨区域赛题目 [题目链接]: http://acm.hdu.edu.cn/showproblem.php?pid=2452 [参考博客]: https://blog.csdn ...
- Lua 打印 table (支持双向引用的table)
网上搜了一下,挺多打印table的方案,基本思路都是一层一层递归遍历table.(我就是参考这种思路做的^_^) 但大部分都不支持双向引用的打印.我所指的双向引用,就是a引用b, b又直接或间接引用a ...
- Java EE javax.servlet ServletContainerInitializer接口
ServletContainerInitializer接口 public interface ServletContainerInitializer 一.介绍 该接口,允许在 web 应用程序的启动阶 ...
- 9-Perl 条件语句
1.Perl 条件语句Perl 条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块.注意,数字 0, 字符串 '0' . "" , 空 list ( ...