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 ...
随机推荐
- 【软件笔记】 ◆笔记·I◆ 各类冷门函数细解
[软件笔记·I] 各类冷门函数细解 ■题外话■ 总觉得作为一个志向远大的 coder (٩(◕‿◕。)۶),我觉得单单只会做题是不够的所以我开始尝试自己编写软件!初入道的我并不知道C++其实并不太适合 ...
- http2.2配置
http: 超文本传输协议,工作在应用层 CentOS 6程序环境:httpd-2.2 配置文件: /etc/httpd/conf/httpd.conf /etc/httpd/conf.d/*.con ...
- springMVC入门一
一.准备工作 eclipse使用maven搭建项目,项目名称为HelloSpringMVC 二.搭建好的项目如下: 项目介绍:实现简单的helloworld 三.具体代码 controller类:He ...
- JAVA / MySql 编程——第二章 初始MySQL
1. MySQL: ● MySQL是一个关系型数据库管理系统,由瑞典MySQL AB公司开发,目前属于Oracle公司. MySQL最流行的关系型数据库管理系统, ...
- SPOJ1026 概率DP
Favorite Dice BuggyD loves to carry his favorite die around. Perhaps you wonder why it's his favorit ...
- [USACO1.5] 回文质数
P1217 Prime Palindromes 题目描述 因为151既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数. 写一个程序来找出范围[a,b](5 < ...
- PHP代码审计5-实战漏洞挖掘-cms后台登录绕过
cms后台登录绕过 练习源码:[来源:源码下载](数据库配置信息有误,interesting) 注:需进行安装 1.创建数据库 2.设置账号密码,连接数据库 3.1 正常登录后台,抓包分析数据提交位置 ...
- 11,flask之--WTForms
WTForms是什么? 相当于django的ModelForm. 在网页中,为了和用户进行信息交互总是不得不出现一些表单.flask设计了WTForm表单库来使flask可以更加简便地管理操作表单数据 ...
- 9,K-近邻算法(KNN)
导引: 如何进行电影分类 众所周知,电影可以按照题材分类,然而题材本身是如何定义的?由谁来判定某部电影属于哪 个题材?也就是说同一题材的电影具有哪些公共特征?这些都是在进行电影分类时必须要考虑的问 题 ...
- HDU 1384 Intervals(差分约束)
Intervals Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...