15
15
namespace cp_algo ::math {
16
16
template <typename T, class Alloc = big_alloc<T>>
17
17
struct poly_t {
18
+ using Vector = std::vector<T, Alloc>;
18
19
using base = T;
19
- std::vector<T, Alloc> a;
20
+ Vector a;
20
21
21
22
poly_t & normalize () {
22
23
while (deg () >= 0 && lead () == base (0 )) {
@@ -27,8 +28,8 @@ namespace cp_algo::math {
27
28
28
29
poly_t (){}
29
30
poly_t (T a0): a{a0} {normalize ();}
30
- poly_t (auto const & t): a(t.begin(), t.end() ) {normalize ();}
31
- poly_t (std::vector<T, Alloc> &&t): a(std::move(t)) {normalize ();}
31
+ poly_t (Vector const & t): a(t) {normalize ();}
32
+ poly_t (Vector &&t): a(std::move(t)) {normalize ();}
32
33
33
34
poly_t & negate_inplace () {
34
35
std::ranges::transform (a, begin (a), std::negate{});
@@ -204,7 +205,7 @@ namespace cp_algo::math {
204
205
return *this ;
205
206
}
206
207
poly_t integr () const { // calculate integral with C = 0
207
- std::vector<T> res (deg () + 2 );
208
+ Vector res (deg () + 2 );
208
209
for (int i = 0 ; i <= deg (); i++) {
209
210
res[i + 1 ] = a[i] * small_inv<T>(i + 1 );
210
211
}
@@ -306,7 +307,7 @@ namespace cp_algo::math {
306
307
return poly_t (T (0 ));
307
308
}
308
309
assert ((*this )[0 ] != T (0 ));
309
- std::vector<T> Q (n);
310
+ Vector Q (n);
310
311
Q[0 ] = bpow (a[0 ], k);
311
312
auto a0inv = a[0 ].inv ();
312
313
for (int i = 1 ; i < (int )n; i++) {
@@ -389,10 +390,10 @@ namespace cp_algo::math {
389
390
// requires multiplying polynomials of size deg() and n+deg()!
390
391
poly_t chirpz (T z, int n) const { // P(1), P(z), P(z^2), ..., P(z^(n-1))
391
392
if (is_zero ()) {
392
- return std::vector<T> (n, 0 );
393
+ return Vector (n, 0 );
393
394
}
394
395
if (z == T (0 )) {
395
- std::vector<T> ans (n, (*this )[0 ]);
396
+ Vector ans (n, (*this )[0 ]);
396
397
if (n > 0 ) {
397
398
ans[0 ] = accumulate (begin (a), end (a), T (0 ));
398
399
}
@@ -405,7 +406,7 @@ namespace cp_algo::math {
405
406
406
407
// res[i] = prod_{1 <= j <= i} 1/(1 - z^j)
407
408
static auto _1mzk_prod_inv (T z, int n) {
408
- std::vector<T> res (n, 1 ), zk (n);
409
+ Vector res (n, 1 ), zk (n);
409
410
zk[0 ] = 1 ;
410
411
for (int i = 1 ; i < n; i++) {
411
412
zk[i] = zk[i - 1 ] * z;
@@ -421,12 +422,12 @@ namespace cp_algo::math {
421
422
// prod_{0 <= j < n} (1 - z^j x)
422
423
static auto _1mzkx_prod (T z, int n) {
423
424
if (n == 1 ) {
424
- return poly_t (std::vector<T> {1 , -1 });
425
+ return poly_t (Vector {1 , -1 });
425
426
} else {
426
427
auto t = _1mzkx_prod (z, n / 2 );
427
428
t *= t.mulx (bpow (z, n / 2 ));
428
429
if (n % 2 ) {
429
- t *= poly_t (std::vector<T> {1 , -bpow (z, n - 1 )});
430
+ t *= poly_t (Vector {1 , -bpow (z, n - 1 )});
430
431
}
431
432
return t;
432
433
}
@@ -443,7 +444,7 @@ namespace cp_algo::math {
443
444
return std::vector{(*this )[1 ], (*this )[0 ] - (*this )[1 ]};
444
445
}
445
446
}
446
- std::vector<T> y (n);
447
+ Vector y (n);
447
448
for (int i = 0 ; i < n; i++) {
448
449
y[i] = (*this )[i];
449
450
}
@@ -465,7 +466,7 @@ namespace cp_algo::math {
465
466
466
467
static poly_t build (std::vector<poly_t > &res, int v, auto L, auto R) { // builds evaluation tree for (x-a1)(x-a2)...(x-an)
467
468
if (R - L == 1 ) {
468
- return res[v] = std::vector<T> {-*L, 1 };
469
+ return res[v] = Vector {-*L, 1 };
469
470
} else {
470
471
auto M = L + (R - L) / 2 ;
471
472
return res[v] = build (res, 2 * v, L, M) * build (res, 2 * v + 1 , M, R);
@@ -483,7 +484,7 @@ namespace cp_algo::math {
483
484
}
484
485
}
485
486
486
- poly_t to_newton (std::vector<T> p) {
487
+ poly_t to_newton (Vector p) {
487
488
if (is_zero ()) {
488
489
return *this ;
489
490
}
@@ -493,7 +494,7 @@ namespace cp_algo::math {
493
494
return to_newton (tree, 1 , begin (p), end (p));
494
495
}
495
496
496
- std::vector<T> eval (std::vector<poly_t > &tree, int v, auto l, auto r) { // auxiliary evaluation function
497
+ Vector eval (std::vector<poly_t > &tree, int v, auto l, auto r) { // auxiliary evaluation function
497
498
if (r - l == 1 ) {
498
499
return {eval (*l)};
499
500
} else {
@@ -505,10 +506,10 @@ namespace cp_algo::math {
505
506
}
506
507
}
507
508
508
- std::vector<T> eval (std::vector<T> x) { // evaluate polynomial in (x1, ..., xn)
509
+ Vector eval (Vector x) { // evaluate polynomial in (x1, ..., xn)
509
510
size_t n = x.size ();
510
511
if (is_zero ()) {
511
- return std::vector<T> (n, T (0 ));
512
+ return Vector (n, T (0 ));
512
513
}
513
514
std::vector<poly_t > tree (4 * n);
514
515
build (tree, 1 , begin (x), end (x));
@@ -526,7 +527,7 @@ namespace cp_algo::math {
526
527
}
527
528
}
528
529
529
- static auto inter (std::vector<T> x, std::vector<T> y) { // interpolates minimum polynomial from (xi, yi) pairs
530
+ static auto inter (Vector x, Vector y) { // interpolates minimum polynomial from (xi, yi) pairs
530
531
size_t n = x.size ();
531
532
std::vector<poly_t > tree (4 * n);
532
533
return build (tree, 1 , begin (x), end (x)).deriv ().inter (tree, 1 , begin (y), end (y));
@@ -552,15 +553,15 @@ namespace cp_algo::math {
552
553
}
553
554
554
555
static poly_t ones (size_t n) { // P(x) = 1 + x + ... + x^{n-1}
555
- return std::vector<T> (n, 1 );
556
+ return Vector (n, 1 );
556
557
}
557
558
558
559
static poly_t expx (size_t n) { // P(x) = e^x (mod x^n)
559
560
return ones (n).borel ();
560
561
}
561
562
562
563
static poly_t log1px (size_t n) { // P(x) = log(1+x) (mod x^n)
563
- std::vector<T> coeffs (n, 0 );
564
+ Vector coeffs (n, 0 );
564
565
for (size_t i = 1 ; i < n; i++) {
565
566
coeffs[i] = (i & 1 ? T (i).inv () : -T (i).inv ());
566
567
}
@@ -602,7 +603,7 @@ namespace cp_algo::math {
602
603
}
603
604
604
605
poly_t x2 () { // P(x) -> P(x^2)
605
- std::vector<T> res (2 * a.size ());
606
+ Vector res (2 * a.size ());
606
607
for (size_t i = 0 ; i < a.size (); i++) {
607
608
res[2 * i] = a[i];
608
609
}
@@ -612,7 +613,7 @@ namespace cp_algo::math {
612
613
// Return {P0, P1}, where P(x) = P0(x) + xP1(x)
613
614
std::array<poly_t , 2 > bisect (size_t n) const {
614
615
n = std::min (n, size (a));
615
- std::vector<T> res[2 ];
616
+ Vector res[2 ];
616
617
for (size_t i = 0 ; i < n; i++) {
617
618
res[i % 2 ].push_back (a[i]);
618
619
}
0 commit comments