QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#427958 | #1262. Justice For Everyone | lfxxx | AC ✓ | 2271ms | 5024kb | C++14 | 4.4kb | 2024-06-01 16:35:54 | 2024-06-01 16:35:55 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define i128 __int128
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(x) (x).begin(), (x).end()
bool be;
constexpr int N = 35, V = 205, mod = 998244353;
int n, t, a[N], b[N], c[N][N], fc[N * V], ifc[N * V], f[N][N][V], g[N * V], h[N * V], tmp[N * V];
inline int qpow(int b, int k)
{
int res = 1;
while (k) {
if (k & 1) res = (ll) res * b % mod;
b = (ll) b * b % mod;
k >>= 1;
}
return res;
}
int det()
{
int ans = 1;
for (int i = 1; i <= n; ++i) {
if (c[i][i] == 0) {
ans = -ans;
for (int j = i + 1; j <= n; ++j) {
if (c[j][i]) {
swap(c[i], c[j]);
break;
}
}
}
for (int j = i + 1; j <= n; ++j) {
int p = (ll) c[j][i] * qpow(c[i][i], mod - 2) % mod;
for (int k = i; k <= n; ++k) {
c[j][k] = (c[j][k] - (ll) c[i][k] * p) % mod;
}
}
}
for (int i = 1; i <= n; ++i) {
ans = (ll) ans * c[i][i] % mod;
}
return ans;
}
int calc(int *f, int x)
{
int ans = 0;
for (int i = 200; i >= 0; --i) {
ans = ((ll) ans * x + f[i]) % mod;
}
return ans;
}
namespace poly {
int a[N * V];
struct Poly {
int a[N * V], cnt;
Poly(int n) {
cnt = n;
memset(a, 0, (N * V) * sizeof(int));
}
inline int& operator [] (int x) {
return a[x];
}
inline Poly operator + (Poly b) {
Poly c(max(cnt, b.cnt));
for (int i = 0; i < cnt; ++i) {
c[i] = (a[i] + b[i]) % mod;
}
return c;
}
inline Poly operator * (int k) {
Poly b(cnt);
for (int i = 0; i < cnt; ++i) {
b[i] = (ll) a[i] * k % mod;
}
return b;
}
inline Poly operator * (Poly b) {
Poly c(cnt + b.cnt - 1);
for (int i = 0; i < cnt; ++i) {
for (int j = 0; j < b.cnt; ++j) {
c[i + j] = (c[i + j] + (ll) a[i] * b[j]) % mod;
}
}
return c;
}
inline Poly operator / (Poly b) {
Poly c(cnt - b.cnt + 1);
for (int i = 0; i < cnt; ++i) tmp[i] = a[i];
for (int i = cnt - 1; i >= b.cnt - 1; --i) {
int p = (ll) a[i] * qpow(b[b.cnt - 1], mod - 2) % mod;
c[i - (b.cnt - 1)] = p;
for (int j = b.cnt - 1; j >= 0; --j) {
a[i - (b.cnt - 1 - j)] = (a[i - (b.cnt - 1 - j)] - (ll) p * b[j]) % mod;
}
}
#ifdef IAKIOI
for (int i = b.cnt - 2; i >= 0; --i) assert(a[i] == 0);
#endif
for (int i = 0; i < cnt; ++i) a[i] = tmp[i];
return c;
}
};
void getpoly(int *ans)
{
for (int i = 0; i <= t; ++i) {
a[i] = 1;
for (int j = 0; j <= t; ++j) {
if (i != j) {
a[i] = (ll) a[i] * (i - j) % mod;
}
}
a[i] = (ll) qpow(a[i], mod - 2) * g[i] % mod;
}
Poly P(1);
P[0] = 1;
for (int i = 0; i <= t; ++i) {
Poly Q(2);
Q[0] = -i, Q[1] = 1;
P = P * Q;
}
Poly R(P.cnt - 1);
for (int i = 0; i <= t; ++i) {
Poly Q(2);
Q[0] = -i, Q[1] = 1;
R = R + ((P / Q) * a[i]);
}
for (int i = 0; i < R.cnt; ++i) {
ans[i] = R[i];
}
}
}
bool en;
int main()
{
cerr << (&be - &en) / 1024.0 / 1024 << " MB\n--------------------------------" << endl;
#ifdef IAKIOI
freopen("in.in", "r", stdin);
// freopen("out.out", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
for (int i = 1; i <= n; ++i) {
cin >> b[i];
}
for (int i = 1; i <= n; ++i) {
t += b[i] - a[i];
if (b[i] < a[i]) {
cout << "0\n";
return 0;
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
if ((a[i] < a[j]) != (b[i] < b[j])) {
cout << "0\n";
return 0;
}
}
}
if (t % 2) {
cout << "0\n";
return 0;
}
t /= 2;
fc[0] = 1;
for (int i = 1; i <= 6002; ++i) fc[i] = (ll) fc[i - 1] * i % mod;
ifc[6002] = qpow(fc[6002], mod - 2);
for (int i = 6001; i >= 0; --i) ifc[i] = (ll) ifc[i + 1] * (i + 1) % mod;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
for (int k = 0; 2 * k <= b[j] - a[i]; ++k) {
f[i][j][k] = (ll) ifc[k] * ifc[b[j] - a[i] - 2 * k] % mod;
}
}
}
for (int x = 0; x <= t; ++x) {
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
c[i][j] = calc(f[i][j], x);
}
}
g[x] = det();
}
poly::getpoly(h);
int ans = 0;
for (int i = 0; i <= t; ++i) {
ans = (ans + (ll) h[i] * (i & 1 ? -1 : 1) * fc[t] % mod * fc[2 * (t - i)] % mod * ifc[t - i]) % mod;
}
cout << ((ll) ans * qpow(qpow(2, t), mod - 2) % mod + mod) % mod << '\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 4020kb
input:
3 1 2 3 3 4 5
output:
1
result:
ok answer is '1'
Test #2:
score: 0
Accepted
time: 1ms
memory: 4124kb
input:
3 1 2 3 7 8 9
output:
42
result:
ok answer is '42'
Test #3:
score: 0
Accepted
time: 1ms
memory: 4124kb
input:
3 1 4 7 3 6 9
output:
6
result:
ok answer is '6'
Test #4:
score: 0
Accepted
time: 1ms
memory: 3896kb
input:
1 1 3
output:
0
result:
ok answer is '0'
Test #5:
score: 0
Accepted
time: 1ms
memory: 4076kb
input:
1 1 1
output:
1
result:
ok answer is '1'
Test #6:
score: 0
Accepted
time: 1ms
memory: 4080kb
input:
2 1 4 4 7
output:
1
result:
ok answer is '1'
Test #7:
score: 0
Accepted
time: 0ms
memory: 3848kb
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: 0ms
memory: 3792kb
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: 1313ms
memory: 4932kb
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: 3852kb
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: 253ms
memory: 4624kb
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: 53ms
memory: 4192kb
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: 2271ms
memory: 5024kb
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'