Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
adamant-pwn committed May 14, 2024
1 parent ce40f15 commit 9e9cfe2
Show file tree
Hide file tree
Showing 42 changed files with 129 additions and 113 deletions.
2 changes: 1 addition & 1 deletion cp-algo/all
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace cp_algo {
using namespace data_structures;
using namespace geometry;
using namespace algebra;
using namespace math;
using namespace linalg;
using namespace random;
}
Expand Down
4 changes: 2 additions & 2 deletions cp-algo/data_structures/segtree/metas/affine.hpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#ifndef CP_ALGO_DATA_STRUCTURES_SEGMENT_TREE_METAS_AFFINE_HPP
#define CP_ALGO_DATA_STRUCTURES_SEGMENT_TREE_METAS_AFFINE_HPP
#include "base.hpp"
#include "../../../algebra/affine.hpp"
#include "../../../math/affine.hpp"
namespace cp_algo::data_structures::segtree::metas {
template<typename base>
struct affine_meta: base_meta<affine_meta<base>> {
using meta = affine_meta;
using lin = algebra::lin<base>;
using lin = math::lin<base>;

base sum = 0;
lin to_push = {};
Expand Down
4 changes: 2 additions & 2 deletions cp-algo/data_structures/treap/metas/reverse.hpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#ifndef CP_ALGO_DATA_STRUCTURES_TREAP_METAS_REVERSE_HPP
#define CP_ALGO_DATA_STRUCTURES_TREAP_METAS_REVERSE_HPP
#include "base.hpp"
#include "../../../algebra/affine.hpp"
#include "../../../math/affine.hpp"
#include <algorithm>
namespace cp_algo::data_structures::treap::metas {
template<typename base>
struct reverse_meta: base_meta {
using lin = algebra::lin<base>;
using lin = math::lin<base>;
base val;
size_t sz = 1;
bool reverse = false;
Expand Down
8 changes: 4 additions & 4 deletions cp-algo/linalg/frobenius.hpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#ifndef CP_ALGO_LINALG_FROBENIUS_HPP
#define CP_ALGO_LINALG_FROBENIUS_HPP
#include "../math/poly.hpp"
#include "matrix.hpp"
#include "../algebra/poly.hpp"
#include <algorithm>
#include <vector>
namespace cp_algo::linalg {
namespace cp_algo::math::linalg {
enum frobenius_mode {blocks, full};
template<frobenius_mode mode = blocks>
auto frobenius_form(auto const& A) {
using matrix = std::decay_t<decltype(A)>;
using base = matrix::base;
using polyn = algebra::poly_t<base>;
using polyn = math::poly_t<base>;
assert(A.n() == A.m());
size_t n = A.n();
std::vector<polyn> charps;
Expand Down Expand Up @@ -76,7 +76,7 @@ namespace cp_algo::linalg {

template<typename base>
auto frobenius_pow(matrix<base> A, uint64_t k) {
using polyn = algebra::poly_t<base>;
using polyn = math::poly_t<base>;
auto [T, Tinv, charps] = frobenius_form<full>(A);
std::vector<matrix<base>> blocks;
for(auto charp: charps) {
Expand Down
4 changes: 2 additions & 2 deletions cp-algo/linalg/matrix.hpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#ifndef CP_ALGO_LINALG_MATRIX_HPP
#define CP_ALGO_LINALG_MATRIX_HPP
#include "../random/rng.hpp"
#include "../algebra/common.hpp"
#include "../math/common.hpp"
#include "vector.hpp"
#include <iostream>
#include <optional>
#include <cassert>
#include <vector>
#include <array>
namespace cp_algo::linalg {
namespace cp_algo::math::linalg {
template<typename base_t>
struct matrix: valarray_base<matrix<base_t>, vec<base_t>> {
using base = base_t;
Expand Down
6 changes: 3 additions & 3 deletions cp-algo/linalg/vector.hpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#ifndef CP_ALGO_LINALG_VECTOR_HPP
#define CP_ALGO_LINALG_VECTOR_HPP
#include "../algebra/modint.hpp"
#include "../random/rng.hpp"
#include "../math/modint.hpp"
#include <functional>
#include <algorithm>
#include <valarray>
#include <iostream>
#include <iterator>
#include <cassert>
namespace cp_algo::linalg {
namespace cp_algo::math::linalg {
template<class vec, typename base>
struct valarray_base: std::valarray<base> {
using Base = std::valarray<base>;
Expand Down Expand Up @@ -120,7 +120,7 @@ namespace cp_algo::linalg {
};

template<typename base>
requires(std::is_base_of_v<algebra::modint_base<base>, base>)
requires(std::is_base_of_v<math::modint_base<base>, base>)
struct vec<base>: vec_base<vec<base>, base> {
using Base = vec_base<vec<base>, base>;
using Base::Base;
Expand Down
File renamed without changes.
8 changes: 4 additions & 4 deletions cp-algo/algebra/affine.hpp → cp-algo/math/affine.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#ifndef CP_ALGO_ALGEBRA_AFFINE_HPP
#define CP_ALGO_ALGEBRA_AFFINE_HPP
#ifndef CP_ALGO_MATH_AFFINE_HPP
#define CP_ALGO_MATH_AFFINE_HPP
#include <optional>
#include <utility>
#include <cassert>
#include <tuple>
namespace cp_algo::algebra {
namespace cp_algo::math {
// a * x + b
template<typename base>
struct lin {
Expand Down Expand Up @@ -68,4 +68,4 @@ namespace cp_algo::algebra {
}
};
}
#endif // CP_ALGO_ALGEBRA_AFFINE_HPP
#endif // CP_ALGO_MATH_AFFINE_HPP
6 changes: 3 additions & 3 deletions cp-algo/algebra/all → cp-algo/math/all
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#ifndef CP_ALGO_ALGEBRA_ALL
#define CP_ALGO_ALGEBRA_ALL
#ifndef CP_ALGO_MATH_ALL
#define CP_ALGO_MATH_ALL
#include "modint.hpp"
#include "affine.hpp"
#include "common.hpp"
#include "poly.hpp"
#include "fft.hpp"
#endif // CP_ALGO_ALGEBRA_ALL
#endif // CP_ALGO_MATH_ALL
25 changes: 25 additions & 0 deletions cp-algo/math/combinatorics.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef CP_ALGO_MATH_COMBINATORICS_HPP
#define CP_ALGO_MATH_COMBINATORICS_HPP
#include "common.hpp"
namespace cp_algo::math {
template<typename T>
T binom_large(T n, int r) {
assert(r < math::maxn);
T ans = 1;
for(int i = 0; i < r; i++) {
ans = ans * T(n - i) * small_inv<T>(i + 1);
}
return ans;
}
template<typename T>
T binom(int n, int r) {
if(r < 0 || r > n) {
return T(0);
} else if(n > math::maxn) {
return binom_large(n, r);
} else {
return math::fact<T>(n) * math::rfact<T>(r) * math::rfact<T>(n-r);
}
}
}
#endif // CP_ALGO_MATH_COMBINATORICS_HPP
18 changes: 4 additions & 14 deletions cp-algo/algebra/common.hpp → cp-algo/math/common.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#ifndef CP_ALGO_ALGEBRA_COMMON_HPP
#define CP_ALGO_ALGEBRA_COMMON_HPP
#ifndef CP_ALGO_MATH_COMMON_HPP
#define CP_ALGO_MATH_COMMON_HPP
#include <functional>
#include <cstdint>
namespace cp_algo::algebra {
namespace cp_algo::math {
#ifdef CP_ALGO_MAXN
const int maxn = CP_ALGO_MAXN;
#else
Expand All @@ -29,7 +29,6 @@ namespace cp_algo::algebra {
T bpow(T const& x, int64_t n) {
return bpow(x, n, T(1));
}

template<typename T>
T fact(int n) {
static std::vector<T> F(maxn);
Expand Down Expand Up @@ -68,14 +67,5 @@ namespace cp_algo::algebra {
}
return F[n];
}

template<typename T>
T nCr(int n, int r) {
if(r < 0 || r > n) {
return T(0);
} else {
return fact<T>(n) * rfact<T>(r) * rfact<T>(n-r);
}
}
}
#endif // CP_ALGO_ALGEBRA_COMMON_HPP
#endif // CP_ALGO_MATH_COMMON_HPP
8 changes: 4 additions & 4 deletions cp-algo/algebra/fft.hpp → cp-algo/math/fft.hpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#ifndef CP_ALGO_ALGEBRA_FFT_HPP
#define CP_ALGO_ALGEBRA_FFT_HPP
#ifndef CP_ALGO_MATH_FFT_HPP
#define CP_ALGO_MATH_FFT_HPP
#include "common.hpp"
#include "modint.hpp"
#include <algorithm>
#include <complex>
#include <cassert>
#include <vector>
namespace cp_algo::algebra::fft {
namespace cp_algo::math::fft {
using ftype = double;
using point = std::complex<ftype>;

Expand Down Expand Up @@ -143,4 +143,4 @@ namespace cp_algo::algebra::fft {
}
}
}
#endif // CP_ALGO_ALGEBRA_FFT_HPP
#endif // CP_ALGO_MATH_FFT_HPP
8 changes: 4 additions & 4 deletions cp-algo/algebra/modint.hpp → cp-algo/math/modint.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#ifndef CP_ALGO_ALGEBRA_MODINT_HPP
#define CP_ALGO_ALGEBRA_MODINT_HPP
#ifndef CP_ALGO_MATH_MODINT_HPP
#define CP_ALGO_MATH_MODINT_HPP
#include "common.hpp"
#include <iostream>
namespace cp_algo::algebra {
namespace cp_algo::math {
template<typename modint>
struct modint_base {
static int64_t mod() {
Expand Down Expand Up @@ -102,4 +102,4 @@ namespace cp_algo::algebra {
};
int64_t dynamic_modint::m = 0;
}
#endif // CP_ALGO_ALGEBRA_MODINT_HPP
#endif // CP_ALGO_MATH_MODINT_HPP
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#ifndef CP_ALGO_ALGEBRA_NUMBER_THEORY_HPP
#define CP_ALGO_ALGEBRA_NUMBER_THEORY_HPP
#ifndef CP_ALGO_MATH_NUMBER_THEORY_HPP
#define CP_ALGO_MATH_NUMBER_THEORY_HPP
#include "../random/rng.hpp"
#include "affine.hpp"
#include "modint.hpp"
#include <algorithm>
#include <optional>
#include <vector>
#include <bit>
namespace cp_algo::algebra {
namespace cp_algo::math {
// https://en.wikipedia.org/wiki/Berlekamp-Rabin_algorithm
template<modint_type base>
std::optional<base> sqrt(base b) {
Expand Down Expand Up @@ -115,4 +115,4 @@ namespace cp_algo::algebra {
});
}
}
#endif // CP_ALGO_ALGEBRA_NUMBER_THEORY_HPP
#endif // CP_ALGO_MATH_NUMBER_THEORY_HPP
11 changes: 6 additions & 5 deletions cp-algo/algebra/poly.hpp → cp-algo/math/poly.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#ifndef CP_ALGO_ALGEBRA_POLY_HPP
#define CP_ALGO_ALGEBRA_POLY_HPP
#ifndef CP_ALGO_MATH_POLY_HPP
#define CP_ALGO_MATH_POLY_HPP
#include "poly/impl/euclid.hpp"
#include "poly/impl/base.hpp"
#include "poly/impl/div.hpp"
#include "combinatorics.hpp"
#include "number_theory.hpp"
#include "fft.hpp"
#include <functional>
Expand All @@ -12,7 +13,7 @@
#include <utility>
#include <vector>
#include <list>
namespace cp_algo::algebra {
namespace cp_algo::math {
template<typename T>
struct poly_t {
using base = T;
Expand Down Expand Up @@ -277,7 +278,7 @@ namespace cp_algo::algebra {
auto ans = div_xk(i).sqrt(n - i / 2);
return ans ? ans->mul_xk(i / 2) : ans;
}
auto st = algebra::sqrt((*this)[0]);
auto st = math::sqrt((*this)[0]);
if(st) {
poly_t ans = *st;
size_t a = 1;
Expand Down Expand Up @@ -659,4 +660,4 @@ namespace cp_algo::algebra {
return b * a;
}
};
#endif // CP_ALGO_ALGEBRA_POLY_HPP
#endif // CP_ALGO_MATH_POLY_HPP
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#ifndef CP_ALGO_ALGEBRA_POLY_IMPL_BASE_HPP
#define CP_ALGO_ALGEBRA_POLY_IMPL_BASE_HPP
#ifndef CP_ALGO_MATH_POLY_IMPL_BASE_HPP
#define CP_ALGO_MATH_POLY_IMPL_BASE_HPP
#include <functional>
#include <algorithm>
#include <iostream>
// really basic operations, typically taking O(n)
namespace cp_algo::algebra::poly::impl {
namespace cp_algo::math::poly::impl {
void normalize(auto& p) {
while(p.deg() >= 0 && p.lead() == 0) {
p.a.pop_back();
Expand Down Expand Up @@ -64,4 +64,4 @@ namespace cp_algo::algebra::poly::impl {
return p;
}
}
#endif // CP_ALGO_ALGEBRA_POLY_IMPL_BASE_HPP
#endif // CP_ALGO_MATH_POLY_IMPL_BASE_HPP
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#ifndef CP_ALGO_ALGEBRA_POLY_IMPL_DIV_HPP
#define CP_ALGO_ALGEBRA_POLY_IMPL_DIV_HPP
#ifndef CP_ALGO_MATH_POLY_IMPL_DIV_HPP
#define CP_ALGO_MATH_POLY_IMPL_DIV_HPP
#include "../../fft.hpp"
#include "../../common.hpp"
#include <cassert>
// operations related to polynomial division
namespace cp_algo::algebra::poly::impl {
namespace cp_algo::math::poly::impl {
auto divmod_slow(auto const& p, auto const& q) {
auto R = p;
auto D = decltype(p){};
Expand Down Expand Up @@ -113,4 +113,4 @@ namespace cp_algo::algebra::poly::impl {
).mod_xk(n);
}
}
#endif // CP_ALGO_ALGEBRA_POLY_IMPL_DIV_HPP
#endif // CP_ALGO_MATH_POLY_IMPL_DIV_HPP
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef CP_ALGO_ALGEBRA_POLY_IMPL_EUCLID_HPP
#define CP_ALGO_ALGEBRA_POLY_IMPL_EUCLID_HPP
#ifndef CP_ALGO_MATH_POLY_IMPL_EUCLID_HPP
#define CP_ALGO_MATH_POLY_IMPL_EUCLID_HPP
#include "../../affine.hpp"
#include "../../fft.hpp"
#include <functional>
Expand All @@ -10,7 +10,7 @@
#include <tuple>
#include <list>
// operations related to gcd and Euclidean algo
namespace cp_algo::algebra::poly::impl {
namespace cp_algo::math::poly::impl {
template<typename poly>
using gcd_result = std::pair<
std::list<std::decay_t<poly>>,
Expand Down Expand Up @@ -99,4 +99,4 @@ namespace cp_algo::algebra::poly::impl {
return Tr.b / q[0];
}
}
#endif // CP_ALGO_ALGEBRA_POLY_IMPL_EUCLID_HPP
#endif // CP_ALGO_MATH_POLY_IMPL_EUCLID_HPP
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
#define PROBLEM "https://judge.yosupo.jp/problem/range_affine_range_sum"
#include "cp-algo/data_structures/segtree/metas/affine.hpp"
#include "cp-algo/data_structures/segtree.hpp"
#include "cp-algo/algebra/modint.hpp"
#include "cp-algo/math/modint.hpp"
#include <bits/stdc++.h>

using namespace std;
using namespace cp_algo::data_structures;

using base = cp_algo::algebra::modint<998244353>;
using base = cp_algo::math::modint<998244353>;
using meta = segtree::metas::affine_meta<base>;

void solve() {
Expand Down
Loading

0 comments on commit 9e9cfe2

Please sign in to comment.