题目链接

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. 【赛时总结】 ◇赛时·I◇ AtCoder ARC-098

    ◆赛时I◆ ARC-098 ■试题&解析■ ◆本场最水◆ C-Attention 长点儿信心吧-- [AtCoder ARC-098 C] [解析] 既然只存在左右(东西)两个朝向,那么领导右 ...

  2. Centos7 使用LVM进行新加磁盘管理

    centos7使用LVM管理一块新的磁盘   注意!文中凡是带#的都是命令标志.   一些重要概念: LV(Logical Volume)- 逻辑卷, VG(Volumne Group)- 卷组, P ...

  3. python3.X中pickle类的用法(cPickle模块移除了)

    1.python3.x中移除了cPickle模块,可以使用pickle模块代替.最终我们将会有一个透明高效的模块. 2.因为存储的是对象,必须使用二进制形式写进文件 #!/usr/bin/python ...

  4. Laravel Nginx 除 `/` 外所有路由 404

    解决方法 在nginx配置添加以下设置: location / { try_files $uri $uri/ /index.php?$query_string; } 详见Laravel官方文档:htt ...

  5. laravels -- Swoole加速php

    LaravelS是一个胶水项目,用于快速集成Swoole到Laravel,然后赋予它们更好的性能.更多可能性. 环境 : ubuntu16 + nginx + php7.1 + LaravelS搭建高 ...

  6. 《Cracking the Coding Interview》——第4章:树和图——题目8

    2014-03-19 05:04 题目:给定两棵二叉树T1和T2,判断T2是否是T1的子树.子树的定义是,以T1的某个节点(可以是T1的根)作为根节点,得到的这棵树和T2一模一样. 解法:首先可以根据 ...

  7. [转]赵桐正thinkphp教程笔记

    原文:赵桐正thinkphp教程笔记 ,有修改 常用配置 常用配置config.php: <?php return array( //'配置项'=>'配置值' 'URL_PATHINFO_ ...

  8. CSS简易学习笔记

    学习地址:http://www.w3school.com.cn/css/index.asp cnblog不能把格式复制上来,有格式文字版:https://github.com/songzhenhua/ ...

  9. Python 3基础教程1-环境安装和运行环境

    本系列开始介绍Python3的基础教程,为什么要选中Python 3呢?之前呢,学Python 2,看过笨方法学Python,学了不到一个礼拜,就开始用Python写Selenium脚本.最近看到一些 ...

  10. 《Android权威编程指南(The Big Nerd Ranch Guide)(第二版)》12.4挑战练习

    本书第12章是讲解Dialog.12.4挑战练习是在CriminalIntent项目中,再增加一个TimePickerFragment的对话框fragment.通过在CrimeFragment用户界面 ...