其他分享
首页 > 其他分享> > JZOJ6930. 【2020.12.26冬令营模拟】quarantine

JZOJ6930. 【2020.12.26冬令营模拟】quarantine

作者:互联网

Description

Solution

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<ctime>
#define maxn 8205
#define maxm 25
#define ll long long
#define mo 9999991
using namespace std;

int n,m,q,i,j,k,a[maxn][maxm][maxm],d1[maxm],d2[maxm];

int rd(){return (rand()*rand()+rand())%(mo-1)+1;}

struct matrix{ll a[maxm][maxm];} t[maxn*4],S;
matrix operator*(matrix A,matrix B){
	matrix C; memset(C.a,0,sizeof(C.a));
	for(int i=0;i<m;i++) for(int j=0;j<m;j++) for(int k=0;k<m;k++)
		C.a[i][k]+=A.a[i][j]*B.a[j][k];
	for(int i=0;i<m;i++) for(int j=0;j<m;j++) C.a[i][j]=C.a[i][j]%mo;
	return C;
}

void maketree(int x,int l,int r){
	if (l==r){
		for(int i=0;i<m;i++) for(int j=0;j<m;j++) if (a[l][i][j])
			t[x].a[i][j]=rd(); else t[x].a[i][j]=0;
		return;
	}
	int mid=(l+r)>>1;
	maketree(x<<1,l,mid),maketree(x<<1^1,mid+1,r);
	t[x]=t[x<<1]*t[x<<1^1];
}

void change(int x,int l,int r,int p){
	if (l==r){
		for(int i=0;i<m;i++) for(int j=0;j<m;j++) if (a[l][i][j])
			t[x].a[i][j]=rd(); else t[x].a[i][j]=0;
		return;
	}
	int mid=(l+r)>>1;
	if (p<=mid) change(x<<1,l,mid,p); else change(x<<1^1,mid+1,r,p);
	t[x]=t[x<<1]*t[x<<1^1];
}

void find(int x,int l,int r,int L,int R){
	if (l>R||r<L) return;
	if (L<=l&&r<=R) {
		S=S*t[x];
		return;
	}
	int mid=(l+r)>>1;
	find(x<<1,l,mid,L,R),find(x<<1^1,mid+1,r,L,R);
}

ll A[maxm][maxm];
ll ksm(ll x,ll y){
	ll s=1;
	for(;y;y/=2,x=x*x%mo) if (y&1)
		s=s*x%mo;
	return s;
}
int getrk(int n,int m){
	static ll tmp[maxm][maxm];
	if (n>m){
		for(i=0;i<n;i++) for(j=0;j<m;j++)
			tmp[j][i]=A[i][j];
		memcpy(A,tmp,sizeof(A));
		swap(n,m);
	}
	int c=0;
	for(i=0;i<m&&c<n;i++){
		for(j=c;j<n;j++) if (A[j][i]) break;
		if (j==n) continue;
		for(k=0;k<m;k++) swap(A[c][k],A[j][k]);
		ll inv=ksm(A[c][i],mo-2);
		for(j=0;j<m;j++) A[c][j]=A[c][j]*inv%mo;
		for(j=0;j<n;j++) if (c!=j) for(k=m-1;k>=i;k--)
			(A[j][k]-=A[j][i]*A[c][k])%=mo;
		c++;
	}
	return c;
}

int main(){
	freopen("quarantine.in","r",stdin);
	freopen("quarantine.out","w",stdout);
//	freopen("ceshi.out","w",stdout);
	srand(time(0));
	scanf("%d%d%d",&n,&m,&q); char ch=getchar();
	for(i=1;i<n;i++) for(j=0;j<m;j++) {
		while (ch!='0'&&ch!='1') ch=getchar();
		for(k=0;k<m;k++) a[i][j][k]=ch-'0',ch=getchar();
	}
	maketree(1,1,n-1);
	while (q--){
		ch=getchar();while (ch!='M'&&ch!='T') ch=getchar();
		if (ch=='T'){
			scanf("%d%d%d",&i,&j,&k),j--,k--;
			a[i][j][k]^=1,change(1,1,n-1,i);
		} else {
			int l,r; scanf("%d%d",&l,&r);
			memset(S.a,0,sizeof(S.a));
			for(i=0;i<m;i++) S.a[i][i]=1;
			find(1,1,n-1,l,r-1);
			ch=getchar(); while (ch!='0'&&ch!='1') ch=getchar();
			d1[0]=d2[0]=0;
			for(i=0;i<m;i++) {
				if (ch-'0') d1[++d1[0]]=i;
				ch=getchar();
			}
			ch=getchar();
			for(i=0;i<m;i++) {
				if (ch-'0') d2[++d2[0]]=i;
				ch=getchar();
			} 
			memset(A,0,sizeof(A));
			for(i=1;i<=d1[0];i++) for(j=1;j<=d2[0];j++)
				A[i-1][j-1]=S.a[d1[i]][d2[j]];
			printf("%d\n",getrk(d1[0],d2[0]));
		}
	}
}

标签:26,ch,JZOJ6930,冬令营,int,mo,d%,maxm,getchar
来源: https://blog.csdn.net/qq_43649416/article/details/111937918