QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#50924 | #1262. Justice For Everyone | tricyzhkx | AC ✓ | 127ms | 18304kb | C++14 | 2.8kb | 2022-09-29 17:57:17 | 2022-09-29 17:57:18 |
Judging History
answer
# include <bits/stdc++.h>
using namespace std;
const int mod=998244353,g=3,_2=(mod+1)/2;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> Poly;
ll fac[6010],inv[6010];
int n,m,a[40],b[40],ta[40],tb[40],id[40],rev[6010],w[6010],maxn;
Poly F[40][40],G;
ll C(int n,int m){return fac[n]*inv[n-m]%mod*inv[m]%mod;}
ll power(ll a,int b)
{
ll ans=1;
for(;b;b>>=1,a=a*a%mod)
if(b&1) ans=ans*a%mod;
return ans;
}
void init(int len)
{
int l=0;maxn=1;
while(maxn<=len) maxn<<=1,l++;
for(int i=0;i<maxn;i++) rev[i]=(rev[i>>1]>>1)|((i&1)<<(l-1));
for(int i=1;i<maxn;i<<=1)
{
ll wm=power(g,(mod-1)/(i<<1));w[i]=1;
for(int j=1;j<i;j++) w[i+j]=w[i+j-1]*wm%mod;
}
}
void NTT(Poly &p,bool flag)
{
static ull a[6010];
p.resize(maxn);
for(int i=0;i<maxn;i++) a[i]=p[rev[i]];
for(int i=1;i<maxn;i<<=1)
for(int j=0;j<maxn;j+=(i<<1))
for(int k=j;k<j+i;k++)
{
ull x=a[k],y=a[k+i]*w[i+k-j]%mod;
a[k]=x+y;a[k+i]=x+mod-y;
}
if(flag) for(int i=0;i<maxn;i++) p[i]=a[i]%mod;
else
{
int inv=power(maxn,mod-2);
reverse(a+1,a+maxn);
for(int i=0;i<maxn;i++) p[i]=a[i]%mod*inv%mod;
}
}
int Det(int x)
{
static int a[40][40];
int ans=1;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
a[i][j]=F[i][j][x];
for(int i=1;i<=n;i++)
{
for(int j=i;j<=n;j++)
if(a[j][i])
{
for(int k=i;k<=n;k++) swap(a[i][k],a[j][k]);
if(i!=j) ans=mod-ans;
break;
}
if(!a[i][i]) return 0;
ans=(ll)ans*a[i][i]%mod;
ll t=power(a[i][i],mod-2);
for(int j=i;j<=n;j++) a[i][j]=a[i][j]*t%mod;
for(int j=i+1;j<=n;j++)
{
ll t=mod-a[j][i];
for(int k=i;k<=n;k++) a[j][k]=(a[j][k]+t*a[i][k])%mod;
}
}
return ans;
}
int main()
{
int len=0,ans=0;
cin>>n;
for(int i=1;i<=n;i++) scanf("%d",&a[i]),id[i]=i,len-=a[i];
for(int i=1;i<=n;i++) scanf("%d",&b[i]),m=max(m,b[i]),len+=b[i];
fac[0]=fac[1]=inv[0]=inv[1]=1;
for(int i=2;i<=n*m;i++) fac[i]=fac[i-1]*i%mod;
for(int i=2;i<=n*m;i++) inv[i]=(mod-mod/i)*inv[mod%i]%mod;
for(int i=2;i<=n*m;i++) inv[i]=inv[i-1]*inv[i]%mod;
sort(id+1,id+n+1,[](int i,int j){return a[i]<a[j];});
for(int i=1;i<=n;i++) ta[i]=a[id[i]],tb[i]=b[id[i]];
swap(a,ta);swap(b,tb);
for(int i=1;i<n;i++)
if(b[i]>b[i+1]) return puts("0"),0;
for(int i=1;i<=n;i++)
if(a[i]>b[i]) return puts("0"),0;
if(len&1) return puts("0"),0;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
if(a[i]>b[j]) continue;
int l=b[j]-a[i];
F[i][j].resize(l/2+1);
for(int k=0;k<=l/2;k++) F[i][j][k]=inv[k]*inv[l-2*k]%mod;
}
init(n*m/2);G.resize(maxn);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
NTT(F[i][j],1);
for(int i=0;i<maxn;i++) G[i]=Det(i);
NTT(G,0);
for(int i=0;i<=len/2;i++) ans=(ans+(i&1?mod-inv[len/2-i]:inv[len/2-i])*fac[len-2*i]%mod*G[i])%mod;
cout<<power(_2,len/2)*ans%mod*fac[len/2]%mod<<endl;
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 3ms
memory: 3716kb
input:
3 1 2 3 3 4 5
output:
1
result:
ok answer is '1'
Test #2:
score: 0
Accepted
time: 2ms
memory: 3728kb
input:
3 1 2 3 7 8 9
output:
42
result:
ok answer is '42'
Test #3:
score: 0
Accepted
time: 2ms
memory: 3716kb
input:
3 1 4 7 3 6 9
output:
6
result:
ok answer is '6'
Test #4:
score: 0
Accepted
time: 1ms
memory: 3696kb
input:
1 1 3
output:
0
result:
ok answer is '0'
Test #5:
score: 0
Accepted
time: 2ms
memory: 3864kb
input:
1 1 1
output:
1
result:
ok answer is '1'
Test #6:
score: 0
Accepted
time: 2ms
memory: 3776kb
input:
2 1 4 4 7
output:
1
result:
ok answer is '1'
Test #7:
score: 0
Accepted
time: 2ms
memory: 3704kb
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: 2ms
memory: 3828kb
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: 52ms
memory: 10880kb
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: 3788kb
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: 126ms
memory: 18128kb
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: 5ms
memory: 4264kb
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: 127ms
memory: 18304kb
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'