QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#61397#1262. Justice For EveryoneMoQzAC ✓2393ms4624kbC++2.1kb2022-11-12 19:40:262022-11-12 19:40:27

Judging History

This is the latest submission verdict.

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-11-12 19:40:27]
  • Judged
  • Verdict: AC
  • Time: 2393ms
  • Memory: 4624kb
  • [2022-11-12 19:40:26]
  • Submitted

answer

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define fo(i,a,b) for(int i=a;i<=b;++i)
#define fod(i,a,b) for(int i=a;i>=b;--i)
#define LL long long
#define p 998244353
int ksm(int x,int y){
	if(!x)return 1;
	if(x==1)return y;
	LL u=ksm(x/2,y);
	u=u*u%p;
	if(x&1)u=u*y%p;
	return u;
}
int n,a[33],b[33];
int c[33][33];
int det(){
	int sig=1;
	fo(i,1,n){
		fo(j,i+1,n){
			while(c[i][i]){
				int div=c[j][i]/c[i][i];
				fo(k,i,n){
					c[j][k]=(c[j][k]-(LL)c[i][k]*div%p+p)%p;
				}
				swap(c[i],c[j]);
				sig*=(-1);
			}
			swap(c[i],c[j]);
			sig*=(-1);
		}
	}
	LL u=1;
	fo(i,1,n)u=u*c[i][i]%p;
	return (u*sig%p+p)%p;
}
int jc[100011],ny[100011];
void init(){
	jc[0]=ny[0]=1;
	fo(i,1,100000){
		jc[i]=(LL)jc[i-1]*i%p;
		ny[i]=ksm(p-2,jc[i]);
	}
}
int Y[60011];
int cal(int x){
	fo(i,1,n){
		fo(j,1,n){
			LL u=0,v=1;
			fo(k,0,(b[j]-a[i])/2){
				u+=v*ny[k]%p*ny[b[j]-a[i]-2*k]%p;
				v=v*x%p;
			}
			c[i][j]=u%p;
		}
	}
	return det();
}
int Ny(int x){
	return (LL)ny[x]*jc[x-1]%p;
}
int A[60011],B[60011];
int Map[211][211];
int main(){
	scanf("%d",&n);
	fo(i,1,n)scanf("%d",&a[i]);
	fo(i,1,n)scanf("%d",&b[i]),Map[a[i]][b[i]]=1;
	sort(a+1,a+n+1);
	sort(b+1,b+n+1);
	init();
	int len=1;
	fo(i,1,n)len+=b[i];
	fo(i,1,len){
		Y[i]=cal(i);
		fo(j,1,i-1){
			Y[i]=(LL)Y[i]*Ny(i-j)%p; 
		}
		fo(j,i+1,len){
			Y[i]=(LL)Y[i]*(p-Ny(j-i))%p;
		}
	}
	LL ans=0;
	B[0]=1;
	fo(i,1,len){
		fod(j,i-1,0){
			if(j){
				A[j]=((LL)B[j]*Y[i]%p+(LL)A[j]*(p-i)%p+A[j-1])%p;
			}
			else A[j]=((LL)B[j]*Y[i]%p+(LL)A[j]*(p-i)%p)%p;
		}
		fod(j,i,0){
			if(j){
				B[j]=((LL)B[j]*(p-i)%p+B[j-1])%p;
			}
			else B[j]=(LL)B[j]*(p-i)%p;
		}
	}
	int t=0;
	fo(i,1,n){
		t+=b[i]-a[i];
		if(!Map[a[i]][b[i]]){
			t=1;break;
		}
	}
	if(t&1){
		printf("0");
		return 0;
	}
	t/=2;
	fo(i,0,t){
		if(i&1)ans-=(LL)jc[2*(t-i)]*A[i]%p*ny[t-i]%p;
		else ans+=(LL)jc[2*(t-i)]*A[i]%p*ny[t-i]%p;
	}
	ans=ans%p*jc[t]%p*ksm(t,ksm(p-2,2))%p;
	if(ans<0)ans+=p;
	printf("%lld",ans);
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 25ms
memory: 4452kb

input:

3
1 2 3
3 4 5

output:

1

result:

ok answer is '1'

Test #2:

score: 0
Accepted
time: 25ms
memory: 4472kb

input:

3
1 2 3
7 8 9

output:

42

result:

ok answer is '42'

Test #3:

score: 0
Accepted
time: 21ms
memory: 4412kb

input:

3
1 4 7
3 6 9

output:

6

result:

ok answer is '6'

Test #4:

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

input:

1
1
3

output:

0

result:

ok answer is '0'

Test #5:

score: 0
Accepted
time: 25ms
memory: 4560kb

input:

1
1
1

output:

1

result:

ok answer is '1'

Test #6:

score: 0
Accepted
time: 21ms
memory: 4244kb

input:

2
1 4
4 7

output:

1

result:

ok answer is '1'

Test #7:

score: 0
Accepted
time: 49ms
memory: 4292kb

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: 970ms
memory: 4344kb

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: 984ms
memory: 4624kb

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: 674ms
memory: 4416kb

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: 676ms
memory: 4548kb

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: 65ms
memory: 4472kb

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: 2393ms
memory: 4500kb

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'