QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#373252 | #5442. Referee Without Red | kgqy | WA | 143ms | 99512kb | C++14 | 5.1kb | 2024-04-01 12:59:41 | 2024-04-01 12:59:41 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int mod=998244353;
inline int read(){
int w=0;char ch=getchar();
while(!isdigit(ch)) ch=getchar();
while(isdigit(ch)) w=w*10+ch-'0',ch=getchar();
return w;
}
inline int qpow(int x,int b){
int v=1;
while(b){
if(b&1) v=v*x%mod;
b>>=1;
x=x*x%mod;
}
return v;
}
int t,n=3e6,m;
#define tid(x,y) (((x)-1)*t2+(y))
#define id(x,y) (((x)-1)*m+(y))
#define ix(x) (((x)+m-1)/m)
#define iy(y) (((y)-1)%m+1)
int a[3000005],b[3000005],mp[3000005];
int p[1000005],q[1000005];
int vis[1000005];
int s1[1000005],t1,s2[1000005],t2;
int tsz1[1000005],tsz2[1000005];
int s[3000005],tot;
int depi[3000005];
int de[3000005];
int pi[3000005];
int solve(){
for(int i=2;i<=tot;i++) de[i-1]=s[i];
for(int i=1;i<=tot;i++) de[i+tot-1]=s[i];
// puts("OWOW");
// for(int i=1;i<tot*2;i++) printf("%d ",de[i]);puts("");
for(int i=1;i<tot;i++) swap(de[i],de[2*tot-i]);
// for(int i=1;i<tot*2;i++) printf("%d ",de[i]);puts("");
pi[1]=0;
for(int i=2;i<=tot*2-1;i++){
int j=pi[i-1];
while(j&&de[i]!=de[j+1]) j=pi[j];
if(de[i]==de[j+1]) j++;
pi[i]=j;
}
// for(int i=1;i<tot*2;i++) printf("%d ",pi[i]);puts("");
for(int i=tot+1;i<=tot*2-1;i++){
if(pi[i]>=tot) return i-tot;
}
return tot;
}
int fa[3000005];
inline int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
inline void merge(int u,int v){
fa[find(u)]=find(v);
}
int fac[3000005],inv[3000005];
void init(){
fac[0]=1;
depi[0]=1;depi[1]=(mod+1)/2;
for(int i=1;i<=n;i++) fac[i]=fac[i-1]*i%mod,depi[i]=depi[i-1]*depi[1]%mod;
inv[n]=qpow(fac[n],mod-2);
for(int i=n;i;i--) inv[i-1]=inv[i]*i%mod;
}
int findsame(){
for(int i=2;i<=tot;i++) if(s[i]==s[i-1]) return 1;
return 0;
}
int solve2(int fd){
if(!fd) return fac[tot]*depi[1]%mod;
int ans=fac[tot];
// puts("OWO");
// for(int i=1;i<=tot;i++) printf("%d ",s[i]);puts("");
for(int i=1,cnt=0;i<=tot;i++){
cnt++;
if(i==tot||s[i]!=s[i+1]){
ans=ans*inv[cnt]%mod;
cnt=0;
}
}
return ans;
}
main(){
t=read();
init();
while(t--){
n=read(),m=read();
int ans=1;
for(int i=1;i<=n*m;i++) a[i]=read(),tsz1[i]=tsz2[i]=0;
for(int i=1,nw=1;i<=n;i++,nw=(nw+2<=n?nw+2:2)) p[i]=nw;
for(int i=1,nw=1;i<=m;i++,nw=(nw+2<=m?nw+2:2)) q[i]=nw;
t1=t2=0;
for(int i=1;i<=n;i++){
if(vis[i]) continue;
int sz=1;
s1[++t1]=i;vis[i]=1;
for(int j=p[i];j!=i;j=p[j]){
s1[++t1]=j;sz++;
vis[j]=1;
}
tsz1[t1]=sz;
}
for(int i=1;i<=n;i++) vis[i]=0;
for(int i=1;i<=m;i++){
if(vis[i]) continue;
int sz=1;
// printf("wao\n%d ",i);
s2[++t2]=i;vis[i]=1;
for(int j=q[i];j!=i;j=q[j]){
s2[++t2]=j;sz++;
vis[j]=1;
// printf("%d ",j);
}
// puts("");
tsz2[t2]=sz;
}
// for(int i=1;i<=m;i++) printf("%d " ,s2[i]);puts("");
for(int i=1;i<=m;i++) vis[i]=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
b[id(i,j)]=a[id(s1[i],s2[j])];
}
}
// for(int i=1;i<=n;i++,puts("")) for(int j=1;j<=m;j++) printf("%d ",b[id(i,j)]);
t1=t2=0;
for(int i=1;i<=n;i++) if(tsz1[i]>1) t1++,s1[t1]=tsz1[i];
for(int i=1;i<=m;i++) if(tsz2[i]>1) t2++,s2[t2]=tsz2[i];
for(int i=1;i<=n;i++) if(tsz1[i]==1){
int pii=1;
for(int j=1;j<=m;j++){
if(tsz2[j]){
tot=0;
for(int k=j-tsz2[j]+1;k<=j;k++) s[++tot]=b[id(i,k)];
int nw=solve();
// printf("%d %d %d\n",i,j,nw);
pii=pii*nw/__gcd(pii,nw);
}
}
// printf("qwq %d\n",pii);
ans=ans*pii%mod;
}
// printf("%lld\n",ans);
for(int i=1;i<=m;i++) if(tsz2[i]==1){
int pii=1;
for(int j=1;j<=n;j++){
if(tsz1[j]){
tot=0;
for(int k=j-tsz1[j]+1;k<=j;k++) s[++tot]=b[id(k,i)];
int nw=solve();
// printf("%d %d %d\n",i,j,nw);
pii=pii*nw/__gcd(pii,nw);
}
}
// printf("qwq %d\n",pii);
ans=ans*pii%mod;
}
// printf("%lld\n",ans);
for(int i=1,id1=0;i<=n;i++){
if(tsz1[i]<2) continue;
id1++;
for(int k=1,id2=0;k<=m;k++){
if(tsz2[k]<2) continue;
id2++;
tot=0;
for(int j=i-tsz1[i]+1;j<=i;j++){
for(int l=k-tsz2[k]+1;l<=k;l++){
s[++tot]=b[id(j,l)];
}
}
sort(s+1,s+1+tot);
int fd=findsame();
ans=ans*solve2(fd)%mod;
if(fd) mp[tid(id1,id2)]=1;
// printf("%d\n",ans);
}
}
// printf("%d\n",ans);
for(int i=1;i<=t1;i++){
if(!(s1[i]&1)) continue;
int sum=0;
for(int j=1;j<=t2;j++) if(!(s2[j]&1)&&!mp[tid(i,j)]) sum++;
if(sum) ans=ans*2%mod;
}
for(int j=1;j<=t2;j++){
if(!(s2[j]&1)) continue;
int sum=0;
for(int i=1;i<=t1;i++) if(!(s1[i]&1)&&!mp[tid(i,j)]) sum++;
if(sum) ans=ans*2%mod;
}
for(int i=1;i<=t1+t2;i++) fa[i]=i;
for(int i=1;i<=t1;i++){
if(s1[i]&1) continue;
for(int j=1;j<=t2;j++){
if(s2[j]&1) continue;
if(mp[tid(i,j)]) continue;
merge(i,j+t1);
}
}
int acnt=t1+t2;
for(int i=1;i<=t1+t2;i++){
if(fa[i]==i) acnt--;
}
ans=ans*qpow(2,acnt);
printf("%lld\n",ans);
}
}
/*
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
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 19ms
memory: 98172kb
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: 0
Accepted
time: 28ms
memory: 99512kb
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:
690561281
result:
ok 1 number(s): "690561281"
Test #3:
score: -100
Wrong Answer
time: 143ms
memory: 99452kb
input:
71117 7 8 2868391 1228870 2892937 349733 664891 1675356 1981526 762573 2892937 2892937 664891 1228870 959280 762573 664891 959280 349733 250147 1675356 349733 349733 762573 1675356 250147 1675356 959280 664891 250147 250147 250147 2868391 959280 1675356 664891 250147 1228870 1981526 250147 2868391 2...
output:
462363428 38853786 97370043 215040 40320 322560 515350517 1166400 887214222 271193235 375765881 9 4 180795490 913481201 527607149 85428015 311271219 16 645120 557106771 388800 86528587 232068778 460990604 1 309393034 9 285884349 259585761 547807198 799392782 77707572 1 36 3991680 322560 385976470 63...
result:
wrong answer 3rd numbers differ - expected: '194740086', found: '97370043'