QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#74388 | #5442. Referee Without Red | chenshi | WA | 1ms | 40424kb | C++ | 3.6kb | 2023-02-01 00:09:15 | 2023-02-01 00:09:18 |
Judging History
answer
#include<cstdio>
#include<iostream>
using namespace std;
const int o=1e6+10,MOD=998244353;
int T,n,m,E,a[o],b[o],p1[o],l1[o],r1[o],cnt1,p2[o],l2[o],r2[o],cnt2,ans,str[o],z[o],len1[o],len2[o],tot[o*3];
int h[o],cnt,fac[o],inv[o],col[o*3],asd;bool pri[o],vis[o],in[o],sam[o],flg;
struct Edge{int v,p;}e[o*2];
inline void ad(int U,int V){e[++cnt].v=V;e[cnt].p=h[U];h[U]=cnt;}
inline int hsh(int x,int y){return (x-1ll)*m+y;}
inline void slv(int n,int*p,int*l,int*r,int&cnt){
cnt=0;
for(int i=1;i<=n;i+=2) p[++cnt]=i;
for(int i=2;i<=n;i+=2) p[++cnt]=i;
cnt=0;
for(int i=1;i<=n;++i) vis[i]=0;
for(int i=1,t=0;i<=n;++i) if(!vis[i]){
l[++cnt]=t+1;
for(int j=i;!vis[j];j=p[j]) vis[b[++t]=j]=1;
r[cnt]=t;
}
for(int i=1;i<=n;++i) p[b[i]]=i;
}
inline int exkmp(int len){
for(int i=2,l=0,r=0;i<=len;++i){
if(i<=r) z[i]=min(r-i+1,z[i-l+1]);
else z[i]=0;
for(;str[z[i]+1]==str[i+z[i]];++z[i]);
if(i+z[i]-1>r) l=i,r=i+z[i]-1;
}
for(int i=1;i<len;++i) if(len%i==0&&z[i+1]==len-i) return i;
return len;
}
void dfs(int nw){
if(col[nw]==asd) return;
if(vis[nw]||!in[nw]){flg=1;return;}
col[nw]=asd;
for(int i=h[nw];i;i=e[i].p) dfs(e[i].v);
}
inline void calc(int n){
for(int i=2;i<=n;++i) for(int j=i*2;j<=n;j+=i) vis[i]|=vis[j],pri[j]=0;
for(int i=2;i<=n;++i) if(pri[i]) for(long long j=i;j<=n&&vis[j];j*=i) ans=ans*1ll*i%MOD;
}
int main(){
for(scanf("%d",&T);T--;printf("%d\n",ans),cnt=0){
scanf("%d%d",&n,&m);ans=inv[1]=1;
slv(n,p1,l1,r1,cnt1);slv(m,p2,l2,r2,cnt2);
for(int i=fac[0]=1;i<=n*m;++i) fac[i]=fac[i-1]*1ll*i%MOD;
for(int i=2;i<=n*m;++i) inv[i]=MOD-MOD/i*1ll*inv[MOD%i]%MOD;
for(int i=1;i<=n;++i) for(int j=1;j<=m;++j) scanf("%d",&a[hsh(p1[i],p2[j])]);
for(int i=1;i<=E;++i) vis[i]=0,pri[i]=1;
for(int i=1,t,id=1;i<=cnt1;++i){
for(int j=1;j<=m;++j) vis[j]=0,pri[j]=1;
for(int j=1;j<=cnt2;++j,++id)
if(l1[i]==r1[i]){
len1[id]=1;sam[id]=0;
for(int k=l2[j];k<=r2[j];++k) str[k-l2[j]+1]=a[hsh(l1[i],k)];
len2[id]=t=exkmp(r2[j]-l2[j]+1);
if(t&1) vis[t]=1;
else vis[t/2]=1;
}
else if(l2[j]==r2[j]){
len2[id]=1;sam[id]=0;
for(int k=l1[i];k<=r1[i];++k) str[k-l1[i]+1]=a[hsh(k,l2[j])];
len1[id]=exkmp(r1[i]-l1[i]+1);
}
else{
ans=ans*1ll*fac[(len1[id]=r1[i]-l1[i]+1)*(len2[id]=r2[j]-l2[j]+1)]%MOD;sam[id]=0;++asd;
for(int x=l1[i],v;x<=r1[i];++x) for(int y=l2[j];y<=r2[j];ans=ans*1ll*inv[++tot[v]]%MOD,++y)
if(col[v=a[hsh(x,y)]]^asd) sam[id]=1,col[a[hsh(x,y)]]=asd,tot[v]=0;
if(!sam[id]) ans=ans*(MOD+1ll)/2%MOD;
}
calc(m);
}
for(int i=1,t;i<=cnt2;++i) if(l2[i]==r2[i]){
for(int j=1;j<=n;++j) vis[j]=0,pri[j]=1;
for(int j=1;j<=cnt1;++j){
for(int k=l1[j];k<=r1[j];++k) str[k-l1[j]+1]=a[hsh(k,l2[i])];
if((t=exkmp(r1[j]-l1[j]+1))&1) vis[t]=1;
else vis[t/2]=1;
}
calc(n);
}
n=cnt1;m=cnt2;
for(int i=1;i<=n+m;++i) h[i]=vis[i]=in[i]=0;
for(int i=1;i<=n;++i) for(int j=1;j<=m;++j) if(!sam[hsh(i,j)])
if(len1[hsh(i,j)]%2==0&&len2[hsh(i,j)]%2==0) ad(i,j+n),ad(j+n,i);
else if(len1[hsh(i,j)]%2==0) vis[j+n]=1;
else if(len2[hsh(i,j)]%2==0) vis[i]=1;
if(n<m){
for(int i=1;i<=m;++i) if(h[i+n]||vis[i+n]) in[n+i]=1,ans=ans*2%MOD;
for(int i=1;i<=n;++i) if(h[i]||vis[i]){
flg=0;++asd;in[i]=1;dfs(i);
if(flg) ans=ans*2%MOD;
else in[i]=0;
}
}
else{
for(int i=1;i<=n;++i) if(h[i]||vis[i]) in[i]=1,ans=ans*2%MOD;
for(int i=1;i<=m;++i) if(h[i+n]||vis[i+n]){
flg=0;++asd;in[i+n]=1;dfs(i);
if(flg) ans=ans*2%MOD;
else in[i+n]=0;
}
}
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 40416kb
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: 1ms
memory: 40424kb
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:
765756418
result:
wrong answer 1st numbers differ - expected: '690561281', found: '765756418'