• [BZOJ4031][HEOI2015]小Z的房间


    [BZOJ4031][HEOI2015]小Z的房间

    试题描述

    你突然有了一个大房子,房子里面有一些房间。事实上,你的房子可以看做是一个包含 (n imes m) 个格子的格状矩形,每个格子是一个房间或者是一个柱子。在一开始的时候,相邻的格子之间都有墙隔着。
    你想要打通一些相邻房间的墙,使得所有房间能够互相到达。在此过程中,你不能把房子给打穿,或者打通柱子(以及柱子旁边的墙)。同时,你不希望在房子中有小偷的时候会很难抓,所以你希望任意两个房间之间都只有一条通路。现在,你希望统计一共有多少种可行的方案。

    输入

    第一行两个数分别表示 (n)(m)
    接下来 (n) 行,每行 (m) 个字符,每个字符都会是 . 或者 *,其中 . 代表房间,* 代表柱子。

    输出

    一行一个整数,表示合法的方案数(mathrm{mod} 10^9)

    输入示例

    3 3
    ...
    ...
    .*.
    

    输出示例

    15
    

    数据规模及约定

    对于前 (100\%) 的数据,(n,m le 9)

    题解

    矩阵树定理裸题,来练习一下高斯消元。

    高斯消元可以辗转相除,这样好写。

    注意本题在消元过程中要时时取模,中途可能会爆 long long。

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cctype>
    #include <algorithm>
    using namespace std;
    #define rep(i, s, t) for(int i = (s); i <= (t); i++)
    #define dwn(i, s, t) for(int i = (s); i >= (t); i--)
    
    int read() {
    	int x = 0, f = 1; char c = getchar();
    	while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
    	while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
    	return x * f;
    }
    
    
    #define maxr 10
    #define maxn 100
    #define MOD 1000000000
    #define LL long long
    
    int r, c;
    char Map[maxr][maxr];
    
    int n, p[maxr][maxr];
    int id(int i, int j) { return p[i][j] ? p[i][j] : p[i][j] = ++n; }
    
    LL A[maxn][maxn];
    
    void elim(LL* a, LL* b, int tar) { // target: b[tar] = 0
    	if(!b[tar]) return ;
    	LL rate = a[tar] / b[tar];
    	rep(i, 1, n) a[i] = (a[i] - b[i] * rate % MOD + MOD) % MOD;
    	return elim(b, a, tar);
    }
    void calcDet(LL A[][maxn]) {
    	int sgn = 1;
    	rep(i, 1, n)
    		rep(j, i + 1, n) if(A[j][i]) {
    			elim(A[i], A[j], i);
    			if(!A[i][i]) swap(A[i], A[j]), sgn = -sgn;
    		}
    	LL sum = 1;
    	rep(i, 1, n) sum = sum * A[i][i] % MOD;
    	printf("%lld
    ", (sum * sgn + MOD) % MOD);
    	return ;
    }
    
    int main() {
    	r = read(); c = read();
    	rep(i, 1, r) scanf("%s", Map[i] + 1);
    	
    	rep(i, 1, r) rep(j, 1, c) if(Map[i][j] == '.') id(i, j);
    	rep(i, 1, r) rep(j, 1, c) if(Map[i][j] == '.') {
    		int now = id(i, j), nxt;
    		if(i < r && Map[i+1][j] == '.') {
    			nxt = id(i + 1, j);
    			A[now][nxt] = A[nxt][now] = -1;
    			A[now][now]++; A[nxt][nxt]++;
    		}
    		if(j < c && Map[i][j+1] == '.') {
    			nxt = id(i, j + 1);
    			A[now][nxt] = A[nxt][now] = -1;
    			A[now][now]++; A[nxt][nxt]++;
    		}
    	}
    	
    	n--;
    	// rep(i, 1, n) rep(j, 1, n) printf("%lld%c", A[i][j], j < n ? ' ' : '
    ');
    	calcDet(A);
    	
    	return 0;
    }
    
  • 相关阅读:
    Spring使用Cache
    Spring AOP之切入点指示符
    centos下 php未安装posix扩展 安装phpsh导致的问题fix
    Linus:利用二级指针删除单向链表
    AngularJS学习之 ui router
    AngularJS学习之 angular-file-upload控件使用方法
    AngularJS学习之 登录表单 清爽验证(边学边更新)
    AngularJS图片上传功能实践
    JavaScript基础知识之 每日一题(网上搜罗来滴)
    AngularJS学习之 ngTable 翻页 功能以及利用angular service准备测试数据
  • 原文地址:https://www.cnblogs.com/xiao-ju-ruo-xjr/p/8094580.html
Copyright © 2020-2023  润新知