BZOJ4031——HEOI小z的房间
2015-07-13 11:49
465 查看
题意:求某网格图生成树个数,对1e9取模
题解:题目是裸的Matrix-Tree定理,这不是我要说的重点,重点是对于这个取模的处理。
因为这不是个质数,所以不能直接乘逆元来当除法用,直接高斯消元肯定是不行的,需要一定实现的小技巧。
我们可以考虑gcd的实现过程,辗转相除直到一个为0。多么好的思路,对于这个问题我们也可以这样处理,每次减掉相应的倍数即可
下面是代码
题解:题目是裸的Matrix-Tree定理,这不是我要说的重点,重点是对于这个取模的处理。
因为这不是个质数,所以不能直接乘逆元来当除法用,直接高斯消元肯定是不行的,需要一定实现的小技巧。
我们可以考虑gcd的实现过程,辗转相除直到一个为0。多么好的思路,对于这个问题我们也可以这样处理,每次减掉相应的倍数即可
下面是代码
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef double db; const int inf=0x3f3f3f3f; int getint() { int f=1,g=0;char c=getchar(); while(c<'0' || c>'9'){if(c=='-')f=-1;c=getchar();} while(c>='0' && c<='9')g=(g<<3)+(g<<1)+c-'0',c=getchar(); return f*g; } const int maxn=105; const int maxl=10; const int mod=1000000000; const int dx[]={0,0,-1,1}; const int dy[]={1,-1,0,0}; char c[maxl][maxl]; ll a[maxn][maxn]; int pos[maxn][maxn]; int tot; ll det(int n) { for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { a[i][j]=(a[i][j]+mod)%mod; } } ll f=1,res=1ll; for(int i=1;i<=n;i++) { for(int j=i+1;j<=n;j++) { int a1=a[i][i]; int b1=a[j][i]; while(b1!=0) { ll temp=a1/b1; a1%=b1;swap(a1,b1); for(int k=i;k<=n;k++) { a[i][k]=(a[i][k]-temp*a[j][k]%mod+mod)%mod; } for(int k=i;k<=n;k++) { swap(a[i][k],a[j][k]); } f=-f; } } if(!a[i][i])return 0; res=res*a[i][i]%mod; } res*=f; res=(res+mod)%mod; return res; } int main() { // freopen("in.txt","r",stdin); int n=getint(); int m=getint(); for(int i=1;i<=n;i++) { scanf("%s",c[i]+1); for(int j=1;j<=m;j++) { if(c[i][j]!='*')pos[i][j]=++tot; } } for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { if(!pos[i][j])continue; for(int k=0;k<4;k++) { int tx=i+dx[k]; int ty=j+dy[k]; if(ty<1 || tx<1 || tx>n || ty>m || !pos[tx][ty])continue; a[pos[i][j]][pos[i][j]]++; a[pos[i][j]][pos[tx][ty]]--; } } } printf("%d\n",det(tot-1)); return 0; }
相关文章推荐
- Matlab 高斯消元
- 高斯消元
- [BZOJ1013][JSOI2008][高斯消元]球形空间产生器sphere
- poj 1222 高斯消元
- poj 1681 Painter's Problem 高斯消元
- poj 1830 开关问题 高斯消元
- HDU 4870 Rating 多校联合练习赛 高斯消元
- POJ 1830 开关问题 (高斯消元)
- POJ 1753 Flip Game (高斯消元)
- POJ 3185 The Water Bowls(高斯消元)
- POJ 1681 Painter's Problem (高斯消元)
- SGU 275 To xor or not to xor (高斯消元)
- SGU 200 Cracking RSA (高斯消元+大数高精度)
- HDU 3976 Electric resistance (高斯消元)
- poj 1487 Single-Player Games 高斯消元
- poj 1222 EXTENDED LIGHTS OUT 高斯消元
- poj 1753 Flip Game 高斯消元
- poj 1681 Painter's Problem 高斯消元
- poj 1830 开关问题 高斯消元
- poj 3185 The Water Bowls 高斯消元