BZOJ4031 [HEOI2015]小Z的房间 【矩阵树定理 + 高斯消元】
题目链接
题解
第一眼:这不裸的矩阵树定理么
第二眼:这个模\(10^9\)是什么鬼嘛QAQ
想尝试递归求行列式,发现这是\(O(n!)\)的。。
想上高斯消元,却又处理不了逆元这个东西、、
无奈去翻题解,,,
发现可以用类似辗转相除法去消,而避免除法,,,
这样子依旧是每次一行减去另一行乘一个数,行列式不变
orz
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
using namespace std;
const int maxn = 105,maxm = 100005,INF = 1000000000,P = 1000000000;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
int visx[maxn],A[maxn][maxn],n,m,N,id[maxn][maxn];
int X[4] = {0,0,1,-1},Y[4] = {-1,1,0,0};
char s[maxn][maxn];
int Gause(){
int tag = 1;
for (int i = 1; i < N; i++){
int j = i;
for (int k = i + 1; k < N; k++)
if (abs(A[k][i]) > abs(A[j][i])) j = k;
if (j != i){
for (int k = i; k < N; k++) swap(A[i][k],A[j][k]);
tag = -tag;
}
for (j = i + 1; j < N; j++){
while (A[j][i]){
int tmp = A[j][i] / A[i][i];
for (int k = i; k < N; k++)
A[j][k] = ((A[j][k] - 1ll * tmp * A[i][k] % P) % P + P) % P;
if (A[j][i]){
for (int k = i; k < N; k++)
swap(A[i][k],A[j][k]);
tag = -tag;
}
}
}
}
int re = 1;
for (int i = 1; i < N; i++)
re = 1ll * re * A[i][i] % P;
return (re * tag % P + P) % P;
}
int main(){
n = read(); m = read();
REP(i,n){
scanf("%s",s[i] + 1);
REP(j,m) if (s[i][j] == '.') id[i][j] = ++N;
}
REP(i,n) REP(j,m){
if (s[i][j] == '*') continue;
int nx,ny,u = id[i][j];
for (int k = 0; k < 4; k++){
nx = i + X[k];
ny = j + Y[k];
if (nx < 1 || ny < 1 || nx > n || ny > m || s[nx][ny] == '*') continue;
A[u][u]++;
A[u][id[nx][ny]] = -1;
}
}
printf("%d\n",Gause());
return 0;
}
BZOJ4031 [HEOI2015]小Z的房间 【矩阵树定理 + 高斯消元】的更多相关文章
- bzoj4031 [HEOI2015]小Z的房间——矩阵树定理
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4031 矩阵树定理的模板题(第一次的矩阵树定理~): 有点细节,放在注释里了. 代码如下: # ...
- 【bzoj4031】[HEOI2015]小Z的房间 矩阵树定理
题目描述 你突然有了一个大房子,房子里面有一些房间.事实上,你的房子可以看做是一个包含n*m个格子的格状矩形,每个格子是一个房间或者是一个柱子.在一开始的时候,相邻的格子之间都有墙隔着. 你想要打通一 ...
- [HEOI2015]小Z的房间(矩阵树定理学习笔记)
题目描述 你突然有了一个大房子,房子里面有一些房间.事实上,你的房子可以看做是一个包含n*m个格子的格状矩形,每个格子是一个房间或者是一个柱子.在一开始的时候,相邻的格子之间都有墙隔着. 你想要打通一 ...
- BZOJ 4031: [HEOI2015]小Z的房间 [矩阵树定理 行列式取模]
http://www.lydsy.com/JudgeOnline/problem.php?id=4031 裸题........ 问题在于模数是$10^9$ 我们发现消元的目的是让一个地方为0 辗转相除 ...
- BZOJ 4031: [HEOI2015]小Z的房间 (矩阵树定理 板题)
背结论 : 度-邻 CODE1 O(n3logn)O(n^3logn)O(n3logn) #include <bits/stdc++.h> using namespace std; typ ...
- [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个格子的格状矩形,每个格子是一个房间或者是一个柱子.在一开始的时 ...
- LG4111/LOJ2122 「HEOI2015」小Z的房间 矩阵树定理
问题描述 LG4111 题解 矩阵树定理板子题. \(\mathrm{Code}\) #include<bits/stdc++.h> using namespace std; #defin ...
- [spoj104][Highways] (生成树计数+矩阵树定理+高斯消元)
In some countries building highways takes a lot of time... Maybe that's because there are many possi ...
随机推荐
- MySql开启GTID和多线程复制功能
1.修改参数 master: gtid_mode = ON --开启gtid这个必须打开 enforce-gtid-consistency = ON ...
- LNMP+HAProxy+Keepalived负载均衡 - 基础服务准备
日志服务 修改日志服务配置并重启日志服务: ``` vim /etc/rsyslog.conf ``` 编辑系统日志配置,指定包含的配置文件路径和规则: ``` $IncludeConfig /etc ...
- Notepad++安装SVN插件
第一种方法,在插件管理中安装: 插件->Plugin Manager->show plugin manager->找到subversion->install; 第二种方法,直接 ...
- POJ3682 概率DP
King Arthur's Birthday Celebration Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3575 ...
- 调整图像的亮度和对比度—opencv
1.理论基础 两个参数 和 一般称作 增益 和 偏置 参数.我们往往用这两个参数来分别控制 对比度 和 亮度 . 你可以把 看成源图像像素,把 看成输出图像像素.这样一来,上面的式子就能写得更 ...
- POJ:2377-Bad Cowtractors
传送门:http://poj.org/problem?id=2377 Bad Cowtractors Time Limit: 1000MS Memory Limit: 65536K Total Sub ...
- dex2oat 加载多次
我是一个做插件开发的人员,插件就是动态加载dex文件. 然后4.4以后,Android dalvik虚拟机,和ART. art运行的必须要把dex转换为oat elf文件. 然后,这个dex2oat需 ...
- android stadio svn 使用技巧
有时候有这样的需求: 就是我一次要改很多的需求,然后代码要分开提交,那么怎么办? 提交的时候一个一个的点开看? 比如:这次改的还没有提上去,又来了一个需求,怎么区分呢 新建一个active的变化列表 ...
- Java 注解(Annoation)学习笔记
1 Junit中的@Test为例: 1.1 用注解(@Test)前 private boolean isTestMethod(Method m) { return m.getParameterType ...
- 《Cracking the Coding Interview》——第17章:普通题——题目4
2014-04-28 22:32 题目:不用if语句或者比较运算符的情况下,实现max函数,返回两个数中更大的一个. 解法:每当碰见这种无聊的“不用XXX,给我XXX”型的题目,我都默认处理的是int ...