QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#104526 | #1262. Justice For Everyone | lmeowdn | AC ✓ | 1128ms | 6060kb | C++14 | 2.5kb | 2023-05-10 22:44:48 | 2023-05-10 22:44:49 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define fi first
#define se second
#define eb emplace_back
#define popc __builtin_popcount
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pii> vp;
typedef unsigned long long ull;
typedef long double ld;
int read() {
int x=0,w=1; char c=getchar();
while(!isdigit(c)) {if(c=='-') w=-1; c=getchar();}
while(isdigit(c)) {x=x*10+(c-'0'); c=getchar();}
return x*w;
}
const int N=6009,mod=998244353;
int n,m,a[N],b[N],fac[N],ifac[N],inv[N],p[N][N],y[N],f[N],g[N],h[N],r[N],ans;
pii u[N],v[N];
int ksm(int x,int y,int r=1) {
for(;y;y>>=1,x=x*x%mod) if(y&1) r=r*x%mod;
return r;
}
void init() {
fac[0]=1; rep(i,1,6000) fac[i]=fac[i-1]*i%mod;
ifac[6000]=ksm(fac[6000],mod-2);
per(i,5999,0) ifac[i]=ifac[i+1]*(i+1)%mod;
inv[0]=0; rep(i,1,6000) inv[i]=fac[i-1]*ifac[i]%mod;
}
int F(int d,int z) {
int pw=1,res=0; if(d<0) return 0;
rep(i,0,d/2) {
int w=ifac[i]*ifac[d-2*i]%mod*pw%mod;
if(i&1) res=(res+mod-w)%mod;
else res=(res+w)%mod;
pw=pw*z%mod;
} return res;
}
int det(int res=1) {
rep(i,1,n) {
int r=i; while(r<=n&&!p[r][i]) ++r;
if(!p[r][i]) return 0;
if(r!=i) res=mod-res, swap(p[r],p[i]);
rep(j,i+1,n) {
int c=p[j][i]*ksm(p[i][i],mod-2)%mod;
rep(k,i,n) p[j][k]=(p[j][k]+mod-p[i][k]*c%mod)%mod;
} res=res*p[i][i]%mod;
}
return res;
}
void itp() {
rep(i,0,m) {
r[i]=y[i];
rep(j,0,i-1) r[i]=r[i]*inv[i-j]%mod;
rep(j,i+1,m) r[i]=mod-r[i]*inv[j-i]%mod;
}
g[0]=1;
rep(i,0,m) {
per(j,i+1,1) g[j]=(g[j-1]+g[j]*(mod-i))%mod;
g[0]=g[0]*(mod-i)%mod;
}
rep(i,0,m) {
if(i) {
h[0]=g[0]*inv[i]%mod;
rep(j,1,m) h[j]=(h[j-1]-g[j]+mod)*inv[i]%mod;
} else rep(j,0,m) h[j]=g[j+1];
rep(j,0,m) f[j]=(f[j]+h[j]*r[i])%mod;
}
}
signed main() {
n=read(); init();
rep(i,1,n) a[i]=read(), u[i]=pii(a[i],i), m-=a[i];
rep(i,1,n) b[i]=read(), v[i]=pii(b[i],i), m+=b[i];
if(m&1) return puts("0"), 0; m/=2;
sort(u+1,u+n+1); sort(v+1,v+n+1);
rep(i,1,n) if(u[i].se!=v[i].se) return puts("0"), 0;
rep(i,1,n) a[i]=u[i].fi, b[i]=v[i].fi;
rep(z,0,m) {
rep(i,1,n) rep(j,1,n) p[i][j]=F(b[j]-a[i],z);
y[z]=det();
}
itp();
rep(i,0,m) ans=(ans+f[i]*fac[2*m-2*i]%mod*ifac[m-i]%mod*fac[m])%mod;
rep(i,1,m) ans=ans*((mod+1)/2)%mod;
printf("%lld\n",ans);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 3648kb
input:
3 1 2 3 3 4 5
output:
1
result:
ok answer is '1'
Test #2:
score: 0
Accepted
time: 2ms
memory: 3860kb
input:
3 1 2 3 7 8 9
output:
42
result:
ok answer is '42'
Test #3:
score: 0
Accepted
time: 0ms
memory: 3680kb
input:
3 1 4 7 3 6 9
output:
6
result:
ok answer is '6'
Test #4:
score: 0
Accepted
time: 3ms
memory: 5644kb
input:
1 1 3
output:
0
result:
ok answer is '0'
Test #5:
score: 0
Accepted
time: 1ms
memory: 5800kb
input:
1 1 1
output:
1
result:
ok answer is '1'
Test #6:
score: 0
Accepted
time: 2ms
memory: 5844kb
input:
2 1 4 4 7
output:
1
result:
ok answer is '1'
Test #7:
score: 0
Accepted
time: 2ms
memory: 3492kb
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: 3500kb
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: 433ms
memory: 6060kb
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: 2ms
memory: 3604kb
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: 50ms
memory: 5844kb
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: 16ms
memory: 5936kb
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: 1128ms
memory: 5896kb
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'