QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#389530 | #1262. Justice For Everyone | le0n | AC ✓ | 140ms | 10784kb | C++14 | 3.6kb | 2024-04-14 15:07:07 | 2024-04-14 15:07:07 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int mod = 998244353;
int ord[35], a[35], b[35], c[35];
int omg[1 << 15], rev[1 << 15];
int val[205][1 << 13], ans[1 << 13], fac[1 << 14], inv[1 << 14];
int w[35][35];
ull tmp[1 << 15];
int qpow(int x, int y)
{
int ans = 1;
while(y)
{
if(y & 1)
ans = (ll)ans * x % mod;
x = (ll)x * x % mod;
y >>= 1;
}
return ans;
}
void prep(int l)
{
int i, j, w;
for(i = 0; i <= l; i++)
{
for(j = 0; j < (1 << i); j++)
rev[(1 << i) + j] = (rev[(1 << i) + (j >> 1)] >> 1) | ((j & 1) << (i - 1));
omg[1 << i] = 1;
w = qpow(3, (mod - 1) >> i);
for(j = 1; j < (1 << i); j++)
omg[(1 << i) + j] = (ll)omg[(1 << i) + j - 1] * w % mod;
}
fac[0] = 1;
for(i = 1; i <= (1 << l); i++)
fac[i] = (ll)fac[i - 1] * i % mod;
inv[1 << l] = qpow(fac[1 << l], mod - 2);
for(i = (1 << l); i >= 1; i--)
inv[i - 1] = (ll)inv[i] * i % mod;
}
inline void ntt(int *A, int n, bool t) // 0 DFT 1 IDFT
{
int i, j, k, q, x;
for(i = 0; i < (1 << n); i++)
tmp[i] = A[i];
for(i = 0; i < (1 << n); i++)
if(rev[(1 << n) + i] > i)
swap(tmp[rev[(1 << n) + i]], tmp[i]);
for(i = 0; i < n; i++)
{
for(j = 0; j < (1 << n); j += (2 << i))
for(k = 0; k < (1 << i); k++)
{
x = tmp[j + k + (1 << i)] * omg[(2 << i) + k] % mod;
tmp[j + k + (1 << i)] = tmp[j + k] + mod - x;
tmp[j + k] = tmp[j + k] + x;
}
if(i == 11 || i == n - 1)
for(j = 0; j < (1 << n); j++)
tmp[j] %= mod;
}
for(i = 0; i < (1 << n); i++)
A[i] = tmp[i];
if(t)
{
for(i = 1; i < (1 << n); i++)
if(i < (1 << n) - i)
swap(A[i], A[(1 << n) - i]);
q = qpow((1 << n), mod - 2);
for(i = 0; i < (1 << n); i++)
A[i] = (ll)A[i] * q % mod;
}
}
bool cmp(int x, int y)
{
return a[x] < a[y];
}
int det(int n)
{
int i, j, k, r, q, ans = 1;
for(i = 1; i <= n; i++)
{
for(j = i; j <= n; j++)
if(w[j][i])
break;
if(j > n)
return 0;
if(j != i)
{
swap(w[i], w[j]);
ans = mod - ans;
}
ans = (ll)ans * w[i][i] % mod;
r = mod - qpow(w[i][i], mod - 2);
for(j = i + 1; j <= n; j++)
{
q = (ll)r * w[j][i] % mod;
for(k = i; k <= n; k++)
w[j][k] = (w[j][k] + (ll)q * w[i][k]) % mod;
}
}
return ans;
}
int main()
{
prep(13);
int n, m, i, j, k, l, d = 0, tmp;
m = 0;
scanf("%d", &n);
for(i = 1; i <= n; i++)
{
scanf("%d", a + i);
m = max(m, a[i]);
}
for(i = 1; i <= n; i++)
{
scanf("%d", b + i);
m = max(m, b[i]);
}
for(i = 1; i <= n; i++)
ord[i] = i;
sort(ord + 1, ord + n + 1, cmp);
for(i = 1; i <= n; i++)
c[i] = a[ord[i]];
for(i = 1; i <= n; i++)
a[i] = c[i];
for(i = 1; i <= n; i++)
c[i] = b[ord[i]];
for(i = 1; i <= n; i++)
b[i] = c[i];
for(i = 1; i <= n; i++)
{
if(b[i - 1] > b[i] || a[i] > b[i])
{
printf("0\n");
return 0;
}
d += b[i] - a[i];
}
if(d & 1)
{
printf("0\n");
return 0;
}
d /= 2;
l = 0;
while((n * m) >> l)
l++;
for(i = 0; i <= m; i++)
{
for(j = 0; j <= i / 2; j++)
{
val[i][j] = (ll)inv[j] * inv[i - 2 * j] % mod;
if(j & 1)
val[i][j] = mod - val[i][j];
}
ntt(val[i], l, 0);
}
for(i = 0; i < (1 << l); i++)
{
for(j = 1; j <= n; j++)
for(k = 1; k <= n; k++)
w[j][k] = ((b[k] >= a[j]) ? val[b[k] - a[j]][i] : 0);
ans[i] = det(n);
}
ntt(ans, l, 1);
tmp = 0;
for(i = 0; i <= d; i++)
tmp = (tmp + (ll)fac[2 * (d - i)] * ans[i] % mod * inv[d - i]) % mod;
tmp = (ll)tmp * qpow(2, mod - 1 - d) % mod * fac[d] % mod;
printf("%d\n", tmp);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 4160kb
input:
3 1 2 3 3 4 5
output:
1
result:
ok answer is '1'
Test #2:
score: 0
Accepted
time: 1ms
memory: 4180kb
input:
3 1 2 3 7 8 9
output:
42
result:
ok answer is '42'
Test #3:
score: 0
Accepted
time: 1ms
memory: 4144kb
input:
3 1 4 7 3 6 9
output:
6
result:
ok answer is '6'
Test #4:
score: 0
Accepted
time: 1ms
memory: 4212kb
input:
1 1 3
output:
0
result:
ok answer is '0'
Test #5:
score: 0
Accepted
time: 1ms
memory: 4148kb
input:
1 1 1
output:
1
result:
ok answer is '1'
Test #6:
score: 0
Accepted
time: 1ms
memory: 6148kb
input:
2 1 4 4 7
output:
1
result:
ok answer is '1'
Test #7:
score: 0
Accepted
time: 1ms
memory: 4060kb
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: 4088kb
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: 68ms
memory: 10196kb
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: 4076kb
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: 139ms
memory: 10732kb
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: 7ms
memory: 5424kb
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: 140ms
memory: 10784kb
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'