zoukankan      html  css  js  c++  java
  • bzoj4031

    4031: [HEOI2015]小Z的房间

    Time Limit: 10 Sec  Memory Limit: 256 MB
    Submit: 823  Solved: 407
    [Submit][Status][Discuss]

    Description

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

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

    Input

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

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

    Output

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

    Sample Input

    3 3
    ...
    ...
    .*.

    Sample Output

    15

    HINT

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

    Source

    矩阵树定理 要用到模意义下的高斯消元

    似乎辗转相除是为了使一个值迅速变小,从而跳过取模

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    const int dx[]={-1,1,0,0},dy[]={0,0,-1,1};
    #define N 110
    #define mod 1000000000
    int n,m,tot;
    int c[N][N],d[N][N],a[N][N],p[N][N];
    void gauss(int n)
    {
        ll ans=1;
        for(int i=1;i<=n;i++)
        {
            for(int j=i+1;j<=n;j++)
            {
                ll A=a[i][i],B=a[j][i];
                while(B)
                {
                    ll 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;
                    for(int k=i;k<=n;k++) swap(a[i][k],a[j][k]);
                    ans=-ans;
                }
            }
        }
        for(int i=1;i<=n;i++) ans=ans*a[i][i]%mod;
        printf("%d
    ",(ans+mod)%mod);
    }
    int main()
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
            {
                char c; cin>>c;
                if(c=='.') p[i][j]=++tot;
            }
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++) if(p[i][j])
            {
                for(int k=0;k<4;k++)
                {
                    int x=i+dx[k],y=j+dy[k];
                    int u=p[i][j],v=p[x][y];
                    if(x>0&&x<=n&&y>0&&y<=m&&p[x][y])
                    {
                        a[u][u]++; a[u][v]--;
                    }
                }
            }
        tot--;
    //    for(int i=1;i<=tot;i++)
    //        for(int j=1;j<=tot;j++) a[i][j]=(d[i][j]-c[i][j]+mod)%mod;
        gauss(tot);    
        return 0;
    }
    View Code
  • 相关阅读:
    SpringMVC+bootstrap-fileinput文件上传插件使用入门
    [Java]实现Comparable接口不严谨导致Comparison method violates its general contract!
    2021寒假ACM集训队第一次训练-搜索(一)
    第八届“图灵杯”NEUQ-ACM程序设计竞赛个人赛-热身赛
    2021蓝桥杯第三次训练赛
    2021年蓝桥杯第二次训练赛
    2021年蓝桥杯第一次训练赛
    HDU 1312 Red and Black
    HDU 1010 Tempter of the Bone
    HDU 3500 Fling
  • 原文地址:https://www.cnblogs.com/19992147orz/p/6349247.html
Copyright © 2011-2022 走看看