QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#428580 | #1262. Justice For Everyone | LiWenX | AC ✓ | 6796ms | 4392kb | C++20 | 3.2kb | 2024-06-01 20:29:58 | 2024-06-01 20:29:58 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
#define mod 998244353
using namespace std;
int quickpow(int a,int b){
if(a<0) a+=mod;
int ret=1;
while(b){
if(b&1) ret=ret*a%mod;
a=a*a%mod;
b>>=1;
}return ret;
}
int fac[10001],inv[10001];
void init(int n=10000){
fac[0]=1;for(int i=1;i<=n;i++) fac[i]=fac[i-1]*i%mod;
inv[n]=quickpow(fac[n],mod-2);
for(int i=n-1;~i;i--) inv[i]=inv[i+1]*(i+1)%mod;
}
int C(int n,int m){
return fac[n]*inv[m]%mod*inv[n-m]%mod;
}
int y[10001];
#define poly vector<int>
poly operator +(poly A,poly B){
poly ret;int len=max(A.size(),B.size());
A.resize(len),B.resize(len),ret.resize(len);
for(int i=0;i<len;i++){
ret[i]=A[i]+B[i];
if(ret[i]>=mod) ret[i]-=mod;
}
return ret;
}
poly operator *(poly A,int b){
for(int i=0;i<(int)A.size();i++) A[i]=A[i]*b%mod;
return A;
}
poly add(poly A,int b){
if(b<0) b+=mod;
A.resize(A.size()+1);
for(int i=(int)A.size()-1;~i;i--){
if(i) A[i]=A[i-1]+A[i]*b%mod;
else A[i]=A[i]*b%mod;
if(A[i]>=mod) A[i]-=mod;
}
return A;
}
poly del(poly A,int b){
if(b<0) b+=mod;
poly ret;ret.resize(A.size()-1);
for(int i=(int)ret.size()-1;~i;i--){
ret[i]=A[i+1];
A[i]-=ret[i]*b%mod;
if(A[i]<0) A[i]+=mod;
}
return ret;
}
poly get(int n){
poly ans;
poly V={1};for(int i=0;i<=n;i++) V=add(V,-i);
for(int i=0;i<=n;i++){
int S=y[i];
for(int j=0;j<=n;j++){
if(i==j) continue;
S=S*quickpow(i-j,mod-2)%mod;
}
if(S<0) S+=mod;
ans=ans+(del(V,-i)*S);
}
return ans;
}
int po(poly A,int x){
int ret=0,X=1;
for(int i=0;i<(int)A.size();i++){
ret+=A[i]*X%mod;
if(ret>=mod) ret-=mod;
X=X*x%mod;
}return ret;
}
int n;
int c[31][31];
int det(){
int ret=1;
for(int i=1;i<=n;i++){
if(!c[i][i]){
for(int j=i+1;j<=n;j++){
if(c[j][i]){
swap(c[i],c[j]);
ret=mod-ret;
}
}
}
for(int j=i+1;j<=n;j++){
int t=c[j][i]*quickpow(c[i][i],mod-2)%mod;
for(int k=i;k<=n;k++){
c[j][k]-=c[i][k]*t%mod;
if(c[j][k]<0) c[j][k]+=mod;
}
}
}
for(int i=1;i<=n;i++) ret=ret*c[i][i]%mod;
return ret;
}
int a[10001],b[10001];
poly G[201];
mt19937 rd(time(NULL));
signed main(){
init();
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
int t=0;
for(int i=1;i<=n;i++){
cin>>b[i];
if(b[i]<a[i]){
cout<<0<<'\n';
return 0;
}
t+=b[i]-a[i];
}
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
if(a[i]<=a[j]&&b[j]<=b[i]){
cout<<0;
return 0;
}
}
}
if(t&1){
cout<<0<<'\n';
return 0;
}
t>>=1;
int maxn=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
maxn=max(maxn,b[j]-a[i]);
}
}
for(int d=0;d<=maxn;d++){
for(int i=0;i*2<=d;i++){
G[d].push_back(inv[i]*inv[d-2*i]%mod);
}
}
for(int x=0;x<=200*n;x++){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(b[j]-a[i]<0){
c[i][j]=0;continue;
}
c[i][j]=po(G[b[j]-a[i]],x);
}
}
int Det=det();
y[x]=Det;
}
poly A=get(200*n);
int ans=0;
for(int i=0;i<=t;i++){
if(i&1){
ans-=C(t,i)*fac[i]%mod*fac[2*t-2*i]%mod*A[i]%mod;
}
else{
ans+=C(t,i)*fac[i]%mod*fac[2*t-2*i]%mod*A[i]%mod;
}
}
ans=(ans%mod+mod)%mod;
ans=ans*quickpow(quickpow(2,mod-2),t)%mod;
cout<<ans;
}
/*
3
1 2 3
3 4 5
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 44ms
memory: 3932kb
input:
3 1 2 3 3 4 5
output:
1
result:
ok answer is '1'
Test #2:
score: 0
Accepted
time: 40ms
memory: 3872kb
input:
3 1 2 3 7 8 9
output:
42
result:
ok answer is '42'
Test #3:
score: 0
Accepted
time: 44ms
memory: 4184kb
input:
3 1 4 7 3 6 9
output:
6
result:
ok answer is '6'
Test #4:
score: 0
Accepted
time: 3ms
memory: 3860kb
input:
1 1 3
output:
0
result:
ok answer is '0'
Test #5:
score: 0
Accepted
time: 5ms
memory: 3908kb
input:
1 1 1
output:
1
result:
ok answer is '1'
Test #6:
score: 0
Accepted
time: 20ms
memory: 3996kb
input:
2 1 4 4 7
output:
1
result:
ok answer is '1'
Test #7:
score: 0
Accepted
time: 0ms
memory: 3944kb
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: 1ms
memory: 4100kb
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: 5953ms
memory: 4392kb
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: 1ms
memory: 3808kb
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: 5197ms
memory: 4124kb
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: 538ms
memory: 4064kb
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: 6796ms
memory: 4200kb
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'