-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathE.cpp
More file actions
486 lines (330 loc) · 9.83 KB
/
Copy pathE.cpp
File metadata and controls
486 lines (330 loc) · 9.83 KB
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/****************************** begin /template.h ******************************/
/***********************************************************************
* [?] APIO 2021 Template
* [+] Made By: Lior Yehezkely, FiveSixFiveFour
* [~] Apples are ORZ
* *********************************************************************/
/*** begin #define flags ***/
// #define USE_ORDERED_STATISTICS
// #define BIG_BIGINT
/*** end #define flags ***/
/*** begin includes ***/
#include <bits/stdc++.h>
#ifdef USE_ORDERED_STATISTICS
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#endif
using namespace std;
/*** end includes ***/
/*** begin typedefs ***/
typedef long long int ll;
#ifdef BIG_BIGINT
typedef __int128_t bigint;
#else
typedef ll bigint;
#endif
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<vpi> vvpi;
typedef vector<vpl> vvpl;
typedef set<int> si;
typedef multiset<int> msi;
typedef set<ll> sl;
typedef multiset<ll> msl;
typedef long double ld;
template<class T> using func = function<T>;
#ifdef USE_ORDERED_STATISTICS
typedef tree<
pl,
null_type,
less<pl>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
#endif
/*** end typedefs ***/
/*** begin #defines ***/
#define clrcin cin.ignore(numeric_limits<streamsize>::max(),'\n');
#define GOGOGO ios::sync_with_stdio(false); cin.tie(nullptr);
#define BYEBYE return 0;
#define all(cn) (cn).begin(), (cn).end()
#define rep(i, n) for (int i = 0; i < n; ++i)
#define csz(c) ((int)c.size())
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define popcnt __builtin_popcount
#define popcntll __builtin_popcount_ll
/*** end #defines ***/
const int INFI = 1e9 + 5;
const ll INFL = 1e18 + 5;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
auto dist = uniform_int_distribution<int>(0, INFI);
auto distll = uniform_int_distribution<ll>(0, INFL);
int rnd() { return dist(rng); }
ll rndl() { return distll(rng); }
/****************************** end /template.h ******************************/
/****************************** begin /main.cpp ******************************/
/*** begin #define flags ***/
// #define BRUH_WHY_TESTCASES
//#define PREC 7
// #define INPUT_FILE "maze2.in"
//#define OUTPUT_FILE "hi.out"
/*** end #define flags ***/
const int maxn = 1e5 + 5;
const int maxm = 1e5 + 5;
const ll mod = 998244353; // I am assuming that this number is prime
ll half_power[maxn];
ll inv_half_power[maxn];
ll pre_half_power[maxn];
int n, m;
ll a[maxn];
ll p[maxm]; // 0 based and sorted increasingly
ll pre_a[maxn]; // prefix sum
ll wl_pre_a[maxn]; // prefix sum weighted linearly (by distance from 0)
ll wl_suf_a[maxn]; // prefix sum weighted linearly (by distance from n-1)
ll dp[maxn];
ll mpow(ll b, ll p) {
ll res = 1; ll curr = b;
while(p) {
if(p%2ll) {
res *= curr; res %= mod;
}
p /= 2ll;
curr *= curr;
curr %= mod;
}
return res;
}
ll mfix(ll x) {
ll res = x % mod;
if(x < 0) res += mod;
return res;
}
ll minv(ll x) {
return mpow(x, mod-2);
}
ll mmul(ll l, ll r) {
return mfix((l*r));
}
ll madd(ll l, ll r) {
return mfix(l+r);
}
ll msub(ll l, ll r) {
return mfix(l-r);
}
const ll half = minv(2);
const ll inv_half = 2;
void input() {
cin >> n >> m;
rep(i, n) {
cin >> a[i];
}
rep(i, m) {
cin >> p[i]; p[i]--;
}
}
void calc_half_caches() {
half_power[0] = 1;
pre_half_power[0] = 1;
inv_half_power[0] = 1;
for(int i = 1; i <= n+1; i++) {
half_power[i] = mmul(half, half_power[i-1]);
inv_half_power[i] = mmul(inv_half, inv_half_power[i-1]);
pre_half_power[i] = madd(pre_half_power[i-1], half_power[i]);
}
}
void calc_a_caches() {
pre_a[0] = a[0];
wl_pre_a[0] = 0;
wl_suf_a[n-1] = 0;
for(int i = 1; i < n; i++) {
pre_a[i] = madd(a[i], pre_a[i-1]);
wl_pre_a[i] = madd(wl_pre_a[i-1], mmul(i, a[i]));
}
for(int i = n-2; i >= 0; i--) {
wl_suf_a[i] = madd(wl_suf_a[i+1], mmul(n-1-i, a[i]));
}
}
ll asum(int l, int r) {
return msub(pre_a[r], (l-1>=0?pre_a[l-1]:0));
}
ll aincsum(int l, int r) { // 0 * a[l] + 1 * a[l+1] + ...
return msub(msub(wl_pre_a[r], (l-1>=0?wl_pre_a[l-1]:0)), l*asum(l,r));
}
ll adecsum(int l, int r) {
return msub(msub(wl_suf_a[l], (r+1<=n-1?wl_suf_a[r+1]:0)), (n-1-r)*asum(l,r));
}
inline ll gu(int x) {
if(x<0 || x >n-1) return 0;
return wl_pre_a[x];
}
inline ll gd(int x) {
if(x<0 || x >n-1) return 0;
return wl_suf_a[x];
}
inline ll g(int x) {
if(x<0 || x >n-1) return 0;
return pre_a[x];
}
// V(i, j) = gu(pi + (pj - pi) / 2) - gu(pi - 1) - pi * (g(pi + (pj - pi) / 2) - g(pi - 1)) + gd(pj) - gd(pi + (pj - pi) / 2) - (n - 1- pj)* ( g(pj) - g(pi + (pj - pi) / 2))
inline ll h(int x) { // the DEC thing
return madd(mmul((n - 1 - x), g(x/2)), madd(gu(x/2), gd(x/2+1))); // NOTE: check the x/2 + 1
}
//*********************************************************
const ll root = 15311432;
const ll root_1 = minv(root);
const ll root_pw = 1 << 23;
void fft(vector<ll> & vec, bool invert) {
ll sz = vec.size();
for (ll i = 1, j = 0; i < sz; i++) {
ll bit = sz >> 1;
for (; j & bit; bit >>= 1)
j ^= bit;
j ^= bit;
if (i < j)
swap(vec[i], vec[j]);
}
for (ll len = 2; len <= sz; len <<= 1) {
ll wlen = invert ? root_1 : root;
for (ll i = len; i < root_pw; i <<= 1)
wlen = (ll)(1LL * wlen * wlen % mod);
for (ll i = 0; i < sz; i += len) {
ll w = 1;
for (ll j = 0; j < len / 2; j++) {
ll u = vec[i+j], v = (ll)(1LL * vec[i+j+len/2] * w % mod);
vec[i+j] = u + v < mod ? u + v : u + v - mod;
vec[i+j+len/2] = u - v >= 0 ? u - v : u - v + mod;
w = (ll)(1LL * w * wlen % mod);
}
}
}
if (invert) {
ll n_1 = minv(sz);
for (ll & x : vec)
x = (ll)(1LL * x * n_1 % mod);
}
}
vector<ll> multiply(vector<ll> const& aa, vector<ll> const& bb) {
vector<ll> fa(aa.begin(), aa.end()), fb(bb.begin(), bb.end());
ll sz = 1;
while (sz < aa.size() + bb.size())
sz <<= 1;
fa.resize(sz);
fb.resize(sz);
fft(fa, false);
fft(fb, false);
for (ll i = 0; i < sz; i++) {
fa[i] *= fb[i];
fa[i] = mfix(fa[i]);
}
fft(fa, true);
vector<ll> result(sz);
for (ll i = 0; i < sz; i++)
result[i] = mfix(fa[i]);
return result;
}
//*********************************************************
ll res = 0;
// I have no idea what am I even doing at this point
void solvee(int l, int r) { // the D & E parts, also with the corresponding probability of f(i)
if (l>=r) return;
int pl = p[l];
int pr = p[r];
const int sz = pr - pl + 1;
vl p1(sz, 0);
vl p2(sz, 0);
int m = l;
while(m < r && p[m+1] < (pl + pr) / 2) m++;
for(int i = l; i <= m; i++) {
p1[p[i]-pl] = inv_half_power[i];
}
for(int i = m+1; i <= r; i++) {
p2[p[i]-pl] = half_power[i+1];
}
vl p3 = multiply(p1, p2);
for(int i = 0; i < csz(p3); i++) {
res = madd(res, mmul(h(i+2*pl), p3[i]));
}
p1.resize(0);
p2.resize(0);
p3.resize(0);
solvee(l, m);
solvee(m+1, r);
}
void printfr(ll x) {
bool f = false;
for(int i = 1; i <= 1000 && !f; i++) {
for(int j = 1; j <= 1000; j++) {
if(mmul(i,minv(j)) == x) {
cout << i << ' ' << j << '\n'; f = true; break;
}
if(mmul(-i,minv(j)) == x) {
cout << -i << ' ' << j << '\n'; f = true; break;
}
}
}
}
void solve(int t) {
input();
calc_half_caches();
calc_a_caches();
solvee(0, m-1); // D+E+C
bool f = false;
ll b = 0;
ll amul = 0;
for (ll i = m-1; i >=0; i--) {
ll pi = p[i];
if(i < m-1) {
res = madd(res, mmul(madd(-gu(pi-1), mmul(pi,g(pi-1))), amul));
res = madd(res, b);
}
b = madd(b, mmul(madd(mmul(-1, gd(pi+1)), mmul(-1,mmul(n - 1- pi, g(pi)))), half));
b = mmul(b, half);
amul = madd(amul, half);
amul = mmul(amul, half);
}
for(int i = 0; i < m; i++) { // L and R
res = madd(res, mmul(half_power[i+1], adecsum(0,p[i])));
res = madd(res, mmul(half_power[m-1-i+1], aincsum(p[i],n-1)));
}
ll finalres = mmul(res, minv(mmul(inv_half_power[m]-1, minv(inv_half_power[m]))));
cout << finalres << '\n';
}
signed main()
{
#ifdef INPUT_FILE
freopen(INPUT_FILE, "r", stdin);
#endif
#ifdef OUTPUT_FILE
freopen(OUTPUT_FILE, "w", stdout);
#endif
GOGOGO
#ifdef PREC
cout << fixed << setprecision(PREC);
#endif
int t=1;
#ifdef BRUH_WHY_TESTCASES
cin >> t;
#endif
for(int i = 1; i <= t; ++i)
{
solve(i);
}
BYEBYE
}
/* PLEASE READ THIS
* N = 1
* GUESS A!!!!!!!
* DO SOMETHING INSTEAD OF NOTHING
*/
/****************************** end /main.cpp ******************************/