QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#83415 | #5442. Referee Without Red | xiaoyaowudi | WA | 421ms | 52156kb | C++14 | 3.6kb | 2023-03-01 20:37:43 | 2023-03-01 20:37:45 |
Judging History
answer
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
constexpr int N(3e6+10),p(998244353);
int n,m,v[N],r,c;
int h(int x,int y){return (x-1)*m+y;}
int o(int x,int y){return (x-1)*c+y;}
int lcm(int x,int y){return x/std::__gcd(x,y)*y;}
void calc(int *a,int *b,int *c,int &k,int n)
{
int cnt(0);static bool vis[N];std::memset(vis+1,0,sizeof(bool)*n);
for(int i(1);i<=n;++i) if(!vis[i])
{
++k;b[k]=cnt+1;int u(i);
do
{
if((u%2)==1) u=(u+1)/2;
else u=u/2+(n+1)/2;
a[++cnt]=u;
vis[u]=true;
}while(u!=i);
c[k]=cnt;
}
// std::cerr<<"calc "<<cnt<<" "<<n<<std::endl;
}
template<typename T> void clr(T *k,int num){std::memset(k+1,0,sizeof(T)*num);}
bool vvc[N],vvr[N],rid[N],cid[N];
bool dfsc(int _c);
bool dfsr(int _r){bool ret(true);vvr[_r]=true;
for(int i(1);i<=c;++i) if(rid[o(_r,i)]){if(!cid[o(_r,i)]) ret=false;else if(!vvc[i]){auto res=dfsc(i);if(!res) ret=false;}}return ret;}
bool dfsc(int _c){bool ret(true);vvc[_c]=true;
for(int i(1);i<=r;++i) if(cid[o(i,_c)]){if(!rid[o(i,_c)]) ret=false;else if(!vvr[i]){auto res=dfsr(i);if(!res) ret=false;}}return ret;}
int calc(int *a,int n)
{
static int f[N];
// for(int i(1);i<=n;++i) std::cerr<<a[i]<<" ";std::cerr<<std::endl;
for(int i(2);i<=n;++i)
{
int j(f[i-1]);
while(j && a[j+1]!=a[i]) j=f[j];
if(a[j+1]==a[i]) ++j;
f[i]=j;
}
int k(f[n]);
while(n%(n-k)) k=f[k];
// std::cerr<<k<<std::endl;
return n-k;
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int T;std::cin>>T;static int inv[N],fac[N];inv[1]=fac[0]=fac[1]=1;
for(int i(2);i<N;++i) inv[i]=1ll*inv[p%i]*(p-p/i)%p,fac[i]=1ll*fac[i-1]*i%p;
while(T--)
{
std::cin>>n>>m;
for(int i(1);i<=n;++i) for(int j(1);j<=m;++j) std::cin>>v[h(i,j)];
static int ci[N],cs[N],ce[N],ri[N],rs[N],re[N],vq[N],qid,vc[N];
static bool hvc[N],hvr[N];
r=c=0;
calc(ci,cs,ce,c,m);calc(ri,rs,re,r,n);
clr(rid,r*c);clr(cid,r*c);clr(hvc,c);clr(hvr,r);clr(vvc,c);clr(vvr,r);
int ans(1);
for(int i(1);i<=r;++i) if(re[i]-rs[i])
{
for(int j(1);j<=c;++j) if(ce[j]-cs[j])
{
++qid;bool cap(true);
// std::cerr<<"nmsl"<<std::endl;
for(int x(rs[i]);x<=re[i];++x)
{
for(int y(cs[j]);y<=ce[j];++y)
{
int cur(v[h(ri[x],ci[y])]);
// std::cerr<<cur<<" ";
if(vq[cur]==qid)
{
ans=1ll*ans*inv[++vc[cur]]%p;
cap=false;
}
else
{
vq[cur]=qid;
vc[cur]=1;
}
}
// std::cerr<<std::endl;
}
ans=1ll*ans*fac[(re[i]-rs[i]+1)*(ce[j]-cs[j]+1)]%p;
// std::cerr<<cap<<std::endl;
if(cap)
{
ans=1ll*ans*inv[2]%p;
if((re[i]-rs[i])&1)
{
rid[o(i,j)]=true;
hvr[i]=true;
}
if((ce[j]-cs[j])&1)
{
cid[o(i,j)]=true;
hvc[j]=true;
}
}
}
}
// std::cout<<ans<<std::endl;
for(int i(1);i<=r;++i) if(hvr[i]){ans=2ll*ans%p;if(!vvr[i]){bool ret(dfsr(i));if(ret) ans=1ll*ans*inv[2]%p;}}
for(int i(1);i<=c;++i) if(hvc[i]){ans=2ll*ans%p;if(!vvc[i]){bool ret(dfsc(i));if(ret) ans=1ll*ans*inv[2]%p;}}
// std::cout<<ans<<std::endl;
for(int i(1);i<=r;++i) if(re[i]==rs[i]){int t(1);for(int j(1);j<=c;++j) if(ce[j]-cs[j])
{
static int tmp[N];int ct(0);for(int k(cs[j]);k<=ce[j];++k) tmp[++ct]=v[h(ri[rs[i]],ci[k])];
t=lcm(t,calc(tmp,ct));
}ans=1ll*ans*t%p;}
for(int i(1);i<=c;++i) if(ce[i]==cs[i]){int t(1);for(int j(1);j<=r;++j) if(re[j]-rs[j])
{
static int tmp[N];int ct(0);for(int k(rs[j]);k<=re[j];++k) tmp[++ct]=v[h(ri[k],ci[cs[i]])];
t=lcm(t,calc(tmp,ct));
}ans=1ll*ans*t%p;}
std::cout<<ans<<"\n";
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 32ms
memory: 27840kb
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: 35ms
memory: 28628kb
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: 421ms
memory: 52156kb
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 618786068 9 571768698 40098691 97370043 799392782 155415144 1 36 3991680 645120 545661527 55...
result:
wrong answer 27th numbers differ - expected: '239327783', found: '618786068'