QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#369547 | #1262. Justice For Everyone | JCY_ | AC ✓ | 80ms | 6628kb | C++14 | 4.7kb | 2024-03-28 14:14:06 | 2024-03-28 14:14:06 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using i128 = __int128;
using u128 = unsigned __int128;
template <typename T>
void chkmax(T &x, const T &y) {
if (x < y) x = y;
}
template <typename T>
void chkmin(T &x, const T &y) {
if (y < x) x = y;
}
constexpr int MOD = 998244353, INV_2 = (MOD + 1) / 2;
void inc(int &x, int y) {
x += y;
if (x >= MOD) x -= MOD;
}
void dec(int &x, int y) {
x -= y;
if (x < 0) x += MOD;
}
int add(int x, int y) {
x += y;
return x >= MOD ? x - MOD : x;
}
int sub(int x, int y) {
x -= y;
return x < 0 ? x + MOD : x;
}
int neg(int x) { return x ? MOD - x : 0; }
int adj(int x) { return x < 0 ? x + MOD : x; }
int qpow(int x, int y) {
int ret = 1;
for (; y; y >>= 1, x = (ll)x * x % MOD)
if (y & 1) ret = (ll)ret * x % MOD;
return ret;
}
namespace poly {
struct poly_t : vector<int> {
using vector<int>::vector;
};
constexpr int MAXLOG = 12, MAXLEN = 1 << MAXLOG;
int coef[MAXLEN], rev[MAXLEN];
void init() {
for (int i = 1; i < MAXLEN; ++i)
rev[i] = rev[i >> 1] >> 1 | (i & 1 ? MAXLEN >> 1 : 0);
int tmp = qpow(3, (MOD - 1) / MAXLEN);
coef[MAXLEN / 2] = 1;
for (int i = MAXLEN / 2 + 1; i < MAXLEN; ++i)
coef[i] = (ll)coef[i - 1] * tmp % MOD;
for (int i = MAXLEN / 2 - 1; i >= 1; --i) coef[i] = coef[i << 1];
}
void ntt(poly_t &f, int v) {
int len = f.size(), d = __lg(len);
for (int i = 1; i < len; ++i) {
int tmp = rev[i] >> (MAXLOG - d);
if (i < tmp) swap(f[i], f[tmp]);
}
for (int i = 1; i < len; i <<= 1) {
for (int j = 0; j < len; j += i << 1) {
for (int k = j; k < j + i; ++k) {
int tmp = (ll)f[k + i] * coef[i + k - j] % MOD;
f[k + i] = sub(f[k], tmp);
inc(f[k], tmp);
}
}
}
if (v == -1) {
int tmp = qpow(len, MOD - 2);
for (auto &i : f) i = (ll)i * tmp % MOD;
reverse(f.begin() + 1, f.end());
}
}
} // namespace poly
using poly::poly_t;
constexpr int MAXN = 32, MAXB = 205, MAXT = 3010;
int n, mxd, t, a[MAXN], b[MAXN], fac[MAXT * 2], ifac[MAXT * 2], id[MAXN];
poly_t f[MAXB], res;
namespace gs {
int mat[MAXN][MAXN];
int det(int n) {
int ret = 1;
for (int i = 1; i <= n; ++i) {
if (!mat[i][i]) {
for (int j = i + 1; j <= n; ++j) {
if (mat[j][i]) {
swap_ranges(mat[i] + i, mat[i] + n + 1, mat[j] + i);
ret = neg(ret);
break;
}
}
if (!mat[i][i]) return 0;
}
ret = (ll)ret * mat[i][i] % MOD;
int inv = qpow(mat[i][i], MOD - 2);
for (int j = i + 1; j <= n; ++j) {
int coef = (ll)mat[j][i] * inv % MOD;
for (int k = i; k <= n; ++k)
mat[j][k] = adj((mat[j][k] - (ll)coef * mat[i][k]) % MOD);
}
}
return ret;
}
} // namespace gs
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
poly::init();
fac[0] = 1;
for (int i = 1; i < MAXT * 2; ++i) fac[i] = (ll)fac[i - 1] * i % MOD;
ifac[MAXT * 2 - 1] = qpow(fac[MAXT * 2 - 1], MOD - 2);
for (int i = MAXT * 2 - 2; i >= 0; --i)
ifac[i] = (ll)ifac[i + 1] * (i + 1) % MOD;
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 1; i <= n; ++i) {
cin >> b[i];
if (a[i] > b[i]) {
cout << "0\n";
return 0;
}
t += b[i] - a[i];
}
iota(id + 1, id + n + 1, 1);
sort(id + 1, id + n + 1, [](int x, int y) { return a[x] < a[y]; });
for (int i = 2; i <= n; ++i) {
if (b[id[i - 1]] > b[id[i]]) {
cout << "0\n";
return 0;
}
}
if (!t) {
cout << "1\n";
return 0;
}
if (t & 1) {
cout << "0\n";
return 0;
}
t >>= 1;
mxd = *max_element(b + 1, b + n + 1) - *min_element(a + 1, a + n + 1);
int len = 2 << __lg(t);
for (int d = 0; d <= mxd; ++d) {
f[d].resize(len);
for (int c = 0; c * 2 <= d && c < len; ++c)
f[d][c] = (ll)ifac[c] * ifac[d - c * 2] % MOD;
poly::ntt(f[d], 1);
}
res.resize(len);
for (int x = 0; x < len; ++x) {
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
if (a[i] > b[j]) {
gs::mat[i][j] = 0;
} else {
gs::mat[i][j] = f[b[j] - a[i]][x];
}
}
}
res[x] = gs::det(n);
}
poly::ntt(res, -1);
int ans = 0;
for (int sum = 0; sum <= t; ++sum) {
ans = (ans + (ll)fac[(t - sum) * 2] * ifac[t - sum] % MOD * res[sum] *
(sum & 1 ? -1 : 1)) %
MOD;
}
ans = (ll)(ans + MOD) * fac[t] % MOD * qpow(INV_2, t) % MOD;
cout << ans << "\n";
return 0;
}
/*
g++ B.cpp -o B -std=c++14 -O2 -Wall -Wextra -Wshadow -g
-fsanitize=address,undefined
*/
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3752kb
input:
3 1 2 3 3 4 5
output:
1
result:
ok answer is '1'
Test #2:
score: 0
Accepted
time: 1ms
memory: 3660kb
input:
3 1 2 3 7 8 9
output:
42
result:
ok answer is '42'
Test #3:
score: 0
Accepted
time: 1ms
memory: 3660kb
input:
3 1 4 7 3 6 9
output:
6
result:
ok answer is '6'
Test #4:
score: 0
Accepted
time: 1ms
memory: 3668kb
input:
1 1 3
output:
0
result:
ok answer is '0'
Test #5:
score: 0
Accepted
time: 0ms
memory: 3704kb
input:
1 1 1
output:
1
result:
ok answer is '1'
Test #6:
score: 0
Accepted
time: 0ms
memory: 3704kb
input:
2 1 4 4 7
output:
1
result:
ok answer is '1'
Test #7:
score: 0
Accepted
time: 0ms
memory: 3712kb
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: 3708kb
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: 40ms
memory: 4468kb
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: 0ms
memory: 3712kb
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: 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 200
output:
836228983
result:
ok answer is '836228983'
Test #12:
score: 0
Accepted
time: 3ms
memory: 3904kb
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: 80ms
memory: 6628kb
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'