QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#534926#6325. Peaceful Resultsucup-team1525#WA 125ms65800kbC++207.2kb2024-08-27 17:33:442024-08-27 17:33:44

Judging History

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

  • [2024-08-27 17:33:44]
  • 评测
  • 测评结果:WA
  • 用时:125ms
  • 内存:65800kb
  • [2024-08-27 17:33:44]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using ull=unsigned long long;
const int mod=998244353;
const int N=1<<21;
using arr=int[N+5];
int add(int x,int y){ return (x+=y)>=mod?x-mod:x; }
int sub(int x,int y){ return (x-=y)<0?x+mod:x; }
int red(int &x){ return x+=(x>>31)&mod; }
int ksm(ll x,int tp,int s=1){
	for(;tp;x=x*x%mod,tp>>=1) if(tp&1) s=x*s%mod;
	return s;
}
arr fac,ifac,inv;
void prep(){
	fac[0]=ifac[0]=1;
	for(int i=1;i<=N;i++) fac[i]=1ll*fac[i-1]*i%mod;
	ifac[N]=ksm(fac[N],mod-2);
	for(int i=N;i;i--) ifac[i-1]=1ll*ifac[i]*i%mod;
	for(int i=1;i<=N;i++) inv[i]=1ll*ifac[i]*fac[i-1]%mod;
}
int C(int n,int m){
    return 1ll*fac[n]*ifac[m]%mod*ifac[n-m]%mod;
}
namespace poly{
	int L,iv;
	arr w,rev;
	arr pa,pb;
	#define szf sizeof(int)
	void cl(arr a){ memset(a,0,L*szf); }
	void r_prep(){
		int L=N>>1;
		int val=ksm(3,(mod-1)/N);
		w[L]=1;
		for(int i=1;i<L;i++) w[i+L]=1ll*w[i+L-1]*val%mod;
		for(int i=L-1;i;i--) w[i]=w[i<<1];
	}
	void pre_n(int n){
		L=1; while(L<n) L<<=1;
		iv=mod-(mod-1)/L;
		for(int i=1;i<L;i++) rev[i]=(rev[i>>1]>>1)|(i&1?L>>1:0);
	}
	void FFT(arr t,bool ok=1){
		int x,y;
		for(int i=1;i<L;i++)
			if(i<rev[i]) swap(t[i],t[rev[i]]);
		for(int i=1;i<L;i<<=1)
			for(int j=0;j<L;j+=i<<1)
				for(int l=0;l<i;l++){
					x=t[j+l],y=1ll*t[i+j+l]*w[i+l]%mod;
					t[i+j+l]=sub(x,y),t[j+l]=add(x,y);
				}
		if(!ok){
			reverse(t+1,t+L);
			for(int i=0;i<L;i++) t[i]=1ll*t[i]*iv%mod;
		}
	}
	void NTT(arr a){ FFT(a); }
	void INTT(arr a){ FFT(a,0); }
	void NTT(arr a,arr b){ memcpy(b,a,L*szf); FFT(b); }
	void INTT(arr a,arr b){ memcpy(b,a,L*szf); FFT(b,0); }

	void Mult(arr a,arr b,arr c,int n){
		pre_n(n);
		NTT(a,pa); NTT(b,pb);
		for(int i=0;i<L;i++)
			c[i]=1ll*pa[i]*pb[i]%mod;
		INTT(c);
		fill(c+n,c+L,0);
		cl(pa); cl(pb);
	}
	void Inv(arr a,arr b,int n){
		int i,j,l;
		pre_n(n); l=L;
		cl(b); *b=ksm(*a,mod-2);
		for(i=2;i<=l;i<<=1){
			pre_n(i);
			NTT(a,pa); NTT(b,pb);
			for(j=0;j<i;j++) pa[j]=1ll*pa[j]*pb[j]%mod;
			INTT(pa); fill(pa,pa+(i>>1),0); NTT(pa);
			for(j=0;j<i;j++) pa[j]=1ll*(mod-pa[j])*pb[j]%mod;
			INTT(pa); j=i>>1;
			memcpy(b+j,pa+j,j*szf);
		}
		fill(b+n,b+l,0);
		cl(pa); cl(pb);
	}
	void Inv2(arr a,arr b,arr c,int n){
		static arr d;
		Inv(a,d,n);
		Mult(b,d,c,n*2);
		fill(c+n,c+2*n,0);
		cl(d);
	}
	void diff(arr t,int n){
		for(int i=1;i<n;i++)
			t[i-1]=1ll*i*t[i]%mod;
		t[n-1]=0;
	}
	void inte(arr t,int n){
		for(int i=n-1;i;i--)
			t[i]=1ll*t[i-1]*inv[i]%mod;
		t[0]=0;
	}
	void Ln(arr a,arr b,int n){
		pre_n(n); cl(b);
		memcpy(b,a,n*szf);
		diff(b,n);
		Inv2(a,b,b,n);
		inte(b,n);
	}
	void Exp(arr a,arr b,int n){
		static arr e;
		int i,j,l;
		pre_n(n);l=L;
		cl(b); *b=1;
		for(i=2;i<=l;i<<=1){
			Ln(b,e,i);
			for(j=0;j<i;j++) e[j]=sub(a[j],e[j]);
			Mult(e,b,e,i); j=i>>1;
			memcpy(b+j,e+j,j*szf);
		}
		fill(b+n,b+l,0);
		cl(e);
	}
}
int n;
int a[5][5];
ll f[10][10];
ll g[10],ss[10],ns[10];
int main()
{
    prep();
    poly::r_prep();
    scanf("%d",&n);
    for (int i=1;i<=3;++i)
        for (int j=1;j<=3;++j)
            scanf("%d",&a[i][j]);
    //111 -1
    //222 -2
    //333 -3
    //123 -4
    //231 -5
    //321 -6
    //213 -7

    f[1][1]=1;f[1][4]=1;g[1]=a[1][1]; //x1+x4=a11
    f[2][2]=1;f[2][5]=1;f[2][7]=1;g[2]=a[1][2];//x2+x5+x7=a12
    f[3][3]=1;f[3][6]=1;g[3]=a[1][3];//x3+x6=a13
    f[4][1]=1;f[4][7]=1;g[4]=a[2][1];//x1+x7=a21
    f[5][2]=1;f[5][4]=1;f[5][6]=1;g[5]=a[2][2];//x2+x4+x6=a22
    f[6][3]=1;f[6][5]=1;g[6]=a[2][3];//x3+x5=a23
    f[7][1]=1;f[7][5]=1;f[7][6]=1;g[7]=a[3][1];//x1+x5+x6=a31
    f[8][2]=1;g[8]=a[3][2];//x2=a32
    f[9][3]=1;f[9][4]=1;f[9][7]=1;g[9]=a[3][3];//x3+x4+x7=a33

    for (int i=1;i<=7;++i)
    {
        if (!f[i][i])
        {
            for (int j=i+1;j<=9;++j)
                if (f[j][i])
                {
                    for (int k=1;k<=7;++k)
                        swap(f[i][k],f[j][k]);
                    swap(g[i],g[j]);
                    break;
                }
        }
        assert(f[i][i]);
        ll lcm=1;
        for (int j=1;j<=9;++j)
            if (f[j][i])
                lcm=lcm/__gcd(abs(f[j][i]),lcm)*abs(f[j][i]);
        int inv=lcm/f[i][i];
        for (int j=1;j<=7;++j)
            // f[i][j]=(ll)f[i][j]*inv%mod;
            f[i][j]=f[i][j]*inv;
        // g[i]=(ll)g[i]*inv%mod;
        g[i]=g[i]*inv;
        for (int j=1;j<=9;++j)
            if (f[j][i]&&i!=j)
            {
                ll tmp=lcm/f[j][i];
                for (int k=1;k<=7;++k)
                {
                    f[j][k]*=tmp;
                    f[j][k]-=f[i][k];
                }
                g[j]*=tmp;
                g[j]-=g[i];
                // for (int k=1;k<=7;++k)
                //     (f[j][k]+=(ll)tmp*f[i][k]%mod)%=mod;
                // (g[j]+=(ll)tmp*g[i]%mod)%=mod;
            }
    }
    for (int i=1;i<=7;++i)
        if (g[i]%f[i][i]!=0)
        {
            puts("0");
            return 0;
        }
        else
            g[i]/=f[i][i];
    // for (int i=1;i<=7;++i)
    //     printf("%d:%d\n",i,g[i]);
    ss[1]=g[4];ss[2]=g[5];ss[3]=0;
    ns[3]=g[6];ns[2]=g[7];ns[1]=0;
    for (int i=1;i<=2;++i)
        // if (ss[i]>n)
        // {
        //     int tmp=mod-ss[i];
        //     for (int j=1;j<=3;++j)
        //     {
        //         g[j]-=tmp;
        //         g[j]%=mod;
        //         (ss[j]+=tmp)%=mod;
        //     }
        // }
        if (ss[i]<0)
        {
            int tmp=-ss[i];
            for (int j=1;j<=3;++j)
            {
                g[j]-=tmp;
                ss[j]+=tmp;
            }
        }
    for (int i=1;i<=2;++i)
        // if (ns[i]>n)
        // {
        //     int tmp=mod-ns[i];
        //     for (int j=1;j<=3;++j)
        //     {
        //         g[j]-=tmp;
        //         g[j]%=mod;
        //         (ns[j]+=tmp)%=mod;
        //     }
        // }
        if (ns[i]<0)
        {
            int tmp=-ns[i];
            for (int j=1;j<=3;++j)
            {
                g[j]-=tmp;
                ns[j]+=tmp;
            }
        }
    for(int i=1;i<=3;i++){
        if (g[i]<0||ss[i]<0||ns[i]<0)
        {
            puts("0");
            return 0;
        }
        // g[i]=(g[i]+mod)%mod;
        // ss[i]=(ss[i]+mod)%mod;
        // ns[i]=(ns[i]+mod)%mod;
        // if(g[i]>n||ss[i]>n||ns[i]>n){
        //     puts("0");
        //     return 0;
        // }
    }
    // printf("equal: %d %d %d\n",g[1],g[2],g[3]);
    // printf("clock: %d %d %d\n",ss[1],ss[2],ss[3]);
    // printf("invclock: %d %d %d\n",ns[1],ns[2],ns[3]); 
    static arr ta,tb,tc;
    int L=min({g[1],g[2],g[3]});
    for(int i=0;i<=L;i++){
        ta[i]=1;
        for(int j=1;j<=3;j++)
            ta[i]=1ll*ifac[ss[j]+i]*ta[i]%mod;
    }
    for(int i=0;i<=L;i++){
        tb[i]=1;
        for(int j=1;j<=3;j++)
            tb[i]=1ll*ifac[ns[j]+i]*tb[i]%mod;
    }
    poly::Mult(ta,tb,tc,L*2+2);
    int ans=0;
    for(int i=0;i<=L;i++){
        int s=tc[i];
        for(int j=1;j<=3;j++)
            s=1ll*s*ifac[g[j]-i]%mod;
        ans=add(ans,s);
    }
    printf("%lld\n",1ll*fac[n]*ans%mod);
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 28ms
memory: 44788kb

input:

2
2 0 0
1 1 0
1 0 1

output:

2

result:

ok 1 number(s): "2"

Test #2:

score: 0
Accepted
time: 24ms
memory: 38440kb

input:

3
0 1 2
3 0 0
1 1 1

output:

0

result:

ok 1 number(s): "0"

Test #3:

score: 0
Accepted
time: 45ms
memory: 54692kb

input:

333333
111111 111111 111111
111111 111111 111111
111111 111111 111111

output:

383902959

result:

ok 1 number(s): "383902959"

Test #4:

score: 0
Accepted
time: 118ms
memory: 61200kb

input:

1500000
500000 500000 500000
500000 500000 500000
500000 500000 500000

output:

355543262

result:

ok 1 number(s): "355543262"

Test #5:

score: 0
Accepted
time: 125ms
memory: 65800kb

input:

1499999
499999 499999 500001
499999 499999 500001
499999 499999 500001

output:

934301164

result:

ok 1 number(s): "934301164"

Test #6:

score: 0
Accepted
time: 28ms
memory: 44816kb

input:

1500000
1 0 1499999
1499999 1 0
0 1499999 1

output:

1500000

result:

ok 1 number(s): "1500000"

Test #7:

score: 0
Accepted
time: 24ms
memory: 44732kb

input:

1499999
0 749999 750000
750000 0 749999
749999 750000 0

output:

713966599

result:

ok 1 number(s): "713966599"

Test #8:

score: 0
Accepted
time: 16ms
memory: 44796kb

input:

1
1 0 0
0 0 1
0 1 0

output:

1

result:

ok 1 number(s): "1"

Test #9:

score: 0
Accepted
time: 20ms
memory: 44796kb

input:

1
0 1 0
0 1 0
0 1 0

output:

1

result:

ok 1 number(s): "1"

Test #10:

score: 0
Accepted
time: 28ms
memory: 38700kb

input:

1
0 0 1
1 0 0
1 0 0

output:

0

result:

ok 1 number(s): "0"

Test #11:

score: -100
Wrong Answer
time: 19ms
memory: 38680kb

input:

1499999
500000 500000 499999
499999 499999 500001
499999 499999 500001

output:

0

result:

wrong answer 1st numbers differ - expected: '617065435', found: '0'