QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#742766#5442. Referee Without RedwosileRE 21ms98268kbC++146.0kb2024-11-13 17:16:012024-11-13 17:16:07

Judging History

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

  • [2024-11-13 17:16:07]
  • 评测
  • 测评结果:RE
  • 用时:21ms
  • 内存:98268kb
  • [2024-11-13 17:16:01]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
int Tc,n,m;
#define N 1000005
vector<int>a[N],bp[N],bq[N];
int p[N],q[N],fp[N],fq[N],fa[N];
int find(int *f,int x){
    if(f[x]==x)return x;
    return f[x]=find(f,f[x]);
}
inline void merge(int *f,int x,int y){
    int fx=find(f,x),fy=find(f,y);
    if(fx!=fy)f[fx]=fy;
}
int lmx[N],kmp[N];
#define mod 998244353
inline int calc(vector<int>&s){
    int len=s.size();
    s.insert(s.begin(),0);
    // for(int i=2;i<=len;i++){
    //     int tmp=kmp[i-1];
    //     while(s[tmp+1]!=s[i] && tmp>0)tmp=kmp[tmp];
    //     if(s[tmp+1]==s[i])kmp[i]=tmp+1;
    //     else kmp[i]=tmp;
    // }
    // int tmp=kmp[len];
    // while(len%(len-tmp)){
    //     tmp=kmp[tmp];
    //     // cerr<<tmp<<endl;
    // }
    // return len-tmp;
    for(int i=1;i<=len;i++)if(len%i==0){
        int fl=1;
        for(int j=i+1;j<=len;j++)if(s[j]!=s[j-i])fl=0;
        if(fl)return i;
    }
    return len;
}
vector<int>tmp;
typedef long long ll;
int qp(int x,int y){
    int ans=1;
    while(y){
        if(y&1)ans=(ll)ans*x%mod;
        x=(ll)x*x%mod;
        y>>=1;
    }
    return ans;
}
int cnt[N],ft[N],ift[N],inv[N],sz[N];
int main(){
    // freopen("matrix.in","r",stdin);
    // freopen("matrix.out","w",stdout);
    scanf("%d",&Tc);
    while(Tc--){
        // input
        // cerr<<"input start"<<endl;
        scanf("%d%d",&n,&m);
        ft[0]=1;
        for(int i=1;i<=n*m;i++)ft[i]=(ll)ft[i-1]*i%mod;
        ift[n*m]=qp(ft[n*m],mod-2);
        for(int i=n*m;i>=1;i--)ift[i-1]=(ll)ift[i]*i%mod;
        for(int i=1;i<=n*m;i++)inv[i]=(ll)ift[i]*ft[i-1]%mod;
        for(int i=1;i<=n;i++)a[i].resize(m+2);
        for(int i=1;i<=n;i++){
            if(i&1)p[i]=i/2+1;
            else p[i]=i/2+(n+1)/2;
        }
        for(int i=1;i<=m;i++){
            if(i&1)q[i]=i/2+1;
            else q[i]=i/2+(m+1)/2;
        }
        // for(int i=1;i<=n;i++)scanf("%d",&p[i]);
        // for(int j=1;j<=m;j++)scanf("%d",&q[j]);
        for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)scanf("%d",&a[i][j]);

        // blocking
        // cerr<<"blocking start"<<endl;
        for(int i=1;i<=n;i++)fp[i]=i;
        for(int i=1;i<=m;i++)fq[i]=i;
        for(int i=1;i<=n;i++)merge(fp,i,p[i]);
        for(int i=1;i<=m;i++)merge(fq,i,q[i]);
        for(int i=1;i<=n;i++)bp[i].clear();
        for(int i=1;i<=m;i++)bq[i].clear();
        for(int i=1;i<=n;i++)if(find(fp,i)==i){
            int tmp=i;
            do{
                bp[i].push_back(tmp);
                tmp=p[tmp];
            }
            while(tmp!=i);
        }
        for(int i=1;i<=m;i++)if(find(fq,i)==i){
            int tmp=i;
            do{
                bq[i].push_back(tmp);
                tmp=q[tmp];
            }
            while(tmp!=i);
        }
        int ans=1;

        // long note
        // cerr<<"long note start"<<endl;
        for(int i=1;i<=n;i++)if(find(fp,i)==i && bp[i].size()==1){
            for(int j=1;j<=m;j++)lmx[j]=0;
            for(int j=1;j<=m;j++)if(find(fq,j)==j){
                tmp.clear();
                for(int x:bq[j])tmp.push_back(a[i][x]);
                // if(tmp.size()==368 && i==1){
                //     for(int x:tmp)printf("%d ",x);
                //     printf("\n");
                // }
                // printf("qwq=%llu\n",tmp.size());
                int len=calc(tmp);
                
                // printf("len=%d\n",len);
                for(int p=2;p<=len;p++){
                    int cnt=0;
                    while(len%p==0){
                        ++cnt;
                        len/=p;
                    }
                    lmx[p]=max(lmx[p],cnt);
                }
            }
            // cout<<"long i="<<i<<endl;
            // for(int j=1;j<=n;j++)printf("%d ",lmx[j]);printf("\n");
            for(int j=1;j<=m;j++){
                ans=(ll)ans*qp(j,lmx[j])%mod;
                // if(lmx[j])printf("%d %d\n",j,lmx[j]);
            }
        }
        // cerr<<"long note halfway"<<endl;
        for(int i=1;i<=m;i++)if(find(fq,i)==i && bq[i].size()==1){
            // cerr<<"i="<<i<<endl;
            for(int j=1;j<=n;j++)lmx[j]=0;
            // cerr<<"i="<<i<<endl;
            for(int j=1;j<=n;j++)if(find(fp,j)==j){
                tmp.clear();
                for(int x:bp[j])tmp.push_back(a[x][i]);
                int len=calc(tmp);
                // cerr<<"len="<<len<<endl;
                for(int p=2;p<=len;p++){
                    int cnt=0;
                    while(len%p==0){
                        ++cnt;
                        len/=p;
                    }
                    lmx[p]=max(lmx[p],cnt);
                }
            }
            // cout<<"long i="<<i<<endl;
            // for(int j=1;j<=n;j++)printf("%d ",lmx[j]);printf("\n");
            for(int j=1;j<=n;j++)ans=(ll)ans*qp(j,lmx[j])%mod;
        }
        // cerr<<"ans="<<ans<<endl;
        // fat note
        // cerr<<"fat note start "<<endl;
        for(int i=0;i<=n+m;i++)fa[i]=i;
        for(int i=1;i<=n;i++)if(find(fp,i)==i && bp[i].size()>1)for(int j=1;j<=m;j++)if(find(fq,j)==j && bq[j].size()>1){
            // cerr<<i<<' '<<j<<endl;
            // assert(false);
            int fl=0,prod=ft[bp[i].size()*bq[j].size()];
            // printf("prod=%d\n",prod);
            for(int x:bp[i])for(int y:bq[j]){
                cnt[a[x][y]]++;
                prod=(ll)prod*inv[cnt[a[x][y]]]%mod;
                if(cnt[a[x][y]]>1)fl=1;
            }
            for(int x:bp[i])for(int y:bq[j])cnt[a[x][y]]--;
            if(fl){
                ans=(ll)ans*prod%mod;
                continue;
            }
            ans=(ll)ans*prod%mod*inv[2]%mod;
            merge(fa,(bp[i].size()&1)?0:j+n,(bq[j].size()&1)?0:i);
        }
        for(int i=0;i<=n+m;i++)sz[i]=0;
        for(int i=0;i<=n+m;i++){
            sz[find(fa,i)]++;
            if(sz[find(fa,i)]>1)ans=ans*2%mod;
        }
        printf("%d\n",ans);
    }
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 21ms
memory: 98268kb

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: 12ms
memory: 97240kb

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...

result: