Advertisement
newb_ie

some useful libraries

Apr 6th, 2022
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. template<typename T>
  5. struct Fenwick {
  6. std::vector<T> data;
  7. int len;
  8. Fenwick(int _len) : data(_len + 1), len(_len + 1) {}
  9. void add(int idx, T val) {
  10. for (int i = idx; i < len; i += i & -i) data[i] += val;
  11. }
  12. T get(int idx) { // [1, idx]
  13. T res = 0;
  14. for (int i = idx; i > 0; i -= i & -i) res += data[i];
  15. return res;
  16. }
  17. T get(int left, int right) { // [left, right]
  18. return get(right) - get(left - 1);
  19. }
  20. }; // struct Fenwick
  21. using ll=long long;
  22.  
  23. // https://github.com/ecnerwala/cp-book/blob/master/src/modnum.hpp
  24. template <typename T> T mod_inv_in_range(T a, T m) {
  25. assert(0 <= a && a < m);
  26. T x = a, y = m;
  27. T vx = 1, vy = 0;
  28. while (x) {
  29. T k = y / x;
  30. y %= x;
  31. vy -= k * vx;
  32. std::swap(x, y);
  33. std::swap(vx, vy);
  34. }
  35. assert(y == 1);
  36. return vy < 0 ? m + vy : vy;
  37. }
  38.  
  39. template <typename T> T mod_inv(T a, T m) {
  40. a %= m;
  41. a = a < 0 ? a + m : a;
  42. return mod_inv_in_range(a, m);
  43. }
  44.  
  45. template <int MOD_> struct modnum {
  46. static constexpr int MOD = MOD_;
  47. static_assert(MOD_ > 0, "MOD must be positive");
  48.  
  49. int v;
  50.  
  51. modnum() : v(0) {}
  52. modnum(long long v_) : v(int(v_ % MOD)) { if (v < 0) v += MOD; }
  53. explicit operator int() const { return v; }
  54. friend std::ostream& operator << (std::ostream& out, const modnum& n) { return out << int(n); }
  55. friend std::istream& operator >> (std::istream& in, modnum& n) { long long v_; in >> v_; n = modnum(v_); return in; }
  56.  
  57.  
  58. friend bool operator == (const modnum& a, const modnum& b) { return a.v == b.v; }
  59. friend bool operator != (const modnum& a, const modnum& b) { return a.v != b.v; }
  60.  
  61. modnum inv() const {
  62. modnum res;
  63. res.v = mod_inv_in_range(v, MOD);
  64. return res;
  65. }
  66. friend modnum inv(const modnum& m) { return m.inv(); }
  67. modnum neg() const {
  68. modnum res;
  69. res.v = v ? MOD-v : 0;
  70. return res;
  71. }
  72. friend modnum neg(const modnum& m) { return m.neg(); }
  73.  
  74. modnum operator- () const {
  75. return neg();
  76. }
  77. modnum operator+ () const {
  78. return modnum(*this);
  79. }
  80.  
  81. modnum& operator ++ () {
  82. v ++;
  83. if (v == MOD) v = 0;
  84. return *this;
  85. }
  86. modnum& operator -- () {
  87. if (v == 0) v = MOD;
  88. v --;
  89. return *this;
  90. }
  91. modnum& operator += (const modnum& o) {
  92. v -= MOD-o.v;
  93. v = (v < 0) ? v + MOD : v;
  94. return *this;
  95. }
  96. modnum& operator -= (const modnum& o) {
  97. v -= o.v;
  98. v = (v < 0) ? v + MOD : v;
  99. return *this;
  100. }
  101. modnum& operator *= (const modnum& o) {
  102. //v = int(ll(v) * ll(o.v) % MOD);
  103. v = (int)((long long)v * (long long)o.v % MOD);
  104. return *this;
  105. }
  106. modnum& operator /= (const modnum& o) {
  107. return *this *= o.inv();
  108. }
  109.  
  110. friend modnum operator ++ (modnum& a, int) { modnum r = a; ++a; return r; }
  111. friend modnum operator -- (modnum& a, int) { modnum r = a; --a; return r; }
  112. friend modnum operator + (const modnum& a, const modnum& b) { return modnum(a) += b; }
  113. friend modnum operator - (const modnum& a, const modnum& b) { return modnum(a) -= b; }
  114. friend modnum operator * (const modnum& a, const modnum& b) { return modnum(a) *= b; }
  115. friend modnum operator / (const modnum& a, const modnum& b) { return modnum(a) /= b; }
  116. };
  117.  
  118. template <typename T> T modnum_pow(T a, long long b) {
  119. assert(b >= 0);
  120. T r = 1; while (b) { if (b & 1) r *= a; b >>= 1; a *= a; } return r;
  121. }
  122.  
  123. using mint = modnum<998244353>;
  124.  
  125. std::vector<mint> facts, ifacts;
  126.  
  127. // all factorials up to and including n, mod m
  128. void gen_facts(int n) {
  129. if (!facts.empty()) return;
  130. facts.resize(n + 1);
  131. facts[0] = 1;
  132. for (int i = 1; i <= n; i++) facts[i] = facts[i - 1] * i;
  133. ifacts.resize(n + 1);
  134. ifacts[n] = facts[n].inv();
  135. for (int i = n; i > 0; i--) ifacts[i - 1] = ifacts[i] * i;
  136. }
  137.  
  138. // n permute k, mod m
  139. mint perm(int n, int k) {
  140. //if (n >= (int)facts.size()) throw runtime_error("call gen_facts before calling perm");
  141. //assert(n >= k);
  142. return facts[n] * ifacts[n - k];
  143. }
  144.  
  145. // n choose k, mod m
  146. mint choose(int n, int k) {
  147. return perm(n, k) * ifacts[k];
  148. }
  149.  
  150. // nth catalan number, mod m
  151. mint catalan(int n) {
  152. return choose(2 * n, n) - choose(2 * n, n + 1);
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement