QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#333370#5442. Referee Without Redgrass8cowWA 30ms160620kbC++172.3kb2024-02-19 21:07:292024-02-19 21:07:30

Judging History

你现在查看的是最新测评结果

  • [2024-02-19 21:07:30]
  • 评测
  • 测评结果:WA
  • 用时:30ms
  • 内存:160620kb
  • [2024-02-19 21:07:29]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int mod=998244353,inv2=(mod+1)/2;
int qpow(int a,int b){
	int c=1;
	for(;b;b>>=1){
		if(b&1)c=1ll*a*c%mod;
		a=1ll*a*a%mod;
	}
	return c;
}
int n,m,inv[3010000],a[3010000];
int id(int x,int y){return (x-1)*m+y; }
int p[3001000],A,B;bool vi[3010000];
vector<int>gx[3010000],gy[3010000];
int fa[3010000],tot[3010000];
#define pb push_back
int gcd(int a,int b){if(!b)return a;return gcd(b,a%b);}
int lcm(int a,int b){return a/gcd(a,b)*b;}
int kmp[3010000];
int F(int x){if(x==fa[x])return x;return fa[x]=F(fa[x]);}
int calc(vector<int>c){
	int N=c.size();
	for(int i=2,j=0;i<=N;i++){
		while(j&&c[j]!=c[i-1])j=kmp[j];
		if(c[j]==c[i-1])j++;kmp[i]=j;
	}
	if(N%(N-kmp[N]))return N;
	return N-kmp[N];
}
void sol(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)scanf("%d",&a[id(i,j)]);
	for(int i=1;i<=n;i++)p[i]=(i&1)?((i+1)/2):((n+1)/2+i/2),vi[i]=0;
	A=B=0;
	for(int i=1;i<=n;i++)if(!vi[i]){
		A++,gx[A].clear();
		int u=i;while(!vi[u])gx[A].pb(u),vi[u]=1,u=p[u];
	}
	for(int i=1;i<=m;i++)p[i]=(i&1)?((i+1)/2):((m+1)/2+i/2),vi[i]=0;
	for(int i=1;i<=m;i++)if(!vi[i]){
		B++;gy[B].clear();
		int u=i;while(!vi[u])gy[B].pb(u),vi[u]=1,u=p[u];
	}
	int ans=1;
	for(int i=1;i<=A;i++)if(gx[i].size()==1){
		int ed=1;
		for(int j=1;j<=B;j++){
			vector<int>sb;
			for(int z:gy[j])sb.pb(a[id(gx[i][0],z)]);
			ed=lcm(ed,calc(sb));
		}
		ans=1ll*ans*ed%mod;
	}
	for(int i=1;i<=B;i++)if(gy[i].size()==1){
		int ed=1;
		for(int j=1;j<=A;j++){
			vector<int>sb;
			for(int z:gx[j])sb.pb(a[id(z,gy[i][0])]);
			ed=lcm(ed,calc(sb));
		}
		ans=1ll*ans*ed%mod;
	}
	for(int i=1;i<=A+B+1;i++)fa[i]=i;
	for(int i=1;i<=A;i++)if(gx[i].size()!=1)for(int j=1;j<=B;j++)if(gy[j].size()!=1){
		int ed=0;
		bool fl=0;
		for(int x:gx[i])for(int y:gy[j]){
			int o=a[id(x,y)];
			ed++,tot[o]++;
			if(tot[o]>1)fl=1;
			ans=1ll*ans*ed*inv[tot[o]]%mod;
		}
		for(int x:gx[i])for(int y:gy[j])tot[a[id(x,y)]]=0;
		if(fl)continue;ans=1ll*ans*inv2%mod;
		int u=F((gy[j].size()&1)?(A+B+1):i),v=((gx[i].size()&1)?(A+B+1):(A+j));
		u=F(u),v=F(v);if(u!=v)fa[u]=v,ans=2ll*ans%mod;
	}
	printf("%d\n",ans);
}
const int N=3e6;
int main(){
	inv[1]=1;
	for(int i=2;i<=N;i++)inv[i]=mod-1ll*(mod/i)*inv[mod%i]%mod;
	int T;scanf("%d",&T);while(T--)sol();
	return 0;
} 

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 30ms
memory: 159532kb

input:

2
4 4
1 2 3 4
3 4 1 2
1 2 4 1
4 3 3 2
3 9
1 8 1 1 8 1 1 8 1
1 8 8 8 8 8 8 8 1
1 1 1 8 8 8 1 1 1

output:

96
6336

result:

ok 2 number(s): "96 6336"

Test #2:

score: -100
Wrong Answer
time: 26ms
memory: 160620kb

input:

1
18 16
8 8 1 1 8 8 8 1 8 8 8 1 8 8 8 1
8 1 8 1 8 1 1 1 8 1 1 1 8 1 1 1
8 8 8 1 8 8 8 1 8 8 8 1 8 8 8 1
8 8 1 1 8 1 1 1 8 1 1 1 8 1 1 1
8 1 8 1 8 8 8 1 8 1 1 1 8 8 8 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
8 8 1 1 8 8 8 1 8 8 8 1 7 7 7 1
8 1 8 1 8 1 1 1 8 1 1 1 1 1 7 1
8 8 8 1 8 8 8 1 8 8 8 1 1 7 7 1
8 8 ...

output:

-958925691

result:

wrong answer 1st numbers differ - expected: '690561281', found: '-958925691'