QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#738223 | #5442. Referee Without Red | yhddd | RE | 28ms | 115284kb | C++14 | 4.0kb | 2024-11-12 18:18:25 | 2024-11-12 18:18:32 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
#define mod 998244353ll
#define pii pair<int,int>
#define fi first
#define se second
#define mems(x,y) memset(x,y,sizeof(x))
#define pb push_back
#define db double
using namespace std;
const int maxn=1000010;
const int inf=1e18;
inline int read(){
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch-48);ch=getchar();}
return x*f;
}
bool Mbe;
int n,m,ans;
vector<int> a[maxn];
int p[maxn],q[maxn];
int id1[maxn],id2[maxn],idx1,idx2;
vector<int> pos1[maxn],pos2[maxn];
bool vis[maxn];
int t[maxn];
inline int ksm(int a,int b=mod-2){
int ans=1;
while(b){
if(b&1)ans=ans*a%mod;
a=a*a%mod;
b>>=1;
}
return ans;
}
int fac[maxn],inv[maxn],pw[maxn],inv2=ksm(2);
int f[maxn];
int fd(int x){
if(f[x]==x)return x;
return f[x]=fd(f[x]);
}
bool merge(int u,int v){
u=fd(u),v=fd(v);
if(u==v)return 0;
f[u]=v;return 1;
}
int nxt[maxn];
int num[maxn];
bool isp[maxn];
int pre[maxn],cnt;
void sovle(int n){
for(int i=2;i<=n;i++){
if(!isp[i])pre[++cnt]=i;
for(int j=1;j<=cnt&&i*pre[j]<=n;j++){
isp[i*pre[j]]=1;
if(i%pre[j]==0)break;
}
}
}
void add(int v){
for(int i=1;i<=cnt&&pre[i]<=v;i++)if(v%pre[i]==0){
int mul=1;
while(v%pre[i]==0)++cnt,mul*=pre[i],v/=pre[i];
num[i]=max(num[i],mul);
}
}
void work(){
n=read();m=read();ans=1;
for(int i=1;i<=n;i++)p[i]=(i&1)?(i+1)/2:(n+1)/2+i/2;
for(int i=1;i<=m;i++)q[i]=(i&1)?(i+1)/2:(m+1)/2+i/2;
for(int i=0;i<=n;i++)a[i].resize(m+1);
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++)a[i][j]=read();
}
idx1=idx2=0;
for(int i=1;i<=n;i++)if(!vis[i]){
int x=i;++idx1;pos1[idx1].clear();
while(!vis[x]){
vis[x]=1,id1[x]=idx1;
pos1[idx1].pb(x);
x=p[x];
}
}
for(int i=1;i<=n;i++)vis[i]=0;
for(int i=1;i<=m;i++)if(!vis[i]){
int x=i;++idx2;pos2[idx2].clear();
while(!vis[x]){
vis[x]=1,id2[x]=idx2;
pos2[idx2].pb(x);
x=q[x];
}
}
for(int i=1;i<=m;i++)vis[i]=0;
for(int i=1;i<=n;i++)if(p[i]==i){
for(int j=1;j<=cnt&&pre[j]<=m;j++)num[j]=1;
for(int j=1;j<=idx2;j++){
int sz=pos2[j].size();
vector<int> vv;vv.pb(0);
for(int k:pos2[j])vv.pb(a[i][k]);
for(int k=2,j=0;k<=sz;k++){
while(j&&vv[j+1]!=vv[k])j=nxt[j];
if(vv[j+1]==vv[k])j++;
nxt[k]=j;
}
if(sz%(sz-nxt[sz]))add(sz);
else add(sz-nxt[sz]);
}
for(int j=1;j<=cnt&&pre[j]<=m;j++)ans=ans*num[j]%mod;
}
for(int i=1;i<=m;i++)if(q[i]==i){
for(int j=1;j<=cnt&&pre[j]<=n;j++)num[j]=1;
for(int j=1;j<=idx1;j++){
int sz=pos1[j].size();
vector<int> vv;vv.pb(0);
for(int k:pos1[j])vv.pb(a[k][i]);
for(int k=2,j=0;k<=sz;k++){
while(j&&vv[j+1]!=vv[k])j=nxt[j];
if(vv[j+1]==vv[k])j++;
nxt[k]=j;
}
if(sz%(sz-nxt[sz]))add(sz);
else add(sz-nxt[sz]);
}
for(int j=1;j<=cnt&&pre[j]<=n;j++)ans=ans*num[j]%mod;
}
// cout<<ans<<"\n";
for(int i=1;i<=n+m+1;i++)f[i]=i;
for(int i=1;i<=idx1;i++)if(pos1[i].size()>1){
for(int j=1;j<=idx2;j++)if(pos2[j].size()>1){
bool fl=0;
for(int k:pos1[i]){
for(int l:pos2[j])fl|=t[a[k][l]],t[a[k][l]]++;
}
int l1=pos1[i].size(),l2=pos2[j].size();
ans=ans*fac[l1*l2]%mod;
for(int k:pos1[i]){
for(int l:pos2[j])if(t[a[k][l]]){
ans=ans*inv[t[a[k][l]]]%mod;
t[a[k][l]]=0;
}
}
// cout<<i<<" "<<j<<" "<<l1<<" "<<l2<<" "<<ans<<"\n";
if(!fl){
if(!merge((l1&1)?n+m+1:n+j,(l2&1)?n+m+1:i))ans=ans*inv2%mod;
}
}
}
printf("%lld\n",ans);
}
// \
444
bool Med;
int T;
signed main(){
// freopen("matrix.in","r",stdin);
// freopen("matrix.out","w",stdout);
// ios::sync_with_stdio(0);
// cin.tie(0);cout.tie(0);
// cerr<<(&Mbe-&Med)/1048576.0<<" MB\n";
pw[0]=1;for(int i=1;i<maxn;i++)pw[i]=pw[i-1]*2%mod;
fac[0]=1;for(int i=1;i<maxn;i++)fac[i]=fac[i-1]*i%mod;
inv[maxn-1]=ksm(fac[maxn-1]);for(int i=maxn-2;~i;i--)inv[i]=inv[i+1]*(i+1)%mod;
sovle(maxn-10);
T=read();
while(T--)work();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 24ms
memory: 115284kb
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: 109032kb
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
Runtime Error
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 194740086 215040 40320 322560 32456681 1166400 887214222 542386470 375765881 9 4 361590980 913481201 527607149 85428015 311271219 16 645120 557106771 388800 173057174 232068778 460990604 1 239327783 9 145293043 40098691 97370043 799392782 155415144 1 36 3991680 645120 545661527 55...