QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#376656 | #1262. Justice For Everyone | waretle | AC ✓ | 1089ms | 6680kb | C++14 | 9.6kb | 2024-04-04 14:40:05 | 2024-04-04 14:40:06 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int mod=998244353;
int pw(int a,ll b,int c=1)
{
for(;b;b>>=1,a=1ll*a*a%mod)
if(b&1)c=1ll*c*a%mod;
return c;
}
void add(int&a,int b){a+=b;if(a>=mod)a-=mod;}
namespace Poly
{
const int N=1<<18,mod=998244353,g=3;
int pwg[N],inv[N];
int getsz(int n){return 1<<__lg(n)+1;}
void init()
{
for(int h=2;h<=N;h<<=1)
{
int t=pw(3,(mod-1)/h);pwg[h>>1]=1;
for(int i=(h>>1)+1;i<h;++i)pwg[i]=1ll*pwg[i-1]*t%mod;
}
inv[0]=inv[1]=1;
for(int i=2;i<N;++i)inv[i]=1ll*inv[mod%i]*(mod-mod/i)%mod;
}
void dft(vector<int>&f,int n)
{
f.resize(n);
for(int h=n;h>1;h>>=1)
for(int i=0,p=h>>1;i<n;i+=h)
for(int j=i;j<i+p;++j)
{
int u=f[j],v=f[j+p];
add(f[j],v),f[j+p]=1ll*pwg[j-i+p]*(u-v+mod)%mod;
}
}
void idft(vector<int>&f,int n)
{
f.resize(n);
for(int h=2;h<=n;h<<=1)
for(int i=0,p=h>>1;i<n;i+=h)
for(int j=i;j<i+p;++j)
{
int t=1ll*pwg[j-i+p]*f[j+p]%mod;
f[j+p]=f[j],add(f[j+p],mod-t),add(f[j],t);
}
reverse(f.begin()+1,f.begin()+n);
for(int i=0,k=pw(n,mod-2);i<n;++i)f[i]=1ll*f[i]*k%mod;
}
struct poly:vector<int>
{
using vector<int>::vector;
void modxn(int n){if(n<size())resize(n);}
void drv()
{
for(int i=0;i+1<size();++i)(*this)[i]=1ll*(*this)[i+1]*(i+1)%mod;
pop_back();
}
void itg()
{
push_back(0);
for(int i=size()-1;i>0;--i)(*this)[i]=1ll*(*this)[i-1]*inv[i]%mod;
(*this)[0]=0;
}
int operator()(int x)
{
int ret=0;
for(int i=size()-1;~i;--i)ret=(1ll*ret*x+(*this)[i])%mod;
return ret;
}
};
poly&operator+=(poly&a,const poly&b)
{
if(a.size()<b.size())a.resize(b.size());
for(int i=0;i<b.size();++i)add(a[i],b[i]);
return a;
}
poly&operator-=(poly&a,const poly&b)
{
if(a.size()<b.size())a.resize(b.size());
for(int i=0;i<b.size();++i)add(a[i],mod-b[i]);
return a;
}
poly operator+(const poly&a,const poly&b)
{
poly c=a;c+=b;return c;
}
poly operator-(const poly&a,const poly&b)
{
poly c=a;c-=b;return c;
}
poly operator*(const poly&a,const poly&b)
{
if(!a.size()||!b.size())return {0};
poly A=a,B=b,C;
int len=getsz(a.size()+b.size()-1);
A.resize(len),B.resize(len),C.resize(len);
dft(A,len),dft(B,len);
for(int i=0;i<len;++i)C[i]=1ll*A[i]*B[i]%mod;
idft(C,len);
return C;
}
poly operator*(int a,const poly&b)
{
a=(a%mod+mod)%mod;poly B=b;
for(int i=0;i<B.size();++i)B[i]=1ll*B[i]*a%mod;
return B;
}
poly operator*(const poly&b,int a)
{
a=(a%mod+mod)%mod;poly B=b;
for(int i=0;i<B.size();++i)B[i]=1ll*B[i]*a%mod;
return B;
}
poly drv(const poly&a)
{
poly A=a;A.drv();return A;
}
poly itg(const poly&a)
{
poly A=a;A.itg();return A;
}
poly modxn(const poly&a,int n)
{
poly A;A.resize(n);for(int i=0;i<n&&i<a.size();++i)A[i]=a[i];return A;
}
poly ivs(const poly&A,int n)
{
assert(A[0]);
int cur=1;
poly b;b.resize(1);b[0]=pw(A[0],mod-2);
while(cur<n)
{
cur<<=1;
dft(b,cur*2);
poly a=modxn(A,cur);
dft(a,cur*2);
for(int i=0;i<cur*2;++i)
b[i]=(2*b[i]+1ll*(mod-a[i])*b[i]%mod*b[i])%mod;
idft(b,cur*2);
b.modxn(cur);
}
b.modxn(n);return b;
}
poly ln(const poly&a,int n)
{
assert(a[0]==1);
poly ret=a;ret.drv();
ret=ret*ivs(a,n-1);
ret.modxn(n-1),ret.itg();
return ret;
}
poly exp(const poly&a,int n)
{
assert(a[0]==0);
int cur=1;
poly b;b.resize(1);b[0]=1;
while(cur<n)
{
cur<<=1;
b=b*(poly{1}-ln(b,cur)+modxn(a,cur));
b.modxn(cur);
}
return modxn(b,n);
}
poly pow(const poly&a,int n,ll tim)
{
if(a[0])
{
poly b=a*pw(a[0],mod-2);
b=exp(ln(b,n)*(tim%mod),n);
return b*pw(a[0],tim%(mod-1));
}
else assert(0);
}
poly pow_a0_may_be_0(const poly&a,int n,ll tim,bool big=0)
{
if(a[0])return pow(a,n,tim);
if(big)return poly(n,0);
int pos=0;
while(pos<a.size()&&!a[pos])++pos;
if(pos==a.size())return poly(n,0);
if((__int128)tim*pos>=n)return poly(n,0);
poly b;b.resize(n-tim*pos);
for(int i=0;i<b.size()&&i+pos<a.size();++i)
b[i]=a[i+pos];
b=pow(b,n,tim);b.resize(n);
for(int i=n-1;i>=tim*pos;--i)b[i]=b[i-tim*pos];
for(int i=tim*pos-1;i>=0;--i)b[i]=0;
return b;
}
poly sqrt(const poly&a,int n)
{
assert(a[0]==1);
return exp(ln(a,n)*pw(2,mod-2),n);
}
}//namespace Poly
using Poly::poly;
struct matrix
{
int a[35][35];
int det(int n)
{
int res=1;
for(int i=1;i<=n;++i)
{
for(int j=i+1;j<=n;++j)if(j!=i)
{
while(a[i][i])
{
int div=mod-a[j][i]/a[i][i];
for(int k=i;k<=n;++k)
a[j][k]=(a[j][k]+1ll*div*a[i][k])%mod;
swap(a[i],a[j]),res=mod-res;
}
swap(a[i],a[j]),res=mod-res;
}
res=1ll*res*a[i][i]%mod;
}
return res;
}
}A;
int pw(int a,int b=mod-2)
{
int c=1;
for(;b;b>>=1,a=1ll*a*a%mod)
if(b&1)c=1ll*c*a%mod;
return c;
}
struct mint
{
int a;
mint(){a=0;}
mint(int x){a=x%mod;}
void operator+=(mint b){a+=b.a;if(a>=mod)a-=mod;}
mint operator+(mint b)const{mint c=a;c+=b;return c;}
void operator-=(mint b){a+=mod-b.a;if(a>=mod)a-=mod;}
mint operator-(mint b)const{mint c=a;c-=b;return c;}
void operator*=(mint b){a=1ll*a*b.a%mod;}
mint operator*(mint b)const{mint c=a;c*=b;return c;}
void operator/=(mint b){a=1ll*a*pw(b.a)%mod;}
mint operator/(mint b)const{mint c=a;c/=b;return c;}
mint operator-(){mint c=mod-a;if(c.a>=mod)c.a-=mod;return c;}
bool operator==(mint b)const{return a==b.a;}
bool operator!=(mint b)const{return a!=b.a;}
bool operator<(mint b)const{return a<b.a;}
};
mint pw(mint a,int b)
{
mint c=1;
for(;b;b>>=1,a*=a)
if(b&1)c*=a;
return c;
}
namespace old
{
struct poly
{
vector<mint>a;
poly(){a.clear();}
poly(mint x){a.resize(1);a[0]=x;}
poly(vector<mint>x){a=x;}
mint&operator[](int x){if(a.size()<=x)a.resize(x+1);return a[x];}
int size()const{return a.size();}
void resize(int x){a.resize(x);}
void operator+=(poly b)
{
if(b.size()>a.size())a.resize(b.size());
for(int i=0;i<b.size();++i)a[i]+=b[i];
}
poly operator+(poly b){poly c=*this;c+=b;return c;}
void operator-=(poly b)
{
if(b.size()>a.size())a.resize(b.size());
for(int i=0;i<b.size();++i)a[i]-=b[i];
}
poly operator-(poly b){poly c=*this;c-=b;return c;}
poly operator*(poly b)
{
poly c;c.resize(a.size()+b.size()-1);
for(int i=0;i<a.size();++i)
for(int j=0;j<b.size();++j)
c[i+j]+=a[i]*b[j];
return c;
}
void operator*=(mint b)
{
for(auto&i:a)i*=b;
}
poly operator*(mint b){poly c=*this;c*=b;return c;}
poly operator/(mint b){b=pw(b.a);return *this*b;}
void operator/=(mint b){b=pw(b.a);*this*=b;}
poly operator/(poly b)
{
poly a=*this;
assert(b.size()==2);
mint x=mint(1)/b[1];
poly c;c.resize(a.size()-1);
for(int i=a.size()-1;i;--i)
c[i-1]=a[i]*x,a[i-1]-=c[i-1]*b[0];
return c;
}
poly operator<<(int b)
{
poly c;c.resize(a.size()+b);
for(int i=0;i<a.size();++i)c[i+b]=a[i];
return c;
}
mint operator()(mint x)
{
mint cur=1,res=0;
for(int i=0;i<a.size();++i)
res+=cur*a[i],cur*=x;
return res;
}
};
struct point{mint x,y;point(mint a=0,mint b=0){x=a,y=b;}};
poly interpolate(const vector<point>&vec)
{
poly all(mint(1)),sum;
for(auto i:vec)
all=all*poly({-i.x,mint(1)});
for(auto i:vec)
{
mint den=1;
for(auto j:vec)if(i.x!=j.x)den*=i.x-j.x;
sum+=all/poly({-i.x,mint(1)})/den*i.y;
}
return sum;
}
}//namespace old
int fac[6005],ifac[6005];
poly wys[35][35];
int n,a[35],b[35];
int main()
{
Poly::init();
fac[0]=fac[1]=ifac[0]=ifac[1]=1;
for(int i=2;i<=6000;++i)fac[i]=1ll*fac[i-1]*i%mod,ifac[i]=1ll*ifac[i-1]*Poly::inv[i]%mod;
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);
int t=0;
for(int i=1;i<=n;++i)
if(a[i]>b[i]){puts("0");return 0;}
else t+=b[i]-a[i];
if(t&1){puts("0");return 0;}t/=2;
for(int i=1;i<=n;++i)for(int j=1;j<=n;++j)
if(a[i]<a[j]&&b[i]>b[j]){puts("0");return 0;}
for(int i=1;i<=n;++i)for(int j=1;j<=n;++j)if(a[i]<=b[j])
{
wys[i][j].resize(b[j]-a[i]+2>>1);
for(int c=0;2*c<=b[j]-a[i];++c)
add(wys[i][j][c],1ll*pw(mod-1,c)*ifac[c]%mod*ifac[b[j]-a[i]-2*c]%mod);
}
vector<old::point>vec;
for(int i=0;i<=t;++i)
{
for(int x=1;x<=n;++x)for(int y=1;y<=n;++y)
A.a[x][y]=wys[x][y](i);
vec.push_back({i,A.det(n)});
}
old::poly res=old::interpolate(vec);
int ans=0;
for(int i=0;i<=t;++i)
ans=(ans+1ll*fac[t-i<<1]*ifac[t-i]%mod*res[i].a)%mod;
ans=1ll*ans*pw(mod+1>>1,t)%mod*fac[t]%mod;
printf("%d\n",ans);
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 5936kb
input:
3 1 2 3 3 4 5
output:
1
result:
ok answer is '1'
Test #2:
score: 0
Accepted
time: 3ms
memory: 6220kb
input:
3 1 2 3 7 8 9
output:
42
result:
ok answer is '42'
Test #3:
score: 0
Accepted
time: 3ms
memory: 5892kb
input:
3 1 4 7 3 6 9
output:
6
result:
ok answer is '6'
Test #4:
score: 0
Accepted
time: 3ms
memory: 5956kb
input:
1 1 3
output:
0
result:
ok answer is '0'
Test #5:
score: 0
Accepted
time: 3ms
memory: 5944kb
input:
1 1 1
output:
1
result:
ok answer is '1'
Test #6:
score: 0
Accepted
time: 3ms
memory: 6220kb
input:
2 1 4 4 7
output:
1
result:
ok answer is '1'
Test #7:
score: 0
Accepted
time: 3ms
memory: 5772kb
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: 3ms
memory: 5768kb
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: 385ms
memory: 6236kb
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: 3ms
memory: 5876kb
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: 32ms
memory: 6092kb
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: 23ms
memory: 6028kb
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: 1089ms
memory: 6680kb
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'