QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#648961 | #5442. Referee Without Red | zwh2008 | WA | 268ms | 137896kb | C++14 | 2.7kb | 2024-10-17 21:05:09 | 2024-10-17 21:05:09 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using pi=pair<int,int>;
#define fi first
#define se second
const int N=1e6+5,mod=998244353,i2=(mod+1)/2;
int n,m,n0,m0,p[N],q[N],nxt[N],ct[N*3],fa[N*2];
bool vis[N];
ll inv[N],fc[N];
vector<vector<int>>a;
vector<int>cx[N],cy[N];
ll qp(ll a,int b){ll ans=1;for(;b;b>>=1,a=a*a%mod)if(b&1)ans=ans*a%mod;return ans;}
int gf(int x){return x==fa[x]?x:fa[x]=gf(fa[x]);}
ll lcm(ll x,ll y){return x/__gcd(x,y)*y;}
void init(int n) {
fc[0]=1;for(int i=1;i<=n;i++)fc[i]=fc[i-1]*i%mod;
inv[n]=qp(fc[n],mod-2);
for(int i=n-1;~i;i--)inv[i]=inv[i+1]*(i+1)%mod;
}
int calc(vector<int>&a) {
int n=a.size();nxt[1]=0;
for(int i=2;i<=n;i++) {
int j=nxt[i-1];
while(j&&a[j]!=a[i-1])j=nxt[j];
nxt[i]=j+(a[i-1]==a[j]);
}return n%(n-nxt[n])?n:n-nxt[n];
}
void solve() {
cin>>n>>m,n0=m0=0;
a=vector<vector<int>>(n+1,vector<int>(m+1));
for(int i=1;i<=n;i++)p[i]=(i&1?i/2+1:(n+1)/2+i/2);
for(int i=1;i<=m;i++)q[i]=(i&1?i/2+1:(m+1)/2+i/2);
for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)cin>>a[i][j];
fill(vis,vis+n+1,0);
for(int i=1;i<=n;i++)if(!vis[i]) {
int x=i;cx[++n0].clear();
while(!vis[x])cx[n0].push_back(x),vis[x]=1,x=p[x];
}
fill(vis,vis+m+1,0);
for(int i=1;i<=m;i++)if(!vis[i]) {
int x=i;cy[++m0].clear();
while(!vis[x])cy[m0].push_back(x),vis[x]=1,x=q[x];
}
ll ans=1;
for(int i=1;i<=n;i++)if(p[i]==i) {
ll s=1;
for(int j=1;j<=m0;j++) {
vector<int>v;
for(int k:cy[j])v.push_back(a[i][k]);
s=lcm(s,calc(v));
}ans=s%mod*ans%mod;
}
for(int i=1;i<=m;i++)if(q[i]==i) {
ll s=1;
for(int j=1;j<=n0;j++) {
vector<int>v;
for(int k:cx[j])v.push_back(a[k][i]);
s=lcm(s,calc(v));
}ans=s%mod*ans%mod;
}
iota(fa+1,fa+n0+m0+2,1);
for(int i=1;i<=n0;i++)if(cx[i].size()!=1)for(int j=1;j<=m0;j++)if(cy[j].size()!=1) {
bool fl=0;int p=cx[i].size(),q=cy[j].size();
for(int x:cx[i])for(int y:cy[j])fl|=ct[a[x][y]],ct[a[x][y]]++;
ans=ans*fc[p*q]%mod;
for(int x:cx[i])for(int y:cy[j])ans=ans*inv[ct[a[x][y]]]%mod,ct[a[x][y]]=0;
if(!fl) {
int x=(cy[j].size()&1?n0+m0+1:i),y=(cx[i].size()&1?n0+m0+1:n0+j);
x=gf(x),y=gf(y);if(x!=y)fa[x]=y;else ans=ans*i2%mod;
}
}cout<<ans<<'\n';
}
int main() {
#ifndef ONLINE_JUDGE
freopen(".in","r",stdin);
freopen(".out","w",stdout);
#endif
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int tt;cin>>tt,init(1e6);
while(tt--)solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 16ms
memory: 75028kb
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: 20ms
memory: 73824kb
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: 0
Accepted
time: 268ms
memory: 84160kb
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...
result:
ok 71117 numbers
Test #4:
score: 0
Accepted
time: 213ms
memory: 85420kb
input:
755 63 63 2320566 2175957 1897863 2154093 2551257 1353847 334243 2310922 489697 2025148 2898381 1301433 2025918 2624822 916273 446350 489697 1834347 1618696 1170954 1947467 565649 1188980 818151 2167497 604100 933579 1526487 727797 1757541 6096 437958 328396 1130316 1468846 836834 1116369 1764066 23...
output:
287964952 603855872 35995619 2358812 337581814 287964952 287964952 287964952 287964952 779896829 287964952 287964952 287964952 585654112 866781526 35995619 143982476 287964952 287964952 35995619 287964952 287964952 287964952 287964952 287964952 545250000 287964952 71991238 356745197 143982476 287964...
result:
ok 755 numbers
Test #5:
score: 0
Accepted
time: 222ms
memory: 93344kb
input:
3734 20000 67 1389602 980790 2386085 1588484 2146516 1958159 642773 1716215 8079 1390992 1759761 2249217 765459 1929585 2118921 2105012 772668 984626 547175 1431565 170214 158951 1747481 720208 2833374 1145557 935993 2380503 1866168 1786126 914416 226298 685975 2045140 2995280 2664012 2149948 123450...
output:
197365516 551326836 561392495 934386097 831902835 368564644 820576782 135908312 28978034 857802169 412595593 897725368 453922016 594795473 32568607 686163581 643980228 577320582 468537842 208135614 183185490 599452355 234405712 915650810 325429822 215832653 635537720 642480236 699742763 948160486 36...
result:
ok 3734 numbers
Test #6:
score: 0
Accepted
time: 113ms
memory: 75016kb
input:
100000 2 5 2821965 2145474 853310 2911664 2627670 675327 2311210 1625221 1157380 781808 2 5 1679375 1167880 1289337 64420 2022177 774138 352469 1425384 1116876 193830 2 5 639747 904150 1513642 308309 914801 572251 2736819 303116 1418330 487350 2 5 2319520 900918 2481864 2800242 2869240 675529 501477...
output:
16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 ...
result:
ok 100000 numbers
Test #7:
score: 0
Accepted
time: 258ms
memory: 137896kb
input:
1 1000000 3 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 ...
output:
1
result:
ok 1 number(s): "1"
Test #8:
score: -100
Wrong Answer
time: 183ms
memory: 86460kb
input:
1 1019 2939 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 3000000 ...
output:
0
result:
wrong answer 1st numbers differ - expected: '1', found: '0'