QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#287049 | #5442. Referee Without Red | ushg8877 | WA | 330ms | 129020kb | C++14 | 2.2kb | 2023-12-19 13:43:39 | 2023-12-19 13:43:39 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MP make_pair
mt19937 rnd(time(0));
const int MAXN=3e6+5;
const int MOD=998244353;
int n,m;
vector<int> a[MAXN];
ll fac[MAXN],inf[MAXN];
int p[MAXN],q[MAXN],np,nq;bool visx[MAXN],visy[MAXN];
vector<vector<int> > ls,rs;
ll ksm(ll a,int b){ll r=1;while(b){if(b&1)r=r*a%MOD;a=a*a%MOD,b>>=1;}return r;}
ll repeat(vector<int> a){
int n=a.size();
vector<int> border(n);
border[0]=-1;
for(int i=1;i<n;i++){
int x=border[i-1];
while(x>=0&&a[x+1]!=a[i]) x=border[x];
if(a[x+1]==a[i]) x++;
border[i]=x;
}
if(n%(n-1-border[n-1])) return n;
else return n-1-border[n-1];
}
ll solve_line(int x){
ll ans=1;
for(auto &b:rs){
vector<int> p;
for(int i:b) p.push_back(a[x][i]);
ans=ans*repeat(p)%MOD;
}
return ans;
}
ll solve_comm(int x){
ll ans=1;
for(auto &b:ls){
vector<int> p;
for(int i:b) p.push_back(a[i][x]);
ans=ans*repeat(p)%MOD;
}
return ans;
}
void solve(){
ls.clear(),rs.clear();
cin>>n>>m;
for(int i=1;i<=n;i++){
a[i].resize(m+1);
for(int j=1;j<=m;j++) cin>>a[i][j];
}
np=nq=0;
for(int i=1;i<=n;i+=2) p[++np]=i;
for(int i=2;i<=n;i+=2) p[++np]=i;
for(int i=1;i<=m;i+=2) q[++nq]=i;
for(int i=2;i<=m;i+=2) q[++nq]=i;
for(int i=1;i<=n;i++) visx[i]=false;
for(int i=1;i<=m;i++) visy[i]=false;
ll ans=1;
for(int i=1;i<=n;i++)
if(p[i]!=i&&!visx[i]){
vector<int> a;
int t=i;
while(!visx[t]) a.push_back(t),visx[t]=true,t=p[t];
ls.push_back(a);
}
for(int i=1;i<=m;i++)
if(q[i]!=i&&!visy[i]){
vector<int> a;
int t=i;
while(!visy[t]) a.push_back(t),visy[t]=true,t=q[t];
rs.push_back(a);
}
for(auto &l:ls) for(auto &r:rs){
map<int,int> mp;
int cnt=0;
for(int i:l) for(int j:r){
cnt++;mp[a[i][j]]++;
}
ans=ans*fac[cnt]%MOD;
for(auto i:mp) ans=ans*inf[i.second]%MOD;
}
for(int i=1;i<=n;i++) if(p[i]==i) ans=ans*solve_line(i)%MOD;
for(int i=1;i<=m;i++) if(q[i]==i) ans=ans*solve_comm(i)%MOD;
cout<<ans<<endl;
return;
}
int main(){
ios::sync_with_stdio(false);
fac[0]=inf[0]=1;
for(int i=1;i<MAXN;i++) inf[i]=ksm(fac[i]=fac[i-1]*i%MOD,MOD-2);
int T;cin>>T;
while(T--) solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 327ms
memory: 129020kb
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: 330ms
memory: 128020kb
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:
546340904
result:
wrong answer 1st numbers differ - expected: '690561281', found: '546340904'