QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#849619#1262. Justice For EveryoneluanyanjiaAC ✓369ms8736kbC++142.7kb2025-01-09 16:40:372025-01-09 16:40:37

Judging History

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

  • [2025-01-09 16:40:37]
  • 评测
  • 测评结果:AC
  • 用时:369ms
  • 内存:8736kb
  • [2025-01-09 16:40:37]
  • 提交

answer

#include<bits/stdc++.h>
#define pii std::pair<int,int>
#define mkp std::make_pair
#define fir first
#define sec second
typedef long long ll;
inline void rd(){}
template<typename T,typename ...U>
inline void rd(T &x,U &...args){
	int ch=getchar();
	T f=1;x=0;
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9')x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
	x*=f;rd(args...);
}
const int N=35,M=6005,mod=998244353,V=6000;
int n,a[N],b[N],d[N],t;
int fac[M],INV[M],inv[M],Inv[M][205];
inline int KSM(int x,int n){
	int ans=1;
	while(n){
		if(n&1)ans=1ll*ans*x%mod;
		x=1ll*x*x%mod;
		n>>=1;
	}return ans;
}
inline void Init(){
	fac[0]=inv[0]=INV[0]=1;
	for(int i=1;i<=V;i++)fac[i]=1ll*fac[i-1]*i%mod;
	inv[V]=KSM(fac[V],mod-2);
	for(int i=V-1;i>=1;i--)inv[i]=1ll*inv[i+1]*(i+1)%mod;
	for(int i=1;i<=V;i++)INV[i]=1ll*inv[i]*fac[i-1]%mod;
	
}
int Y[M],A[M],B[M],mp[N][N];
inline int Det(){
	int f=1;
	for(int i=1;i<=n;i++){
		int cur=i;
		while(!mp[cur][i]&&cur<n)++cur;
		if(!mp[cur][i])return 0;
		if(cur!=i)std::swap(mp[cur],mp[i]),f=mod-f;
		for(int j=i+1;j<=n;j++){
			int div=1ll*mp[j][i]*KSM(mp[i][i],mod-2)%mod;
			for(int k=i;k<=n;k++){
				mp[j][k]=(mp[j][k]+1ll*mp[i][k]*(mod-div)%mod)%mod;
			}
		}
	}
	int ans=1;
	for(int i=1;i<=n;i++)ans=1ll*ans*mp[i][i]%mod;
	return 1ll*ans*f%mod;
}
inline int Calc(int x){
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			if(b[j]<a[i]){
				mp[i][j]=0;
				continue;
			}
			mp[i][j]=Inv[x][b[j]-a[i]];
		}
	}
	return Det();
}
int map[M];
signed main(){
	rd(n);
	for(int i=1;i<=n;i++)rd(a[i]);
	for(int i=1;i<=n;i++)rd(b[i]),map[a[i]]=b[i];
	for(int i=1;i<=n;i++){
		d[i]=b[i]-a[i];
		t+=d[i];
		if(d[i]<0)return puts("0"),0;
	}
	if(t&1)return puts("0"),0;
	t/=2;
	Init();
	for(int x=1;x<=t+1;x++){
		for(int r=0;r<=200;r++){
			int now=0,v=1;
			for(int k=0;k<=r/2;k++){
				now=(now+1ll*v*inv[k]%mod*inv[r-2*k]%mod)%mod;
				v=1ll*v*(mod-x)%mod;
			}
			Inv[x][r]=now;
		}
	}
	for(int i=1;i<=t+1;i++){
		Y[i]=Calc(i);
		for(int j=1;j<i;j++)Y[i]=1ll*Y[i]*INV[i-j]%mod;
		for(int j=i+1;j<=t+1;j++)Y[i]=1ll*Y[i]*(mod-INV[j-i])%mod;
	}
	B[0]=1;
	for(int i=1;i<=t+1;i++){
		for(int j=i-1;j>=0;j--){
			A[j]=(1ll*B[j]*Y[i]+1ll*A[j]*(mod-i))%mod;
			if(j)(A[j]+=A[j-1])%=mod;
		}
		for(int j=i;j>=0;j--){
			B[j]=1ll*B[j]*(mod-i)%mod;
			if(j)(B[j]+=B[j-1])%=mod;
		}
	}
	int ans=0;
	for(int i=0;i<=t;i++){
		ans=(ans+1ll*fac[2*t-2*i]%mod*inv[t-i]%mod*A[i]%mod)%mod;
	}ans=1ll*ans*fac[t]%mod*KSM(KSM(2,t),mod-2)%mod;
	std::sort(a+1,a+n+1);
	std::sort(b+1,b+n+1);
	for(int i=1;i<=n;i++)if(map[a[i]]!=b[i])ans=0;
	printf("%d\n",ans);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3940kb

input:

3
1 2 3
3 4 5

output:

1

result:

ok answer is '1'

Test #2:

score: 0
Accepted
time: 1ms
memory: 4056kb

input:

3
1 2 3
7 8 9

output:

42

result:

ok answer is '42'

Test #3:

score: 0
Accepted
time: 1ms
memory: 3884kb

input:

3
1 4 7
3 6 9

output:

6

result:

ok answer is '6'

Test #4:

score: 0
Accepted
time: 0ms
memory: 3868kb

input:

1
1
3

output:

0

result:

ok answer is '0'

Test #5:

score: 0
Accepted
time: 0ms
memory: 3932kb

input:

1
1
1

output:

1

result:

ok answer is '1'

Test #6:

score: 0
Accepted
time: 0ms
memory: 3868kb

input:

2
1 4
4 7

output:

1

result:

ok answer is '1'

Test #7:

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

input:

10
10 9 8 7 6 1 2 3 4 5
11 12 13 114 115 120 129 128 127 126

output:

0

result:

ok answer is '0'

Test #8:

score: 0
Accepted
time: 0ms
memory: 3652kb

input:

30
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 131

output:

0

result:

ok answer is '0'

Test #9:

score: 0
Accepted
time: 209ms
memory: 8736kb

input:

30
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130

output:

936606510

result:

ok answer is '936606510'

Test #10:

score: 0
Accepted
time: 0ms
memory: 3652kb

input:

30
16 21 33 44 51 60 71 81 91 100 110 122 131 144 155 160 162 171 172 174 177 179 187 188 189 191 192 193 194 199
110 111 112 113 114 115 116 117 118 119 120 122 131 144 155 160 162 171 172 174 177 179 187 188 189 191 192 193 194 199

output:

0

result:

ok answer is '0'

Test #11:

score: 0
Accepted
time: 40ms
memory: 4108kb

input:

30
16 21 33 44 51 60 71 81 91 100 110 122 131 144 155 160 162 171 172 174 177 179 187 188 189 191 192 193 194 199
110 111 112 113 114 115 116 117 118 119 120 122 131 144 155 160 162 171 172 174 177 179 187 188 189 191 192 193 194 200

output:

836228983

result:

ok answer is '836228983'

Test #12:

score: 0
Accepted
time: 33ms
memory: 6120kb

input:

10
10 9 8 7 6 5 4 3 2 1
110 109 108 107 106 105 104 103 102 101

output:

422463757

result:

ok answer is '422463757'

Test #13:

score: 0
Accepted
time: 369ms
memory: 7000kb

input:

30
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200

output:

575061951

result:

ok answer is '575061951'