-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC.cpp
593 lines (477 loc) · 16.8 KB
/
C.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
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
#include <bits/stdc++.h>
using namespace std;
using namespace chrono;
template<const int &MOD>
struct _m_int {
int val;
_m_int(int64_t v = 0) {
if (v < 0) v = v % MOD + MOD;
if (v >= MOD) v %= MOD;
val = int(v);
}
_m_int(uint64_t v) {
if (v >= MOD) v %= MOD;
val = int(v);
}
_m_int(int v) : _m_int(int64_t(v)) {}
_m_int(unsigned v) : _m_int(uint64_t(v)) {}
explicit operator int() const { return val; }
explicit operator unsigned() const { return val; }
explicit operator int64_t() const { return val; }
explicit operator uint64_t() const { return val; }
explicit operator double() const { return val; }
explicit operator long double() const { return val; }
_m_int& operator+=(const _m_int &other) {
val -= MOD - other.val;
if (val < 0) val += MOD;
return *this;
}
_m_int& operator-=(const _m_int &other) {
val -= other.val;
if (val < 0) val += MOD;
return *this;
}
static unsigned fast_mod(uint64_t x, unsigned m = MOD) {
#if !defined(_WIN32) || defined(_WIN64)
return unsigned(x % m);
#endif
// Optimized mod for Codeforces 32-bit machines.
// x must be less than 2^32 * m for this to work, so that x / m fits in an unsigned 32-bit int.
unsigned x_high = unsigned(x >> 32), x_low = unsigned(x);
unsigned quot, rem;
asm("divl %4\n"
: "=a" (quot), "=d" (rem)
: "d" (x_high), "a" (x_low), "r" (m));
return rem;
}
_m_int& operator*=(const _m_int &other) {
val = fast_mod(uint64_t(val) * other.val);
return *this;
}
_m_int& operator/=(const _m_int &other) {
return *this *= other.inv();
}
friend _m_int operator+(const _m_int &a, const _m_int &b) { return _m_int(a) += b; }
friend _m_int operator-(const _m_int &a, const _m_int &b) { return _m_int(a) -= b; }
friend _m_int operator*(const _m_int &a, const _m_int &b) { return _m_int(a) *= b; }
friend _m_int operator/(const _m_int &a, const _m_int &b) { return _m_int(a) /= b; }
_m_int& operator++() {
val = val == MOD - 1 ? 0 : val + 1;
return *this;
}
_m_int& operator--() {
val = val == 0 ? MOD - 1 : val - 1;
return *this;
}
_m_int operator++(int) { _m_int before = *this; ++*this; return before; }
_m_int operator--(int) { _m_int before = *this; --*this; return before; }
_m_int operator-() const {
return val == 0 ? 0 : MOD - val;
}
friend bool operator==(const _m_int &a, const _m_int &b) { return a.val == b.val; }
friend bool operator!=(const _m_int &a, const _m_int &b) { return a.val != b.val; }
friend bool operator<(const _m_int &a, const _m_int &b) { return a.val < b.val; }
friend bool operator>(const _m_int &a, const _m_int &b) { return a.val > b.val; }
friend bool operator<=(const _m_int &a, const _m_int &b) { return a.val <= b.val; }
friend bool operator>=(const _m_int &a, const _m_int &b) { return a.val >= b.val; }
static const int SAVE_INV = int(1e6) + 5;
static _m_int save_inv[SAVE_INV];
static void prepare_inv() {
// Make sure MOD is prime, which is necessary for the inverse algorithm below.
for (int64_t p = 2; p * p <= MOD; p += p % 2 + 1)
assert(MOD % p != 0);
save_inv[0] = 0;
save_inv[1] = 1;
for (int i = 2; i < SAVE_INV; i++)
save_inv[i] = save_inv[MOD % i] * (MOD - MOD / i);
}
_m_int inv() const {
if (save_inv[1] == 0)
prepare_inv();
if (val < SAVE_INV)
return save_inv[val];
_m_int product = 1;
int v = val;
while (v >= SAVE_INV) {
product *= MOD - MOD / v;
v = MOD % v;
}
return product * save_inv[v];
}
_m_int pow(int64_t p) const {
if (p < 0)
return inv().pow(-p);
_m_int a = *this, result = 1;
while (p > 0) {
if (p & 1)
result *= a;
p >>= 1;
if (p > 0)
a *= a;
}
return result;
}
friend ostream& operator<<(ostream &os, const _m_int &m) {
return os << m.val;
}
};
template<const int &MOD> _m_int<MOD> _m_int<MOD>::save_inv[_m_int<MOD>::SAVE_INV];
extern const int MOD = 998244353;
using mod_int = _m_int<MOD>;
vector<mod_int> _factorial = {1, 1}, _inv_factorial = {1, 1};
void prepare_factorials(int64_t maximum) {
static int prepared_maximum = 1;
if (maximum <= prepared_maximum)
return;
// Prevent increasing maximum by only 1 each time.
maximum += maximum / 16;
_factorial.resize(maximum + 1);
_inv_factorial.resize(maximum + 1);
for (int i = prepared_maximum + 1; i <= maximum; i++) {
_factorial[i] = i * _factorial[i - 1];
_inv_factorial[i] = _inv_factorial[i - 1] / i;
}
prepared_maximum = int(maximum);
}
mod_int factorial(int n) {
if (n < 0) return 0;
prepare_factorials(n);
return _factorial[n];
}
mod_int inv_factorial(int n) {
if (n < 0) return 0;
prepare_factorials(n);
return _inv_factorial[n];
}
mod_int choose(int64_t n, int64_t r) {
if (r < 0 || r > n) return 0;
prepare_factorials(n);
return _factorial[n] * _inv_factorial[r] * _inv_factorial[n - r];
}
mod_int permute(int64_t n, int64_t r) {
if (r < 0 || r > n) return 0;
prepare_factorials(n);
return _factorial[n] * _inv_factorial[n - r];
}
mod_int inv_choose(int64_t n, int64_t r) {
assert(0 <= r && r <= n);
prepare_factorials(n);
return _inv_factorial[n] * _factorial[r] * _factorial[n - r];
}
mod_int inv_permute(int64_t n, int64_t r) {
assert(0 <= r && r <= n);
prepare_factorials(n);
return _inv_factorial[n] * _factorial[n - r];
}
template<const int &MOD>
struct NTT {
using ntt_int = _m_int<MOD>;
vector<ntt_int> roots = {0, 1};
vector<int> bit_reverse;
int max_size = -1;
ntt_int root;
void reset() {
roots = {0, 1};
max_size = -1;
}
static bool is_power_of_two(int n) {
return (n & (n - 1)) == 0;
}
static int round_up_power_two(int n) {
while (n & (n - 1))
n = (n | (n - 1)) + 1;
return max(n, 1);
}
// Given n (a power of two), finds k such that n == 1 << k.
static int get_length(int n) {
assert(is_power_of_two(n));
return __builtin_ctz(n);
}
// Rearranges the indices to be sorted by lowest bit first, then second lowest, etc., rather than highest bit first.
// This makes even-odd div-conquer much easier.
void bit_reorder(int n, vector<ntt_int> &values) {
if (int(bit_reverse.size()) != n) {
bit_reverse.assign(n, 0);
int length = get_length(n);
for (int i = 1; i < n; i++)
bit_reverse[i] = (bit_reverse[i >> 1] >> 1) | ((i & 1) << (length - 1));
}
for (int i = 0; i < n; i++)
if (i < bit_reverse[i])
swap(values[i], values[bit_reverse[i]]);
}
void find_root() {
max_size = 1 << __builtin_ctz(MOD - 1);
root = 2;
// Find a max_size-th primitive root of MOD.
while (!(root.pow(max_size) == 1 && root.pow(max_size / 2) != 1))
root++;
}
void prepare_roots(int n) {
if (max_size < 0)
find_root();
assert(n <= max_size);
if (int(roots.size()) >= n)
return;
int length = get_length(int(roots.size()));
roots.resize(n);
// The roots array is set up such that for a given power of two n >= 2, roots[n / 2] through roots[n - 1] are
// the first half of the n-th primitive roots of MOD.
while (1 << length < n) {
// z is a 2^(length + 1)-th primitive root of MOD.
ntt_int z = root.pow(max_size >> (length + 1));
for (int i = 1 << (length - 1); i < 1 << length; i++) {
roots[2 * i] = roots[i];
roots[2 * i + 1] = roots[i] * z;
}
length++;
}
}
void fft_iterative(int n, vector<ntt_int> &values) {
assert(is_power_of_two(n));
prepare_roots(n);
bit_reorder(n, values);
for (int len = 1; len < n; len *= 2)
for (int start = 0; start < n; start += 2 * len)
for (int i = 0; i < len; i++) {
ntt_int even = values[start + i];
ntt_int odd = values[start + len + i] * roots[len + i];
values[start + len + i] = even - odd;
values[start + i] = even + odd;
}
}
void invert_fft(int n, vector<ntt_int> &values) {
ntt_int inv_n = ntt_int(n).inv();
for (int i = 0; i < n; i++)
values[i] *= inv_n;
reverse(values.begin() + 1, values.end());
fft_iterative(n, values);
}
const int FFT_CUTOFF = 150;
// Note: `circular = true` can be used for a 2x speedup when only the `max(n, m) - min(n, m) + 1` fully overlapping
// ranges are needed. It computes results using indices modulo the power-of-two FFT size; see the brute force below.
template<typename T>
vector<T> mod_multiply(const vector<T> &_left, const vector<T> &_right, bool circular = false) {
if (_left.empty() || _right.empty())
return {};
vector<ntt_int> left(_left.begin(), _left.end());
vector<ntt_int> right(_right.begin(), _right.end());
int n = int(left.size());
int m = int(right.size());
int output_size = circular ? round_up_power_two(max(n, m)) : n + m - 1;
// Brute force when either n or m is small enough.
if (min(n, m) < FFT_CUTOFF) {
auto &&mod_output_size = [&](int x) {
return x < output_size ? x : x - output_size;
};
static const uint64_t U64_BOUND = numeric_limits<uint64_t>::max() - uint64_t(MOD) * MOD;
vector<uint64_t> result(output_size, 0);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
int index = mod_output_size(i + j);
result[index] += uint64_t(left[i]) * uint64_t(right[j]);
if (result[index] > U64_BOUND)
result[index] %= MOD;
}
for (uint64_t &x : result)
if (x >= MOD)
x %= MOD;
return vector<T>(result.begin(), result.end());
}
int N = round_up_power_two(output_size);
left.resize(N, 0);
right.resize(N, 0);
if (left == right) {
fft_iterative(N, left);
right = left;
} else {
fft_iterative(N, left);
fft_iterative(N, right);
}
for (int i = 0; i < N; i++)
left[i] *= right[i];
invert_fft(N, left);
return vector<T>(left.begin(), left.begin() + output_size);
}
template<typename T>
vector<T> mod_inverse(const vector<T> &_a) {
vector<ntt_int> a(_a.begin(), _a.end());
assert(!a.empty());
int N = int(a.size());
vector<ntt_int> b = {a[0].inv()};
while(int(b.size()) < N) {
vector<ntt_int> x(a.begin(), a.begin() + min(a.size(), b.size() << 1));
x.resize(b.size() << 1);
b.resize(b.size() << 1);
vector<ntt_int> c = b;
fft_iterative(int(c.size()), c);
fft_iterative(int(x.size()), x);
ntt_int inv = ntt_int(int(x.size())).inv();
for (int i = 0; i < (int) x.size(); i++) {
x[i] *= c[i] * inv;
}
reverse(x.begin() + 1, x.end());
fft_iterative(int(x.size()), x);
rotate(x.begin(), x.begin() + (x.size() >> 1), x.end());
fill(x.begin() + (x.size() >> 1), x.end(), 0);
fft_iterative(int(x.size()), x);
for (int i = 0; i < (int) x.size(); i++) {
x[i] *= c[i] * inv;
}
reverse(x.begin() + 1, x.end());
fft_iterative(int(x.size()), x);
for (int i = 0; i < ((int) x.size() >> 1); i++) {
b[i + ((int) x.size() >> 1)] = -x[i];
}
}
b.resize(N);
return vector<T>(b.begin(), b.end());
}
template<typename T>
vector<T> mod_power(const vector<T> &v, int exponent) {
assert(exponent >= 0);
vector<T> result = {1};
if (exponent == 0)
return result;
for (int k = 31 - __builtin_clz(exponent); k >= 0; k--) {
result = mod_multiply(result, result);
if (exponent >> k & 1)
result = mod_multiply(result, v);
}
return result;
}
// Multiplies many polynomials whose total degree is n in O(n log^2 n).
template<typename T>
vector<T> mod_multiply_all(const vector<vector<T>> &polynomials) {
if (polynomials.empty())
return {1};
struct compare_size {
bool operator()(const vector<T> &x, const vector<T> &y) {
return x.size() > y.size();
}
};
priority_queue<vector<T>, vector<vector<T>>, compare_size> pq(polynomials.begin(), polynomials.end());
while (pq.size() > 1) {
vector<T> a = pq.top(); pq.pop();
vector<T> b = pq.top(); pq.pop();
pq.push(mod_multiply(a, b));
}
return pq.top();
}
};
NTT<MOD> ntt;
void run_cases() {
int n;
cin >> n;
const int maxN = 3e6;
vector<int64_t> a(n);
for(auto &u: a) cin >> u;
int maxA = *max_element(a.begin(), a.end());
vector<vector<int>> ind(maxN);
for(int i=0;i<n;i++) {
ind[a[i]].push_back(i);
}
unordered_map<int64_t, int> cnt;
int cnt2 = 0;
for(auto u: a) {
cnt[u]++;
}
vector<int> twos;
for(auto u: cnt) {
if(u.second >= 2) {
cnt2++;
twos.push_back(u.first);
}
if(u.second >= 4) {
cout << "YES" << '\n';
int x,y,z,w;
x = ind[u.first][0] + 1;
z = ind[u.first][1] + 1;
y = ind[u.first][2] + 1;
w = ind[u.first][3] + 1;
cout << x << " " << y << " " << z << " " << w << '\n';
return;
}
}
if(cnt2 >= 2) {
cout << "YES" << '\n';
int x,y,z,w;
x = ind[twos[0]][0] + 1;
z = ind[twos[0]][1] + 1;
y = ind[twos[1]][0] + 1;
w = ind[twos[1]][1] + 1;
cout << x << " " << y << " " << z << " " << w << '\n';
return;
}
if(cnt2 == 1) {
int x,y,z,w;
x = ind[twos[0]][0] + 1;
y = ind[twos[0]][1] + 1;
set<int> contains;
int target = 2 * twos[0];
contains.insert(x);
contains.insert(y);
for(int i=0;i<n;i++) {
if(cnt.count(- a[i] + target)) {
z = ind[a[i]][0] + 1;
w = ind[- a[i] + target][0] + 1;
if(!contains.count(z) and !contains.count(w)){
contains.insert(z);
contains.insert(w);
break;
}
}
}
if(contains.size() == 4) {
cout << "YES" << '\n';
cout << x << " " << y << " " << z << " " << w << '\n';
return;
}
}
{
vector<mod_int> p1(maxA + 1, 0);
for(auto u: a) {
p1[u] = 1;
}
p1 = ntt.mod_multiply(p1, p1);
for(auto u: cnt) {
p1[2 * u.first]--;
}
for(int j=1;j<p1.size();j++) {
int u = int(p1[j]);
if(u >= 4) {
cout << "YES" << '\n';
int x,y,z,w;
set<int> contains;
for(int i=0;i<n;i++) {
if(cnt.count(- a[i] + j)) {
x = ind[a[i]][0] + 1;
y = ind[- a[i] + j][0] + 1;
contains.insert(x);
contains.insert(y);
break;
}
}
for(int i=0;i<n;i++) {
if(cnt.count(- a[i] + j)) {
z = ind[a[i]][0] + 1;
w = ind[- a[i] + j][0] + 1;
if(!contains.count(z) and !contains.count(w))
break;
}
}
cout << x << " " << y << " " << z << " " << w << '\n';
return;
}
}
cout << "NO" << '\n';
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(nullptr);
int tests = 1;
// cin >> tests;
for(int test = 1;test <= tests;test++) {
run_cases();
}
}