QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#369925 | #1262. Justice For Everyone | schrodingerstom | AC ✓ | 179ms | 53372kb | C++14 | 10.7kb | 2024-03-28 19:30:38 | 2024-03-28 19:30:40 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
bool memBeg;
template<typename T> void chkmin(T &x,T y) {if(x>y) x=y;}
template<typename T> void chkmax(T &x,T y) {if(x<y) x=y;}
constexpr int mod=998244353;
void inc(int &x,int y) {x+=y; x-=(x>=mod)*mod;}
void dec(int &x,int y) {x-=y; x+=(x<0)*mod;}
constexpr int add(int x,int y) {return (x+y>=mod)?x+y-mod:x+y;}
constexpr int sub(int x,int y) {return (x<y)?x-y+mod:x-y;}
constexpr int quick_pow(int x,ll times) {
int ret=1;
for(;times;times>>=1,x=1ll*x*x%mod) {
if(times&1) ret=1ll*ret*x%mod;
}
return ret;
}
namespace polynomial {
constexpr int G=3;
constexpr int L=1<<22;
constexpr int maxlen=L+5;
constexpr int brute_force_limit=16;
int pwg[maxlen],up[maxlen],inv[maxlen],rev[maxlen];
void prepare() {
pwg[1]=1;
for(int i=2;i<=L;i<<=1) {
pwg[i]=1; int rt=quick_pow(G,(mod-1)/i);
for(int j=i-1;j>(i>>1);j--) pwg[j]=1ll*pwg[j+1]*rt%mod;
}
for(int i=1;i<L;i<<=1) {
for(int j=i;j<(i<<1);j++) up[j]=i<<1;
}
inv[1]=1;
for(int i=2;i<=L;i++) inv[i]=mod-1ll*(mod/i)*inv[mod%i]%mod;
}
void init(int _siz) {
static int siz;
if(siz==_siz) return;
assert(__builtin_popcount(_siz)==1);
siz=_siz;
for(int i=0;i<siz;i++) rev[i]=rev[i>>1]>>1|((i&1)*(siz>>1));
}
void ntt(vector<int> &coef,int tp) {
int len=coef.size();
init(len);
for(int i=0;i<len;i++) {
if(i<rev[i]) swap(coef[i],coef[rev[i]]);
}
for(int i=2;i<=len;i<<=1) {
for(int j=0;j<len;j+=i) {
for(int k=j;k<(i>>1|j);k++) {
int u=coef[k],v=1ll*pwg[i-k+j]*coef[i>>1|k]%mod;
coef[k]=add(u,v); coef[i>>1|k]=sub(u,v);
}
}
}
if(tp==-1) {
reverse(coef.begin()+1,coef.end());
int iv=inv[len];
for(int &v:coef) v=1ll*v*iv%mod;
}
}
struct poly:vector<int> {
using vector<int>::vector;
poly integral() const;
poly derivation() const;
poly mod_xn(int n) const;
poly mul_xn(int n) const;
poly div_xn(int n) const;
poly inversion(int n) const;
poly logarithm(int n) const;
poly exponential(int n) const;
poly power(int n,ll K) const;
poly sqrt(int n) const;
};
poly operator*(const poly &_lhs,const poly &_rhs) {
if((int)_lhs.size()<brute_force_limit||(int)_rhs.size()<brute_force_limit) {
poly ret(_lhs.size()+_rhs.size()-1);
for(int i=0;i<(int)_lhs.size();i++) {
for(int j=0;j<(int)_rhs.size();j++) {
ret[i+j]=(ret[i+j]+1ll*_lhs[i]*_rhs[j])%mod;
}
}
return ret;
}
int len=up[_lhs.size()+_rhs.size()-1];
poly lhs(_lhs),rhs(_rhs);
lhs.resize(len); rhs.resize(len);
ntt(lhs,1); ntt(rhs,1);
for(int i=0;i<len;i++) lhs[i]=1ll*lhs[i]*rhs[i]%mod;
ntt(lhs,-1); lhs.resize(_lhs.size()+_rhs.size()-1); return lhs;
}
poly& operator*=(poly &lhs,const poly &_rhs) {
if((int)lhs.size()<brute_force_limit||(int)_rhs.size()<brute_force_limit) {
int psiz=lhs.size();
lhs.resize(lhs.size()+_rhs.size()-1);
for(int i=psiz-1;i>=0;i--) {
int tmp=lhs[i]; lhs[i]=0;
for(int j=0;j<(int)_rhs.size();j++) {
lhs[i+j]=(lhs[i+j]+1ll*tmp*_rhs[j])%mod;
}
}
return lhs;
}
size_t tsiz=lhs.size()+_rhs.size()-1;
int len=up[lhs.size()+_rhs.size()-1];
lhs.resize(len); poly rhs(_rhs);
rhs.resize(len);
ntt(lhs,1); ntt(rhs,1);
for(int i=0;i<len;i++) lhs[i]=1ll*lhs[i]*rhs[i]%mod;
ntt(lhs,-1); lhs.resize(tsiz); return lhs;
}
poly operator+(const poly &lhs,const poly &rhs) {
poly ret(lhs); ret.resize(max(lhs.size(),rhs.size()));
for(int i=0;i<(int)rhs.size();i++) inc(ret[i],rhs[i]);
return ret;
}
poly& operator+=(poly &lhs,const poly &rhs) {
lhs.resize(max(lhs.size(),rhs.size()));
for(int i=0;i<(int)rhs.size();i++) inc(lhs[i],rhs[i]);
return lhs;
}
poly operator-(const poly &lhs,const poly &rhs) {
poly ret(lhs); ret.resize(max(lhs.size(),rhs.size()));
for(int i=0;i<(int)rhs.size();i++) dec(ret[i],rhs[i]);
return ret;
}
poly& operator-=(poly &lhs,const poly &rhs) {
lhs.resize(max(lhs.size(),rhs.size()));
for(int i=0;i<(int)rhs.size();i++) dec(lhs[i],rhs[i]);
return lhs;
}
poly poly::integral() const {
poly ret(*this); ret.emplace_back(0);
for(int i=(int)ret.size()-2;i>=0;i--) {
ret[i+1]=1ll*ret[i]*inv[i+1]%mod;
}
ret[0]=0; return ret;
}
poly poly::derivation() const {
poly ret(*this);
for(int i=1;i<(int)ret.size();i++) {
ret[i-1]=1ll*i*ret[i]%mod;
}
ret.pop_back(); return ret;
}
poly poly::mod_xn(int n) const {
if((int)this->size()<=n) return *this;
return poly(this->begin(),this->begin()+n);
}
poly poly::mul_xn(int n) const {
if(this->empty()) return *this;
poly ret(*this);
ret.insert(ret.begin(),n,0);
return ret;
}
poly poly::div_xn(int n) const {
if((int)this->size()<n) return poly(1,0);
return poly(this->begin()+n,this->end());
}
poly poly::inversion(int n) const {
assert(!this->empty());
assert((*this)[0]);
poly ret(1,quick_pow((*this)[0],mod-2));
for(int i=1;i<n;i<<=1) {
ret=(ret*(poly(1,2)-(ret*(*this).mod_xn(i<<1)).mod_xn(i<<1))).mod_xn(i<<1);
}
return ret.mod_xn(n);
}
poly poly::logarithm(int n) const {
assert((*this)[0]==1);
return (this->derivation()*this->inversion(n)).integral().mod_xn(n);
}
poly poly::exponential(int n) const {
assert((*this)[0]==0);
poly ret(1,1);
for(int i=1;i<n;i<<=1) {
ret=(ret*(poly(1,1)-ret.logarithm(i<<1)+(*this).mod_xn(i<<1))).mod_xn(i<<1);
}
return ret.mod_xn(n);
}
poly poly::power(int n,ll K) const {
if((*this)[0]==0) {
int num=0;
for(int i=0;i<(int)this->size();i++) {
if((*this)[i]) {
num=i; break;
}
}
if(!num) return poly(1,0);
return (this->div_xn(num)).power(n,K).mul_xn(min<ll>(n,1ll*num*min<ll>(n,K))).mod_xn(n);
}
if((*this)[0]!=1) {
int x=(*this)[0],ix=quick_pow(x,mod-2);
return ((((*this)*poly(1,ix)).power(n,K))*poly(1,quick_pow(x,K))).mod_xn(n);
}
assert((*this)[0]==1);
poly ret=this->logarithm(n); K%=mod;
for(int i=0;i<n;i++) ret[i]=1ll*ret[i]*K%mod;
return ret.exponential(n);
}
poly poly::sqrt(int n) const {
return power(n,inv[2]);
}
} // polynomial
using polynomial::poly;
constexpr int V=200;
constexpr int maxn=35;
constexpr int maxV=V+5;
constexpr int maxT=maxV*maxn;
int n,a[maxn],b[maxn],oa[maxn],val[maxT],fac[maxT],ifac[maxT],mat[maxn][maxn];
int det() {
// int chk=0;
// chk=(chk+1ll*mat[1][1]*mat[2][2]%mod*mat[3][3])%mod;
// dec(chk,1ll*mat[1][1]*mat[2][3]%mod*mat[3][2]%mod);
// dec(chk,1ll*mat[1][2]*mat[2][1]%mod*mat[3][3]%mod);
// chk=(chk+1ll*mat[1][2]*mat[2][3]%mod*mat[3][1])%mod;
// chk=(chk+1ll*mat[1][3]*mat[2][1]%mod*mat[3][2])%mod;
// dec(chk,1ll*mat[1][3]*mat[2][2]%mod*mat[3][1]%mod);
int ret=1;
for(int i=1;i<=n;i++) {
if(!mat[i][i]) {
int key=-1;
for(int j=i+1;j<=n;j++) {
if(mat[j][i]) {
key=j; break;
}
}
if(key==-1) return 0;
for(int j=i;j<=n;j++) swap(mat[i][j],mat[key][j]);
ret=sub(0,ret);
}
int iv=mod-quick_pow(mat[i][i],mod-2);
for(int j=i+1;j<=n;j++) {
int coef=1ll*mat[j][i]*iv%mod;
for(int k=i;k<=n;k++) mat[j][k]=(mat[j][k]+1ll*mat[i][k]*coef)%mod;
}
}
for(int i=1;i<=n;i++) ret=1ll*ret*mat[i][i]%mod;
// assert(chk==ret);
return ret;
}
int cache[maxV];
int P(int a,int b,int x) {
if(a>b) return 0;
int &ret=cache[b-a];
if(ret!=-1) return ret;
ret=0;
for(int t=0,run=1;2*t<=b-a;t++) {
if(t&1) dec(ret,1ll*ifac[t]*ifac[b-a-2*t]%mod*run%mod);
else ret=(ret+1ll*ifac[t]*ifac[b-a-2*t]%mod*run)%mod;
run=1ll*run*x%mod;
}
// printf("b - a = %d, x = %d, ret = %d\n",b-a,x,ret);
return ret;
}
int solve(int x) {
memset(cache,-1,sizeof(cache));
for(int i=1;i<=n;i++) {
for(int j=1;j<=n;j++) {
mat[i][j]=P(a[i],b[j],x);
// printf("mat[i = %d][j = %d] = %d\n",i,j,mat[i][j]);
}
}
return det();
}
int coef[maxT];
void lagrange(int t) {
static int num[maxT],tnum[maxT];
num[0]=1;
for(int i=1;i<=t+1;i++) {
for(int j=i-1;j>=0;j--) {
dec(num[j+1],num[j]);
num[j]=1ll*num[j]*i%mod;
}
}
for(int i=1;i<=t+1;i++) {
memcpy(tnum,num,sizeof(int)*(t+2));
int iv=quick_pow(i,mod-2);
tnum[0]=1ll*tnum[0]*iv%mod;
for(int j=1;j<=t+1;j++) {
inc(tnum[j],tnum[j-1]);
tnum[j]=1ll*tnum[j]*iv%mod;
}
// for(int j=0;j<=t;j++) printf("%d ",tnum[j]); puts("");
assert(!tnum[t+1]);
int cst=1ll*ifac[i-1]*ifac[t+1-i]%mod*val[i]%mod;
if((i-1)&1) cst=sub(0,cst);
for(int j=0;j<=t;j++) {
coef[j]=(coef[j]+1ll*cst*tnum[j])%mod;
}
}
}
bool memEn;
void fl() {
freopen(".in","r",stdin);
freopen(".out","w",stdout);
}
int main() {
fprintf(stderr,"%.24lf\n",fabs(&memEn-&memBeg)/1024.0/1024.0);
// fl();
polynomial::prepare();
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
for(int i=1;i<=n;i++) scanf("%d",&b[i]);
for(int i=1;i<=n;i++) if(a[i]>b[i]) return puts("0"),0;
int sa=accumulate(a+1,a+n+1,0),sb=accumulate(b+1,b+n+1,0);
if((sa-sb)&1) return puts("0"),0;
iota(oa+1,oa+n+1,1); sort(oa+1,oa+n+1,[&](int u,int v){return a[u]<a[v];});
for(int i=1;i<n;i++) if(b[oa[i]]>=b[oa[i+1]]) return puts("0"),0;
int t=(sb-sa)/2,T=n*V;
fac[0]=1;
for(int i=1;i<=T;i++) fac[i]=1ll*fac[i-1]*i%mod;
ifac[T]=quick_pow(fac[T],mod-2);
for(int i=T-1;i>=0;i--) ifac[i]=1ll*ifac[i+1]*(i+1)%mod;
for(int i=1;i<=t+1;i++) val[i]=solve(i);
// for(int i=1;i<=t+1;i++) printf("%d ",val[i]); puts("");
lagrange(t); int ret=0;
// for(int i=0;i<=t;i++) printf("%d ",coef[i]); puts("");
// for(int i=1;i<=t+1;i++) {
// int chk=0;
// for(int j=0,run=1;j<=t;j++) {
// chk=(chk+1ll*coef[j]*run)%mod;
// run=1ll*run*i%mod;
// }
// assert(chk==val[i]);
// }
for(int i=0;i<=t;i++) ret=(ret+1ll*fac[2*(t-i)]*ifac[t-i]%mod*coef[i])%mod;
ret=1ll*ret*fac[t]%mod*quick_pow(2,mod-1-t)%mod;
printf("%d\n",ret);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 47ms
memory: 53016kb
input:
3 1 2 3 3 4 5
output:
1
result:
ok answer is '1'
Test #2:
score: 0
Accepted
time: 37ms
memory: 53120kb
input:
3 1 2 3 7 8 9
output:
42
result:
ok answer is '42'
Test #3:
score: 0
Accepted
time: 36ms
memory: 53284kb
input:
3 1 4 7 3 6 9
output:
6
result:
ok answer is '6'
Test #4:
score: 0
Accepted
time: 40ms
memory: 53052kb
input:
1 1 3
output:
0
result:
ok answer is '0'
Test #5:
score: 0
Accepted
time: 34ms
memory: 53296kb
input:
1 1 1
output:
1
result:
ok answer is '1'
Test #6:
score: 0
Accepted
time: 41ms
memory: 53276kb
input:
2 1 4 4 7
output:
1
result:
ok answer is '1'
Test #7:
score: 0
Accepted
time: 44ms
memory: 53056kb
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: 37ms
memory: 53264kb
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: 97ms
memory: 53356kb
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: 38ms
memory: 52988kb
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: 48ms
memory: 53296kb
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: 38ms
memory: 53304kb
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: 179ms
memory: 53372kb
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'