题目链接

BZOJ4031

题解

第一眼:这不裸的矩阵树定理么

第二眼:这个模\(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的房间 【矩阵树定理 + 高斯消元】的更多相关文章

  1. bzoj4031 [HEOI2015]小Z的房间——矩阵树定理

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4031 矩阵树定理的模板题(第一次的矩阵树定理~): 有点细节,放在注释里了. 代码如下: # ...

  2. 【bzoj4031】[HEOI2015]小Z的房间 矩阵树定理

    题目描述 你突然有了一个大房子,房子里面有一些房间.事实上,你的房子可以看做是一个包含n*m个格子的格状矩形,每个格子是一个房间或者是一个柱子.在一开始的时候,相邻的格子之间都有墙隔着. 你想要打通一 ...

  3. [HEOI2015]小Z的房间(矩阵树定理学习笔记)

    题目描述 你突然有了一个大房子,房子里面有一些房间.事实上,你的房子可以看做是一个包含n*m个格子的格状矩形,每个格子是一个房间或者是一个柱子.在一开始的时候,相邻的格子之间都有墙隔着. 你想要打通一 ...

  4. BZOJ 4031: [HEOI2015]小Z的房间 [矩阵树定理 行列式取模]

    http://www.lydsy.com/JudgeOnline/problem.php?id=4031 裸题........ 问题在于模数是$10^9$ 我们发现消元的目的是让一个地方为0 辗转相除 ...

  5. BZOJ 4031: [HEOI2015]小Z的房间 (矩阵树定理 板题)

    背结论 : 度-邻 CODE1 O(n3logn)O(n^3logn)O(n3logn) #include <bits/stdc++.h> using namespace std; typ ...

  6. [HEOI2015] 小Z的房间 - 矩阵树定理

    #include <bits/stdc++.h> using namespace std; #define int long long const int N = 105; const i ...

  7. bzoj 4031: 小Z的房间 矩阵树定理

    bzoj 4031: 小Z的房间 矩阵树定理 题目: 你突然有了一个大房子,房子里面有一些房间.事实上,你的房子可以看做是一个包含n*m个格子的格状矩形,每个格子是一个房间或者是一个柱子.在一开始的时 ...

  8. LG4111/LOJ2122 「HEOI2015」小Z的房间 矩阵树定理

    问题描述 LG4111 题解 矩阵树定理板子题. \(\mathrm{Code}\) #include<bits/stdc++.h> using namespace std; #defin ...

  9. [spoj104][Highways] (生成树计数+矩阵树定理+高斯消元)

    In some countries building highways takes a lot of time... Maybe that's because there are many possi ...

随机推荐

  1. windows服务器配置tomcat开机自动启动

    背景:最近在做服务器部署的工作,开始的时候都是手动将tomcat进行启动的,但是在遇到几次服务器重启后发现这样太过麻烦,影响项目运行,故事就从这里开始了. 我们的项目是使用spring-boot进行开 ...

  2. OCCI的迭代修改

    传统的在执行多行DML(INSERT.UPDATE.DELETE)时,我们是多次调用executeUpdate():注意!当我们调用一次此函数时,则执行一次网络往返,当数据量大时则效率非常低.不过 O ...

  3. Javascript简单特效及摘要

    1.js中的Element对象 ** var input1=docuemnt.getElementById("input1"); //alert(input1.value); // ...

  4. 【Python3】操作文件,目录和路径

    1.遍历文件夹和文件  Python代码   import os import os.path rootdir = "d:/test" for parent,dirnames,fi ...

  5. 【jQuery】手机验证码倒计时效果

    <ul class="ulist"> <li class="group"> <label class="label&qu ...

  6. 细说 unicode 、utf-8 、utf-16、ascii 、gbk 、gb2312

    一.计算机的由来 很久很久以前,有一群人,他们决定用8个可以开合的晶体管来组合成不同的状态,以表示世界上的万物.他们看到8个开关状态是好的,于是他们把这称为”字节“.再后来,他们又做了一些可以处理这些 ...

  7. openwrt(三) 固件的烧录

    导航: 方法1: tftp: 方法2: 在线升级 方法3: BIOS烧录 方法1:TFTP 这应该是最万能的一种方法了.TFTP是一种依靠网口传送数据的一种通信协议,没错,只是传输数据,并不是烧录,所 ...

  8. tar命令,vi编辑器

    一.将用户信息数据库文件和组信息数据库文件纵向合并为一个文件/1.txt(覆盖): [root@localhost /]# cat /etc/passwd /etc/group > 1.txt ...

  9. UIView和CALayer是什么关系?

    UIView显示在屏幕上归功于CALayer,通过调用drawRect方法来渲染自身的内容,调节CALayer属性可以调整UIView的外观,UIView继承自UIResponder,比起CALaye ...

  10. react事件处理及动态样式添加

    多数据的事件绑定,循环数据来进行绑定.如下方式就是循环绑定事件的基本代码: this.state.lists.map(function(value,index,array){//代码片段}.bind( ...