-
Notifications
You must be signed in to change notification settings - Fork 0
/
1988F.cpp
311 lines (274 loc) · 6.68 KB
/
1988F.cpp
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// #pragma GCC optimize("O2,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
const int MAXN = 1024;
const int MOD = 998244353;
int n;
int a[MAXN], b[MAXN], c[MAXN];
// Debugging macro
#ifdef DBG
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#else
#define dbg(...)
#endif
/* Snippet BEGIN */
template <class T> constexpr T power(T a, ll b)
{
T res{1};
for (; b; b /= 2, a *= a) {
if (b % 2) {
res *= a;
}
}
return res;
}
constexpr ll mul(ll a, ll b, ll p)
{
ll res = a * b - ll(1.L * a * b / p) * p;
res %= p;
if (res < 0) {
res += p;
}
return res;
}
template <ll P> struct MInt
{
ll x;
constexpr MInt() : x{0} {}
constexpr MInt(ll x) : x{norm(x % P)} {}
constexpr ll norm(ll x) const
{
if (x < 0) {
x += P;
}
if (x >= P) {
x -= P;
}
return x;
}
constexpr ll val() const { return x; }
constexpr MInt operator-() const
{
MInt res;
res.x = norm(P - x);
return res;
}
constexpr MInt inv() const { return power(*this, P - 2); }
constexpr MInt &operator*=(MInt rhs) &
{
if (P < (1ULL << 31)) {
x = x * rhs.x % int(P);
} else {
x = mul(x, rhs.x, P);
}
return *this;
}
constexpr MInt &operator+=(MInt rhs) &
{
x = norm(x + rhs.x);
return *this;
}
constexpr MInt &operator-=(MInt rhs) &
{
x = norm(x - rhs.x);
return *this;
}
constexpr MInt &operator/=(MInt rhs) & { return *this *= rhs.inv(); }
friend constexpr MInt operator*(MInt lhs, MInt rhs)
{
MInt res = lhs;
res *= rhs;
return res;
}
friend constexpr MInt operator+(MInt lhs, MInt rhs)
{
MInt res = lhs;
res += rhs;
return res;
}
friend constexpr MInt operator-(MInt lhs, MInt rhs)
{
MInt res = lhs;
res -= rhs;
return res;
}
friend constexpr MInt operator/(MInt lhs, MInt rhs)
{
MInt res = lhs;
res /= rhs;
return res;
}
friend constexpr bool operator==(MInt lhs, MInt rhs)
{
return lhs.val() == rhs.val();
}
friend constexpr bool operator!=(MInt lhs, MInt rhs)
{
return lhs.val() != rhs.val();
}
friend constexpr bool operator<(MInt lhs, MInt rhs)
{
return lhs.val() < rhs.val();
}
};
constexpr int P = MOD;
using Z = MInt<P>;
struct Comb
{
int n;
std::vector<Z> _fac;
std::vector<Z> _invfac;
std::vector<Z> _inv;
Comb() : n{0}, _fac{1}, _invfac{1}, _inv{0} {}
Comb(int n) : Comb() { init(n); }
void init(int m)
{
m = std::min<ll>(m, P - 1);
if (m <= n)
return;
_fac.resize(m + 1);
_invfac.resize(m + 1);
_inv.resize(m + 1);
for (int i = n + 1; i <= m; i++) {
_fac[i] = _fac[i - 1] * i;
}
_invfac[m] = _fac[m].inv();
for (int i = m; i > n; i--) {
_invfac[i - 1] = _invfac[i] * i;
_inv[i] = _invfac[i] * _fac[i - 1];
}
n = m;
}
Z fac(int m)
{
if (m > n)
init(2 * m);
return _fac[m];
}
Z invfac(int m)
{
if (m > n)
init(2 * m);
return _invfac[m];
}
Z inv(int m)
{
if (m > n)
init(2 * m);
return _inv[m];
}
Z binom(int n, int m)
{
if (n < m || m < 0)
return 0;
return fac(n) * invfac(m) * invfac(n - m);
}
} comb;
/* Snippet END */
// Reset function for global variables
void reset_arrays(int size)
{
fill(a, a + size, 0);
fill(b, b + size, 0);
fill(c, c + size, 0);
}
// Solve function would be implemented here
void solve()
{
/// dp[ n ][ prefix max ][ ascent ]
static Z f[2][MAXN][MAXN];
using pointer = Z(*)[MAXN];
pointer curr = f[0];
pointer next = f[1];
curr[1][0] = 1;
static Z F[MAXN][MAXN], G[MAXN][MAXN];
for (int i = 1; i <= n; i++) {
// dbg("i: %d, cur: %p\n", i, curr);
// for (int j = 1; j <= n; j++) {
// // ascent
// for (int k = 0; k <= n; k++) {
// F[i][k] += curr[j][k] * a[j];
// G[i][k] += curr[j][i - 1 - k] * b[j];
// }
// }
//
// if (i == n)
// break;
// prefix max
for (int j = 1; j <= i; j++) {
// ascent
// Z* next_j = nextj];
// Z* next_j1 = next[j+1];
for (int k = j - 1; k < i; k++) {
// G[i][k] += curr[j][i - 1 - k] * b[j];
Z current = curr[j][k];
if (current == 0)
continue;
G[i][i - 1 - k] += current * b[j];
F[i][k] += current * a[j];
// 1xxxxxxxx
next[j + 1][k + 1] += current;
// xxA1Bxxxx A<B
next[j][k] += current * k;
// xxA1Bxxxx A>B
next[j][k + 1] += current * (i - k - 1);
curr[j][k] = 0;
}
// fill(&curr[j][0], &curr[j][i], 0);
}
swap(curr, next);
// memset(next, 0, sizeof(f[0]));
}
// if (n == 700)
// exit(0);
//
dbg("F, G calculated\n");
static Z H[MAXN][MAXN];
for (int j = 1; j <= n; j++) {
for (int p = 0; p <= n; p++) {
Z &value = H[j][p];
for (int q = 0; q < j && p + q < n; q++) {
value += G[j][q] * c[p + q];
}
}
}
Comb comb(n);
for (int ni = 1; ni <= n; ni++) {
dbg("ni: %d\n", ni);
Z ans = 0;
for (int i = 1; i <= ni; i++) {
int j = ni - i + 1;
for (int p = 0; p < i; p++) {
// for (int q = 0; q < j; q++) {
// Z val = comb.binom(ni - 1, i - 1) * F[i][p] * G[j][q] *
// c[p + q];
// dbg("%d %d %d %d %lld\n", i, j, p, q, val.val());
// ans += val;
// }
Z val = comb.binom(ni - 1, i - 1) * F[i][p] * H[j][p];
ans += val;
}
}
printf("%lld ", ans.val());
}
printf("\n");
}
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
for (int i = 1; i <= n; i++) {
scanf("%d", &b[i]);
}
for (int i = 0; i < n; i++) {
scanf("%d", &c[i]);
}
solve();
return 0;
}