zoukankan      html  css  js  c++  java
  • bzoj4031 [HEOI2015]小Z的房间

    [HEOI2015]小Z的房间

    Time Limit: 10 Sec Memory Limit: 256 MB

    Description

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

    你想要打通一些相邻房间的墙,使得所有房间能够互相到达。在此过程中,你不能把房子给打穿,或者打通柱子(以及柱子旁边的墙)。同时,你不希望在房子中有小偷的时候会很难抓,所以你希望任意两个房间之间都只有一条通路。现在,你希望统计一共有多少种可行的方案。

    Input

    第一行两个数分别表示n和m。

    接下来n行,每行m个字符,每个字符都会是’.’或者’’,其中’.’代表房间,’’代表柱子。

    Output

    一行一个整数,表示合法的方案数 Mod 10^9

    Sample Input

    3 3

    ...

    ...

    .*.

    Sample Output

    15

    HINT

    对于前100%的数据,n,m<=9




    矩阵树定理裸题。。。
    由于没有逆元,所以要辗转相除QAQ

    
    #include<bits/stdc++.h>
    #define mod 1000000000
    using namespace std;
    const int maxn = 105;
    long long a[maxn][maxn];
    char mp[maxn][maxn];
    int n, m, cnt, id[maxn][maxn];
    int x[] = {0, 1, -1, 0, 0};
    int y[] = {0, 0, 0, 1, -1};
    
    inline bool check(int a, int b){
    	return (0 < a && a <= n && 0 < b && b <= m && mp[a][b] == '.');
    }
    
    inline long long calc(int N){
    	long long f = 1, ans = 1;
    	for(int i = 1; i <= N; ++i)
    		for(int j = 1; j <= N; ++j)
    			a[i][j] = (a[i][j] + mod) % mod;
    	for(int i = 1; i <= N; ++i){
    		for(int j = i + 1; j <= N; ++j){
    			long long A = a[i][i], B = a[j][i];
    			while(B){
    				long long t = A / B; A %= B; swap(A, B);
    				for(int k = i; k <= N; ++k){
    					a[i][k] = (a[i][k] - t * a[j][k] % mod + mod) % mod;
    					swap(a[i][k], a[j][k]);
    				}
    				f = -f;
    			}
    		}	
    		if(!a[i][i]) return 0; ans = ans * a[i][i] % mod;	
    	}
    	return (f == 1) ? (ans) : (mod - ans) % mod;
    }
    
    int main()
    {
    	scanf("%d%d", &n, &m);
    	for(int i = 1; i <= n; ++i) scanf("%s", mp[i] + 1);
    	for(int i = 1; i <= n; ++i)
    		for(int j = 1; j <= m; ++j)
    			if(mp[i][j] == '.') id[i][j] = ++cnt;
    	for(int i = 1; i <= n; ++i)
    		for(int j = 1; j <= m; ++j)
    			if(mp[i][j] == '.'){
    				for(int k = 1; k <= 4; ++k)
    					if(check(i + x[k], j + y[k])){
    						a[id[i][j]][id[i][j]]++; a[id[i][j]][id[i + x[k]][j + y[k]]]--;			
    					}
    			}
    	cout << calc(cnt - 1) << endl;
    	return 0;
    }
    
    
    心如花木,向阳而生。
  • 相关阅读:
    ajax提交请求返回对象异常问题
    Rhino+envjs-1.2.js 在java运行网站js 工具类
    CryptoJS遇到的小坑
    BT是如何下载的
    NPOI 复制Word中的表格内容, 操作Word表格
    使用Scapy框架进行PPPOE拨号密码截取
    用Python养一只DHT爬虫
    如何解决jquery版本冲突
    安装ECMall后报PHP Strict Standards错误,请问如何解决
    如何在Webstorm/Phpstorm中设置连接FTP,并快速进行文件比较,上传下载,同步等操作(远程开发)
  • 原文地址:https://www.cnblogs.com/LLppdd/p/9703732.html
Copyright © 2011-2022 走看看