maomao90's Library
A C++20 library for competitive programming.
Loading...
Searching...
No Matches
multiset_hash.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4
7
8namespace maomao90 {
9using namespace std;
10template <StaticModInt modint = static_modint<(1ll << 61) - 1>,
11 size_t num_bases = 1, size_t CACHE = 1'000'000>
13 using mint = modint;
14
15 constexpr array<typename mint::umod_type, num_bases> get_v() const {
16 array<typename mint::umod_type, num_bases> res;
17 for (int i = 0; i < num_bases; i++) {
18 res[i] = v[i].val();
19 }
20 return res;
21 }
22 // insert `cnt` copies of `a` into multiset
23 constexpr MultisetHash insert(long long a, long long cnt = 1) const {
24 MultisetHash res = *this;
25 for (int i = 0; i < num_bases; i++) {
26 res.v[i] += get_pow(i, a) * cnt;
27 }
28 return res;
29 }
30 // erase `cnt` copies of `a` into multiset
31 constexpr MultisetHash erase(long long a, long long cnt = 1) const {
32 MultisetHash res = *this;
33 for (int i = 0; i < num_bases; i++) {
34 res.v[i] -= get_pow(i, a) * cnt;
35 }
36 return res;
37 }
38 // increases all values in multiset by `delta`
39 // (`delta` can be negative)
40 constexpr MultisetHash offset(long long delta) const {
41 MultisetHash res = *this;
42 for (int i = 0; i < num_bases; i++) {
43 res.v[i] *= get_pow(i, delta);
44 }
45 return res;
46 }
47 constexpr MultisetHash operator+(const MultisetHash &o) const {
48 MultisetHash res = *this;
49 return res += o;
50 }
51 constexpr MultisetHash &operator+=(const MultisetHash &o) {
52 for (int i = 0; i < num_bases; i++) {
53 v[i] += o.v[i];
54 }
55 return *this;
56 }
57 constexpr MultisetHash operator-(const MultisetHash &o) const {
58 MultisetHash res = *this;
59 return res -= o;
60 }
61 constexpr MultisetHash &operator-=(const MultisetHash &o) {
62 for (int i = 0; i < num_bases; i++) {
63 v[i] -= o.v[i];
64 }
65 return *this;
66 }
67 constexpr bool operator==(const MultisetHash &o) const {
68 for (int i = 0; i < num_bases; i++) {
69 if (v[i] != o.v[i]) {
70 return false;
71 }
72 }
73 return true;
74 }
75 constexpr bool operator!=(const MultisetHash &o) const {
76 return !(*this == o);
77 }
78
79private:
80 array<mint, num_bases> v;
81 inline static const array<mint, num_bases>
84 inline static const array<array<mint, CACHE>, num_bases>
86 inv_bases_pow =
88 constexpr mint get_pow(int i, long long p) const {
89 assert(i < num_bases);
90 if (abs(p) < CACHE) {
91 return p >= 0 ? bases_pow[i][p] : inv_bases_pow[i][-p];
92 } else {
93 return p >= 0 ? bases[i].pow(p) : inv_bases[i].pow(-p);
94 }
95 }
96};
97} // namespace maomao90
Definition modint.hpp:23
constexpr array< mint, num_bases > gen_inverse(const array< mint, num_bases > &bases)
Definition hashing.hpp:25
constexpr array< mint, num_bases > gen_bases()
Definition hashing.hpp:16
constexpr array< array< mint, CACHE >, num_bases > init_power(const array< mint, num_bases > &bases)
Definition hashing.hpp:34
Definition hashmap.hpp:8
Definition multiset_hash.hpp:12
constexpr MultisetHash insert(long long a, long long cnt=1) const
Definition multiset_hash.hpp:23
modint mint
Definition multiset_hash.hpp:13
constexpr MultisetHash offset(long long delta) const
Definition multiset_hash.hpp:40
constexpr bool operator!=(const MultisetHash &o) const
Definition multiset_hash.hpp:75
constexpr bool operator==(const MultisetHash &o) const
Definition multiset_hash.hpp:67
constexpr MultisetHash erase(long long a, long long cnt=1) const
Definition multiset_hash.hpp:31
constexpr MultisetHash operator-(const MultisetHash &o) const
Definition multiset_hash.hpp:57
constexpr MultisetHash operator+(const MultisetHash &o) const
Definition multiset_hash.hpp:47
constexpr MultisetHash & operator+=(const MultisetHash &o)
Definition multiset_hash.hpp:51
constexpr array< typename mint::umod_type, num_bases > get_v() const
Definition multiset_hash.hpp:15
constexpr MultisetHash & operator-=(const MultisetHash &o)
Definition multiset_hash.hpp:61
Definition modint.hpp:28