标签:for printf char s ace 生成 can set inline c++
#include<bits/stdc++.h>
using namespace std;
const int N=310;
int n,m,p,fa[N*N];
char s[N][N];
inline int id(int x,int y){return (x-1)*(m+1)+y;}
inline int find(int x){return fa[x]==x?x:fa[x]=fa[fa[x]]=fa[fa[fa[x]]]=find(fa[fa[fa[x]]]);}
inline void merge(int x,int y){x=find(x);y=find(y);x!=y?fa[x]=y:0;}
int a[2][N<<2][N<<2],tot[2],bel[N*N];
inline int add(int p,int x,int y){
++a[p][bel[x]][bel[x]];++a[p][bel[y]][bel[y]];
--a[p][bel[x]][bel[y]];--a[p][bel[y]][bel[x]];
}
inline int inv(int a,int b=p-2){
int ret=1;
while(b){
if(b&1) ret=1ll*ret*a%p;
a=1ll*a*a%p;b>>=1;
}
return ret;
}
inline int matrix_tree(int pt,int x){
int ans=1;
for(int j=1;j<=x;++j){
for(int i=j+1;i<=x;++i){
if(a[pt][i][j]){
int d=1ll*a[pt][j][j]*inv(a[pt][i][j])%p;
for(int k=j;k<=x;++k) a[pt][j][k]=(a[pt][j][k]-1ll*d*a[pt][i][k]%p+p)%p;
swap(a[pt][i],a[pt][j]);ans=-ans;
}
}
ans=1ll*ans*a[pt][j][j]%p;
if(ans<0) ans+=p;
}
return ans;
}
int main(){
scanf("%d%d%d",&n,&m,&p);
for(int i=1;i<=(n+1)*(m+1);++i) fa[i]=i;
for(int i=1;i<=n;++i){
scanf("%s",s[i]+1);
for(int j=1;j<=m;++j){
if(s[i][j]==‘/‘) merge(id(i+1,j),id(i,j+1));
else if(s[i][j]!=‘。‘) merge(id(i,j),id(i+1,j+1));
}
}
for(int i=1;i<=n+1;++i)
for(int j=1;j<=m+1;++j)
if(fa[id(i,j)]==id(i,j)){
int wh=(i+j)&1;
bel[id(i,j)]=++tot[wh];
}
for(int i=1;i<=n;++i){
for(int j=1;j<=m;++j){
if(s[i][j]==‘*‘){
add((i+1+j)&1,find(id(i+1,j)),find(id(i,j+1)));
add((i+j)&1,find(id(i,j)),find(id(i+1,j+1)));
}
}
}
printf("%d\n",(matrix_tree(0,tot[0]-1)+matrix_tree(1,tot[1]-1))%p);
return 0;
}
标签:for printf char s ace 生成 can set inline c++
原文地址:https://www.cnblogs.com/tqxboomzero/p/13860284.html