QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#645285#5442. Referee Without RedWuyanruWA 150ms107644kbC++144.8kb2024-10-16 17:31:222024-10-16 17:31:29

Judging History

你现在查看的是最新测评结果

  • [2024-10-16 17:31:29]
  • 评测
  • 测评结果:WA
  • 用时:150ms
  • 内存:107644kb
  • [2024-10-16 17:31:22]
  • 提交

answer

#include<bits/stdc++.h>
#define inf 0x3f3f3f3f3f3f3f3fll
#define debug(x) cerr<<#x<<"="<<x<<endl
using namespace std;
using ll=long long;
using ld=long double;
using pli=pair<ll,int>;
using pi=pair<int,int>;
template<typename A>
using vc=vector<A>;
inline int read()
{
    int s=0,w=1;char ch;
    while((ch=getchar())>'9'||ch<'0') if(ch=='-') w=-1;
    while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
    return s*w;
}
inline ll lread()
{
    ll s=0,w=1;char ch;
    while((ch=getchar())>'9'||ch<'0') if(ch=='-') w=-1;
    while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
    return s*w;
}
const int mod=998244353;
bool vis1[1000005];
bool vis2[1000005];
ll jc[1000005];
ll inv[1000005];
int pri[1000005];
int is[3000005];
int wtf[1000005];
int fa[2000005];
vc<int>a[1000005];
vc<int>w1[1000005];
vc<int>w2[1000005];
bool v[1000005];
int p[1000005];
int s[1000005];
int nx[1000005];
int n,m,c1,c2;
vc<int>now;
inline ll qow(ll a,ll b)
{
    ll ans=1;
    while(b)
    {
        if(b&1) ans=ans*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return ans;
}
inline void init(int L)
{
    for(int i=2;i<=L;i++)
    {
        if(!is[i]) pri[++pri[0]]=i;
        for(int j=1;j<=pri[0]&&i*pri[j]<=L;j++)
        {
            is[i*pri[j]]=1;
            if(i%pri[j]==0) break;
        }
    }
    for(int i=1;i<=pri[0];i++)
    {
        ll now=pri[i];
        while(now<=L) wtf[now]=pri[i],now*=pri[i];
    }
    memset(is,0,sizeof(is));

    jc[0]=1;
    for(int i=1;i<=L;i++) jc[i]=jc[i-1]*i%mod;
    inv[L]=qow(jc[L],mod-2);
    for(int i=L;i;i--) inv[i-1]=inv[i]*i%mod;
    for(int i=1;i<=L;i++) inv[i]=jc[i-1]*inv[i]%mod;
}
inline int run(int len)
{
    for(int i=2,j=0;i<=len;i++)
    {
        while(j&&s[j+1]!=s[i]) j=nx[j];
        if(s[j+1]==s[i]) j++;
        nx[i]=j;
    }
    int ans=len;
    while(nx[len])
    {
        if(len%(len-nx[len])==0) ans=min(ans,len-nx[len]);
        nx[len]=nx[nx[len]];
    }
    return ans;
}
inline ll push(int v)
{
    ll ans=1;
    for(int i=1;i<=v;i++) if(v%i==0&&wtf[i]&&!is[i])
    {
        ans=ans*wtf[i]%mod,is[i]=1;
        now.push_back(i);
    }
    return ans;
}
inline void clear()
{
    for(int i:now) is[i]=0;
    now.clear();
}
inline int find(int num)
{
    if(fa[num]==num) return num;
    return fa[num]=find(fa[num]);
}
inline ll get(int x,int y,bool &f)
{
    f=0;ll ans=1;int cnt=0;
    for(int i:w1[x]) for(int j:w2[y])
    {
        cnt++,ans=ans*cnt%mod;
        if(is[a[i][j]]) f=1;
        is[a[i][j]]++,ans=ans*inv[is[a[i][j]]]%mod;
    }
    for(int i:w1[x]) for(int j:w2[y]) is[a[i][j]]=0;
    return ans;
}
int main()
{
    int T=read();init(1000000);
    while(T--)
    {
        n=read(),m=read(),c1=c2=0;
        for(int i=1;i<=n;i++)
        {
            a[i].resize(m+1);
            for(int j=1;j<=m;j++) a[i][j]=read();
        }
        for(int i=1;i<=n;i++) p[i]=(i&1)?(i/2+1):((n+i+1)/2),v[i]=0;
        for(int i=1;i<=n;i++) if(!v[i])
        {
            c1++;w1[c1].clear();vis1[c1]=0;
            for(int j=i;!v[j];j=p[j]) w1[c1].push_back(j),v[j]=1;
        }
        for(int i=1;i<=m;i++) p[i]=(i&1)?(i/2+1):((m+i+1)/2),v[i]=0;
        for(int i=1;i<=m;i++) if(!v[i])
        {
            c2++;w2[c2].clear();vis2[c2]=0;
            for(int j=i;!v[j];j=p[j]) w2[c2].push_back(j),v[j]=1;
        }

        for(int i=1;i<=c1+c2;i++) fa[i]=i;

        ll ans=1;
        for(int i=1;i<=c1;i++) if(w1[i].size()==1)
        {
            for(int j=1;j<=c2;j++)
            {
                int len=0;
                for(int p:w2[j]) s[++len]=a[w1[i][0]][p];
                ans=ans*push(run(len))%mod;
            }
            clear();
        }
        for(int i=1;i<=c2;i++) if(w2[i].size()==1)
        {
            for(int j=1;j<=c1;j++)
            {
                int len=0;
                for(int p:w1[j]) s[++len]=a[p][w2[i][0]];
                ans=ans*push(run(len))%mod;
            }
            clear();
        }

        for(int i=1;i<=c1;i++) for(int j=1;j<=c2;j++)
        {
            if(w1[i].size()==1||w2[j].size()==1) continue;
            bool f;ll val=get(i,j,f);
            if(f) ans=ans*val%mod;
            else
            {
                ans=ans*val%mod*inv[2]%mod;
                if(w1[i].size()%2==0&&w2[j].size()%2==0)
                {
                    int u=find(i),v=find(c1+j);
                    if(u!=v) ans=ans*2%mod,fa[u]=v;
                }
                else if(w1[i].size()%2==0)
                {
                    if(!vis1[i]) vis1[i]=1,ans=ans*2%mod;
                }
                else if(w2[j].size()%2==0)
                {
                    if(!vis2[j]) vis2[j]=1,ans=ans*2%mod;
                }
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 23ms
memory: 105700kb

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: 25ms
memory: 107644kb

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: 150ms
memory: 106440kb

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'