QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#373654 | #1262. Justice For Everyone | 275307894a | AC ✓ | 105ms | 33096kb | C++14 | 5.7kb | 2024-04-01 21:25:52 | 2024-04-01 21:25:53 |
Judging History
answer
#include<bits/stdc++.h>
#define Gc() getchar()
#define Me(x,y) memset(x,y,sizeof(x))
#define Mc(x,y) memcpy(x,y,sizeof(x))
#define d(x,y) ((m)*(x-1)+(y))
#define R(n) (rnd()%(n)+1)
#define Pc(x) putchar(x)
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define eb emplace_back
#define all(x) x.begin(),x.end()
using namespace std;using ll=long long;using db=double;using lb=long db;using ui=unsigned;using ull=unsigned long long;using pii=pair<int,int>;
const int N=30+5,M=6000+5,K=1000+5,mod=998244353,Mod=mod-1;const db eps=1e-8;const int INF=1e9+7;mt19937 rnd(263082);
#define Tp template<typename T>
#define Ts template<typename T,typename... Ar>
namespace Debug{
Tp void _debug(char* f,T t){cerr<<f<<'='<<t<<endl;}
Ts void _debug(char* f,T x,Ar... y){while(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);}
#ifdef LOCAL
#define gdb(...) _debug((char*)#__VA_ARGS__,__VA_ARGS__)
#else
#define gdb(...) void()
#endif
}using namespace Debug;
using poly=vector<ll>;
ll mpow(ll x,int y=mod-2){ll ans=1;while(y) y&1&&(ans=ans*x%mod),y>>=1,x=x*x%mod;return ans;}
namespace Poly{
const int N=1<<21,g=3;
int tr[N],k,a[N],b[N];ll pw[N];
void init(int n){
for(k=2;k<=n;k<<=1);
for(int i=0;i<k;i++) tr[i]=(tr[i>>1]>>1)|(i&1?k/2:0);
pw[k/2]=1;pw[k/2+1]=mpow(g,(mod-1)/k);
for(int i=k/2+2;i<k;i++) pw[i]=pw[i-1]*pw[k/2+1]%mod;
for(int i=k/2-1;i;i--) pw[i]=pw[i<<1];
}
void ntt(int *A,int k,int flag){
static ull a[N];for(int i=0;i<k;i++) a[tr[i]]=A[i];
for(int i=2;i<=k;i<<=1){
ll *e=pw+i/2;
for(int j=0;j<k;j+=i){
for(int h=j;h<j+i/2;h++){
int x=a[h+i/2]*e[h-j]%mod;
a[h+i/2]=a[h]+mod-x;a[h]+=x;
}
}
if(i==(1<<18)){
for(int j=0;j<k;j++) a[j]%=mod;
}
}
for(int i=0;i<k;i++) A[i]=a[i]%mod;
if(flag) return;
reverse(A+1,A+k);
ll iv=mpow(k);for(int i=0;i<k;i++) A[i]=A[i]*iv%mod;
}
namespace Public{
poly operator *(const poly &A,const poly &B){
int n=A.size(),m=B.size(),lim=n+m-1;
init(lim);
copy(A.begin(),A.end(),a);fill(a+n,a+k,0);
copy(B.begin(),B.end(),b);fill(b+m,b+k,0);
ntt(a,k,1);ntt(b,k,1);
for(int i=0;i<k;i++) a[i]=1ll*a[i]*b[i]%mod;
ntt(a,k,0);
poly c(lim);copy(a,a+lim,c.begin());
return c;
}
poly& operator *=(poly &A,const poly &B){
return A=A*B;
}
poly operator *(const poly &A,const int B){
poly C=A;for(int i=0;i<C.size();i++) C[i]=1ll*C[i]*B%mod;
return C;
}
poly& operator *=(poly &A,const int &B){
return A=A*B;
}
poly operator +(const poly &A,const poly &B){
poly C(max(A.size(),B.size()));
for(int i=0;i<A.size();i++) C[i]=A[i];
for(int i=0;i<B.size();i++) C[i]=(B[i]+C[i])%mod;
return C;
}
poly& operator +=(poly &A,const poly &B){
return A=A+B;
}
poly operator -(const poly &A,const poly &B){
poly C(max(A.size(),B.size()));
for(int i=0;i<A.size();i++) C[i]=A[i];
for(int i=0;i<B.size();i++) C[i]=(C[i]+mod-B[i])%mod;
return C;
}
poly& operator -=(poly &A,const poly &B){
return A=A-B;
}
poly operator %(const poly &A,const int &B){
poly C=A;
if(C.size()>B) C.resize(B);
return C;
}
poly& operator %=(poly &A,const int B){
return A=A%B;
}
poly inve(poly A,int n=-1){
if(n==-1) n=A.size();
if(n==1) return poly({(int)mpow(A[0])});
poly A0=inve(A%(n+1>>1),n+1>>1);
return A0*(poly({2})-A*A0%n)%n;
}
poly sqrt(poly A,int n=-1){
if(n==-1) n=A.size();
if(n==1) return poly({1});
poly A0=sqrt(A%(n+1>>1),n+1>>1);
return (A0*A0%n+A)*inve(A0,n)%n*(mod+1>>1);
}
poly der(poly A){
poly B(A.size()-1);
for(int i=1;i<A.size();i++) B[i-1]=1ll*A[i]*i%mod;
return B;
}
poly integ(poly A){
poly B(A.size()+1);
static ll inv[N];inv[1]=1;for(int i=2;i<=A.size();i++) inv[i]=(mod-inv[mod%i])*(mod/i)%mod;
for(int i=0;i<A.size();i++) B[i+1]=inv[i+1]*A[i]%mod;
return B;
}
poly ln(poly A,int n=-1){
if(n==-1) n=A.size();
return integ(der(A)*inve(A,n)%n)%n;
}
poly exp(poly A,int n=-1){
if(n==-1) n=A.size();
if(n==1) return poly({1});
poly A0=exp(A%(n+1>>1),n+1>>1);
return A0*(poly({1})-ln(A0,n)+A)%n;
}
}
}using namespace Poly::Public;
int n,m,k,A[N],B[N],ans[1<<12],e[N][N][1<<12];ll inv[M],frc[M];
ll w[N][N];
ll Gauss(){
ll ans=1;
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++) if(w[j][i]) {
swap_ranges(w[i]+i,w[i]+n+1,w[j]+i);
ans=ans*(mod-1)%mod;break;
}
ans=ans*w[i][i]%mod;ll iv=mpow(w[i][i]);
for(int j=i+1;j<=n;j++){
int x=mod-w[j][i]*iv%mod;
for(int h=i;h<=n;h++) w[j][h]=(w[j][h]+x*w[i][h])%mod;
}
}
return ans;
}
void Solve(){
int i,j,h;scanf("%d",&n);
for(i=1;i<=n;i++) scanf("%d",&A[i]);
for(i=1;i<=n;i++) scanf("%d",&B[i]);
for(i=1;i<=n;i++){
if(A[i]>B[i]){puts("0");return;}
k+=B[i]-A[i];
}
for(i=1;i<=n;i++) for(j=1;j<=n;j++) if(A[i]<A[j]&&B[j]<B[i]){puts("0");return;}
if(k&1){puts("0");return;}k>>=1;
inv[1]=1;for(i=2;i<=2*k;i++) inv[i]=(mod-inv[mod%i])*(mod/i)%mod;
for(frc[0]=inv[0]=i=1;i<=2*k;i++) frc[i]=frc[i-1]*i%mod,inv[i]=inv[i-1]*inv[i]%mod;
Poly::init(k+1);
for(i=1;i<=n;i++)for(j=1;j<=n;j++)if(A[i]<=B[j]){
for(h=0;2*h<=B[j]-A[i];h++) e[i][j][h]=inv[h]*inv[B[j]-A[i]-2*h]%mod;
Poly::ntt(e[i][j],Poly::k,1);
}
for(i=0;i<Poly::k;i++){
Me(w,0);for(int x=1;x<=n;x++) for(int y=1;y<=n;y++) w[x][y]=e[x][y][i];
ans[i]=Gauss();
}
Poly::ntt(ans,Poly::k,0);
ll tot=0;
for(i=0;i<=k;i++) tot+=(i&1?mod-1:1)*frc[k]%mod*inv[k-i]%mod*frc[2*(k-i)]%mod*ans[i]%mod,gdb(tot%mod);
tot%=mod;for(i=1;i<=k;i++) tot=tot*(mod+1>>1)%mod;
printf("%lld\n",tot%mod);
}
int main(){
int t=1;
// scanf("%d",&t);
while(t--) Solve();
cerr<<clock()*1.0/CLOCKS_PER_SEC<<'\n';
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 14184kb
input:
3 1 2 3 3 4 5
output:
1
result:
ok answer is '1'
Test #2:
score: 0
Accepted
time: 0ms
memory: 12228kb
input:
3 1 2 3 7 8 9
output:
42
result:
ok answer is '42'
Test #3:
score: 0
Accepted
time: 0ms
memory: 14248kb
input:
3 1 4 7 3 6 9
output:
6
result:
ok answer is '6'
Test #4:
score: 0
Accepted
time: 2ms
memory: 14400kb
input:
1 1 3
output:
0
result:
ok answer is '0'
Test #5:
score: 0
Accepted
time: 0ms
memory: 14132kb
input:
1 1 1
output:
1
result:
ok answer is '1'
Test #6:
score: 0
Accepted
time: 1ms
memory: 12108kb
input:
2 1 4 4 7
output:
1
result:
ok answer is '1'
Test #7:
score: 0
Accepted
time: 1ms
memory: 7948kb
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: 8136kb
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: 54ms
memory: 29352kb
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: 8176kb
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: 11ms
memory: 25884kb
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: 0ms
memory: 18360kb
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: 105ms
memory: 33096kb
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'