ac-library-rb 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (84) hide show
  1. checksums.yaml +7 -0
  2. data/.github/workflows/unittest.yml +16 -0
  3. data/.gitignore +11 -0
  4. data/.rubocop.yml +198 -0
  5. data/Gemfile +3 -0
  6. data/LICENSE +116 -0
  7. data/README.ja.md +56 -0
  8. data/README.md +41 -0
  9. data/Rakefile +11 -0
  10. data/ac-library-rb.gemspec +32 -0
  11. data/bin/console +8 -0
  12. data/bin/lock_lib.rb +27 -0
  13. data/bin/setup +8 -0
  14. data/document_en/binary_index_tree.md +3 -0
  15. data/document_en/convolution.md +67 -0
  16. data/document_en/dsu.md +132 -0
  17. data/document_en/fenwick_tree.md +99 -0
  18. data/document_en/index.md +79 -0
  19. data/document_en/lazy_segtree.md +141 -0
  20. data/document_en/math.md +104 -0
  21. data/document_en/max_flow.md +165 -0
  22. data/document_en/min_cost_flow.md +132 -0
  23. data/document_en/modint.md +263 -0
  24. data/document_en/priority_queue.md +119 -0
  25. data/document_en/segtree.md +134 -0
  26. data/document_en/string.md +106 -0
  27. data/document_en/two_sat.md +91 -0
  28. data/document_en/union_find.md +3 -0
  29. data/document_ja/convolution.md +64 -0
  30. data/document_ja/dsu.md +183 -0
  31. data/document_ja/fenwick_tree.md +83 -0
  32. data/document_ja/index.md +89 -0
  33. data/document_ja/lazy_segtree.md +135 -0
  34. data/document_ja/math.md +116 -0
  35. data/document_ja/max_flow.md +129 -0
  36. data/document_ja/min_cost_flow.md +105 -0
  37. data/document_ja/modint.md +349 -0
  38. data/document_ja/priority_queue.md +103 -0
  39. data/document_ja/scc.md +65 -0
  40. data/document_ja/segtree.md +145 -0
  41. data/document_ja/string.md +105 -0
  42. data/document_ja/two_sat.md +87 -0
  43. data/lib/ac-library-rb/version.rb +3 -0
  44. data/lib/convolution.rb +124 -0
  45. data/lib/core_ext/modint.rb +19 -0
  46. data/lib/crt.rb +52 -0
  47. data/lib/dsu.rb +44 -0
  48. data/lib/fenwick_tree.rb +48 -0
  49. data/lib/floor_sum.rb +21 -0
  50. data/lib/inv_mod.rb +26 -0
  51. data/lib/lazy_segtree.rb +149 -0
  52. data/lib/lcp_array.rb +23 -0
  53. data/lib/max_flow.rb +137 -0
  54. data/lib/min_cost_flow.rb +143 -0
  55. data/lib/modint.rb +170 -0
  56. data/lib/pow_mod.rb +13 -0
  57. data/lib/priority_queue.rb +89 -0
  58. data/lib/scc.rb +77 -0
  59. data/lib/segtree.rb +140 -0
  60. data/lib/suffix_array.rb +128 -0
  61. data/lib/two_sat.rb +34 -0
  62. data/lib/z_algorithm.rb +32 -0
  63. data/lib_helpers/ac-library-rb/all.rb +22 -0
  64. data/lib_lock/ac-library-rb.rb +22 -0
  65. data/lib_lock/ac-library-rb/convolution.rb +126 -0
  66. data/lib_lock/ac-library-rb/core_ext/modint.rb +19 -0
  67. data/lib_lock/ac-library-rb/crt.rb +54 -0
  68. data/lib_lock/ac-library-rb/dsu.rb +46 -0
  69. data/lib_lock/ac-library-rb/fenwick_tree.rb +50 -0
  70. data/lib_lock/ac-library-rb/floor_sum.rb +23 -0
  71. data/lib_lock/ac-library-rb/inv_mod.rb +28 -0
  72. data/lib_lock/ac-library-rb/lazy_segtree.rb +151 -0
  73. data/lib_lock/ac-library-rb/lcp_array.rb +25 -0
  74. data/lib_lock/ac-library-rb/max_flow.rb +139 -0
  75. data/lib_lock/ac-library-rb/min_cost_flow.rb +145 -0
  76. data/lib_lock/ac-library-rb/modint.rb +172 -0
  77. data/lib_lock/ac-library-rb/pow_mod.rb +15 -0
  78. data/lib_lock/ac-library-rb/priority_queue.rb +91 -0
  79. data/lib_lock/ac-library-rb/scc.rb +79 -0
  80. data/lib_lock/ac-library-rb/segtree.rb +142 -0
  81. data/lib_lock/ac-library-rb/suffix_array.rb +130 -0
  82. data/lib_lock/ac-library-rb/two_sat.rb +36 -0
  83. data/lib_lock/ac-library-rb/z_algorithm.rb +34 -0
  84. metadata +158 -0
@@ -0,0 +1,83 @@
1
+ # Fenwick Tree
2
+
3
+ **別名** BIT(Binary Indexed Tree)
4
+
5
+ 長さ `N` の配列に対し、
6
+
7
+ - 要素の `1` 点変更
8
+ - 区間の要素の総和
9
+
10
+ を `O(logN)` で求めることが出来るデータ構造です。
11
+
12
+ ## 特異メソッド
13
+
14
+ ### new(n) -> FenwickTree
15
+ ### new(ary) -> FenwickTree
16
+
17
+ ```rb
18
+ fw = FenwickTree.new(arg)
19
+ ```
20
+
21
+ 引数は、`Integer` または `Array` です。
22
+
23
+ 1. 引数が `Integer` クラスの `n` のとき、長さ `n` の全ての要素が `0` で初期化された配列を作ります。
24
+ 2. 引数が長さ `n` の `Array` クラスの配列 `a` のとき、`a` で初期化された配列を作ります。
25
+
26
+ 配列の添字は、0-based indexです。
27
+
28
+ **計算量**
29
+
30
+ `O(n)` (引数が`Array`でも同じです)
31
+
32
+ ## add(pos, x)
33
+
34
+ ```rb
35
+ fw.add(pos, x)
36
+ ```
37
+
38
+ `a[pos] += x`を行います。
39
+
40
+ `pos`は、0-based indexです。
41
+
42
+ **制約** `0 ≦ pos < n`
43
+ **計算量** `O(logn)`
44
+
45
+ ## sum(l, r) ->Integer
46
+
47
+ ```rb
48
+ fw.sum(l, r)
49
+ ```
50
+
51
+ `a[l] + a[l - 1] + ... + a[r - 1]`を返します。
52
+
53
+ 引数は、半開区間です。
54
+
55
+ 実装として、内部で`_sum(r) - _sum(l)`を返しています。
56
+
57
+ **制約** `0 ≦ l ≦ r ≦ n`
58
+ **計算量** `O(logn)`
59
+
60
+ ## _sum(pos) -> Integer
61
+
62
+ ```rb
63
+ fw._sum(pos)
64
+ ```
65
+
66
+ `a[0] + a[1] + ... + a[pos - 1]`を返します。
67
+
68
+ **計算量** `O(logn)`
69
+
70
+ ## Verified
71
+
72
+ - [AtCoder ALPC B \- Fenwick Tree](https://atcoder.jp/contests/practice2/tasks/practice2_b)
73
+ [ACコード(17074108)]https://atcoder.jp/contests/practice2/submissions/17074108 (1272ms)
74
+ - [F \- Range Xor Query](https://atcoder.jp/contests/abc185/tasks/abc185_f)
75
+ FenwickTree(BIT)をxorに改造するだけでも解けます。
76
+ [ACコード(821ms)](https://atcoder.jp/contests/abc185/submissions/18769200)。FenwickTree(BIT)のxor改造版です。
77
+
78
+
79
+ ## 開発者、内部実装を読みたい人向け
80
+
81
+ 本家ACLライブラリの実装は、内部の配列も0-based indexですが、
82
+
83
+ 本ライブラリは定数倍改善のため、内部の配列への添字が1-based indexです。
@@ -0,0 +1,89 @@
1
+ # index
2
+
3
+ `lib`ディレクトリにコードがあります。
4
+
5
+ 現状、この中から探してもらって、コピペして使って下さい。
6
+
7
+ 左の列から、コード本体、ドキュメントです。
8
+
9
+ (ドキュメントのGはGitHubにあるという意味で、HackMDに移す計画があるためです)
10
+
11
+ | C | D | |
12
+ | :--- | :--- | --- |
13
+ | [◎](https://github.com/universato/ac-library-rb/blob/master/lib/fenwick_tree.rb) | [◎G](https://github.com/universato/ac-library-rb/blob/master/document_ja/fenwick_tree.md) |FenwickTree(BIT)|
14
+ | [◎](https://github.com/universato/ac-library-rb/blob/master/lib/segtree.rb) | [◎G](https://github.com/universato/ac-library-rb/blob/master/document_ja/segtree.md) |Segtree|
15
+ | [○](https://github.com/universato/ac-library-rb/blob/master/lib/lazy_segtree.rb) | [○G](https://github.com/universato/ac-library-rb/blob/master/document_ja/lazy_segtree.md) |LazySegtree|
16
+ | [◎](https://github.com/universato/ac-library-rb/blob/master/lib/priority_queue.rb) | [◎G](https://github.com/universato/ac-library-rb/blob/master/document_ja/priority_queue.md) |PriorityQueue|
17
+ | [◎](https://github.com/universato/ac-library-rb/blob/master/lib/suffix_array.rb) | [◎G](https://github.com/universato/ac-library-rb/blob/master/document_ja/string.md) |suffix_array|
18
+ | [◎](https://github.com/universato/ac-library-rb/blob/master/lib/lcp_array.rb) | [◎G](https://github.com/universato/ac-library-rb/blob/master/document_ja/string.md) |lcp_array|
19
+ | [◎](https://github.com/universato/ac-library-rb/blob/master/lib/z_algorithm.rb) | [◎G](https://github.com/universato/ac-library-rb/blob/master/document_ja/string.md) |z_algorithm|
20
+ | [◎](https://github.com/universato/ac-library-rb/blob/master/lib/pow_mod.rb) | [◎G](https://github.com/universato/ac-library-rb/blob/master/document_ja/math.md) |pow_mod|
21
+ | [◎](https://github.com/universato/ac-library-rb/blob/master/lib/inv_mod.rb) | [◎G](https://github.com/universato/ac-library-rb/blob/master/document_ja/math.md) |inv_mod|
22
+ | [◎](https://github.com/universato/ac-library-rb/blob/master/lib/crt.rb) | [◎G](https://github.com/universato/ac-library-rb/blob/master/document_ja/math.md) |crt(中国剰余定理)|
23
+ | [◎](https://github.com/universato/ac-library-rb/blob/master/lib/floor_sum.rb) | [◎G](https://github.com/universato/ac-library-rb/blob/master/document_ja/math.md) |floor_sum|
24
+ | [◎](https://github.com/universato/ac-library-rb/blob/master/lib/convolution.rb) | [◎G](https://github.com/universato/ac-library-rb/blob/master/document_ja/convolution.md) |convolution|
25
+ | [◎](https://github.com/universato/ac-library-rb/blob/master/lib/modint.rb) | [◎G](https://github.com/universato/ac-library-rb/blob/master/document_ja/modint.md) |ModInt|
26
+ | [◎](https://github.com/universato/ac-library-rb/blob/master/lib/dsu.rb) | [◎G](https://github.com/universato/ac-library-rb/blob/master/document_ja/dsu.md) |DSU(UnionFind)|
27
+ | [◎](https://github.com/universato/ac-library-rb/blob/master/lib/max_flow.rb) | [◎G](https://github.com/universato/ac-library-rb/blob/master/document_ja/max_flow.md) |MaxFlow|
28
+ | [◎](https://github.com/universato/ac-library-rb/blob/master/lib/min_cost_flow.rb) |[◎G](https://github.com/universato/ac-library-rb/blob/master/document_ja/min_cost_flow.md) |MinCostFlow|
29
+ | [◎](https://github.com/universato/ac-library-rb/blob/master/lib/scc.rb) | [◎G](https://github.com/universato/ac-library-rb/blob/master/document_ja/scc.md) |SCC(強連結成分)|
30
+ | [◎](https://github.com/universato/ac-library-rb/blob/master/lib/two_sat.rb) | [◎G](https://github.com/universato/ac-library-rb/blob/master/document_ja/two_sat.md) |TwoSat|
31
+
32
+ ## 実装コードへのリンク(GitHub)
33
+
34
+ 上の表の別形式です。
35
+
36
+ ### データ構造
37
+
38
+ - [fenwick_tree.rb](https://github.com/universato/ac-library-rb/blob/master/lib/fenwick_tree.rb)
39
+ - [segtree.rb](https://github.com/universato/ac-library-rb/blob/master/lib/segtree.rb)
40
+ - [lazy_segtree.rb](https://github.com/universato/ac-library-rb/blob/master/lib/lazy_segtree.rb)
41
+ - [priority_queue.rb](https://github.com/universato/ac-library-rb/blob/master/lib/priority_queue.rb)
42
+ - String
43
+ - [suffix_array.rb](https://github.com/universato/ac-library-rb/blob/master/lib/suffix_array.rb)
44
+ - [lcp_array.rb](https://github.com/universato/ac-library-rb/blob/master/lib/lcp_array.rb)
45
+ - [z_algorithm.rb](https://github.com/universato/ac-library-rb/blob/master/lib/z_algorithm.rb)
46
+
47
+ ### 数学
48
+
49
+ - math
50
+ - [pow_mod.rb](https://github.com/universato/ac-library-rb/blob/master/lib/pow_mod.rb)
51
+ - [inv_mod.rb](https://github.com/universato/ac-library-rb/blob/master/lib/inv_mod.rb)
52
+ - [crt.rb](https://github.com/universato/ac-library-rb/blob/master/lib/crt.rb)
53
+ - [floor_sum.rb](https://github.com/universato/ac-library-rb/blob/master/lib/floor_sum.rb)
54
+ - [convolution.rb](https://github.com/universato/ac-library-rb/blob/master/lib/convolution.rb)
55
+ - [modint.rb](https://github.com/universato/ac-library-rb/blob/master/lib/modint.rb)
56
+
57
+ ### グラフ
58
+
59
+ - [dsu.rb](https://github.com/universato/ac-library-rb/blob/master/lib/dsu.rb)
60
+ - [max_flow.rb](https://github.com/universato/ac-library-rb/blob/master/lib/max_flow.rb)
61
+ - [min_cost_flow.rb](https://github.com/universato/ac-library-rb/blob/master/lib/min_cost_flow.rb)
62
+ - [scc.rb](https://github.com/universato/ac-library-rb/blob/master/lib/scc.rb)
63
+ - [two_sat.rb](https://github.com/universato/ac-library-rb/blob/master/lib/two_sat.rb)
64
+
65
+ ## アルファベット順(辞書順)
66
+
67
+ <details>
68
+ <summary>アルファベット順(辞書順)の一覧</summary>
69
+
70
+ [convolution.rb](https://github.com/universato/ac-library-rb/blob/master/lib/convolution.rb)
71
+ [crt.rb](https://github.com/universato/ac-library-rb/blob/master/lib/crt.rb)
72
+ [dsu.rb](https://github.com/universato/ac-library-rb/blob/master/lib/dsu.rb)
73
+ [fenwick_tree.rb](https://github.com/universato/ac-library-rb/blob/master/lib/fenwick_tree.rb)
74
+ [floor_sum.rb](https://github.com/universato/ac-library-rb/blob/master/lib/floor_sum..rb)
75
+ [inv_mod.rb](https://github.com/universato/ac-library-rb/blob/master/lib/inv_mod.rb)
76
+ [lazy_segtree.rb](https://github.com/universato/ac-library-rb/blob/master/lib/lazy_segtree.rb)
77
+ [lcp_array.rb](https://github.com/universato/ac-library-rb/blob/master/lib/lcp_array.rb)
78
+ [max_flow.rb](https://github.com/universato/ac-library-rb/blob/master/lib/max_flow.rb)
79
+ [min_cost_flow.rb](https://github.com/universato/ac-library-rb/blob/master/lib/min_cost_flow.rb)
80
+ [modint.rb](https://github.com/universato/ac-library-rb/blob/master/lib/modint.rb)
81
+ [pow_mod.rb](https://github.com/universato/ac-library-rb/blob/master/lib/pow_mod.rb)
82
+ [priority_queue.rb](https://github.com/universato/ac-library-rb/blob/master/lib/priority_queue.rb)
83
+ [scc.rb](https://github.com/universato/ac-library-rb/blob/master/lib/scc.rb)
84
+ [segtree.rb](https://github.com/universato/ac-library-rb/blob/master/lib/segtree.rb)
85
+ [suffix_array.rb](https://github.com/universato/ac-library-rb/blob/master/lib/suffix_array.rb)
86
+ [two_sat.rb](https://github.com/universato/ac-library-rb/blob/master/lib/two_sat.rb)
87
+ [z_algorithm.rb](https://github.com/universato/ac-library-rb/blob/master/lib/z_algorithm.rb)
88
+
89
+ </details>
@@ -0,0 +1,135 @@
1
+ # LazySegtree
2
+
3
+ 遅延評価セグメントツリーです。
4
+
5
+ この命名には批判があって、遅延伝播セグメントツリーの方が良いという意見も根強いです。
6
+
7
+ 宗教戦争を避けるために、遅延セグ木と呼ぶのがいいかもしれません。
8
+
9
+ ## 特異メソッド
10
+
11
+ ### new(v, e, id, op, mapping, composition)
12
+
13
+ ```ruby
14
+ seg = LazySegtree.new(v, e, id, op, mapping, compositon)
15
+ ```
16
+
17
+ 第1引数は、`Integer`または`Array`です。
18
+
19
+ - 第1引数が`Integer`の`n`のとき、長さ`n`・初期値`e`のセグメント木を作ります。
20
+ - 第1引数が長さ`n`の`Array`の`a`のとき、`a`をもとにセグメント木を作ります。
21
+
22
+ 第2引数`e`は、単位元です。ブロックで二項演算`op(x, y)`を定義することで、モノイドを定義する必要があります。
23
+
24
+ **計算量** `O(n)`
25
+
26
+ ## インスタンスメソッド
27
+
28
+ ### set(pos, x)
29
+
30
+ ```ruby
31
+ seg.set(pos, x)
32
+ ```
33
+
34
+ `a[pos]`に`x`を代入します。
35
+
36
+ **計算量** `O(logn)`
37
+
38
+
39
+ ### get(pos) -> 単位元eと同じクラス
40
+
41
+ ```ruby
42
+ seg.get(pos)
43
+ ```
44
+
45
+ `a[pos]`を返します。
46
+
47
+ **計算量** `O(1)`
48
+
49
+
50
+ ### prod(l, r) -> 単位元eと同じクラス
51
+
52
+ ```ruby
53
+ seg.prod(l, r)
54
+ ```
55
+
56
+ `op(a[l], ..., a[r - 1])` を返します。
57
+
58
+ **制約** `0 ≦ l ≦ r ≦ n`
59
+
60
+ **計算量** `O(logn)`
61
+
62
+ ### all_prod -> 単位元eと同じクラス
63
+
64
+ ```ruby
65
+ seg.all_prod
66
+ ```
67
+
68
+ `op(a[0], ..., a[n - 1])` を返します。
69
+
70
+ **計算量** `O(1)`
71
+
72
+ ### apply(pos, val)
73
+
74
+ ```ruby
75
+ seg.apply(pos, val)
76
+ ```
77
+
78
+ 本家コードで引数が3つのとき実装について、当ライブラリでは`range_apply`という名称で実装しています。実行時間を計測して問題なさそうなら、`apply`メソッドに2引数と3引数の両方に対応できるようにします。
79
+
80
+ **制約** `0 ≦ pos < n`
81
+
82
+ **計算量** `O(log n)`
83
+
84
+ ### range_apply(l, r, val)
85
+
86
+ ```ruby
87
+ seg.range_apply(l, r, val)
88
+ ```
89
+
90
+ **制約** `0 ≦ l ≦ r ≦ n`
91
+
92
+ **計算量** `O(log n)`
93
+
94
+ ### max_right
95
+
96
+ [TODO] 暇な誰かが書く。
97
+
98
+ ### min_left
99
+
100
+ [TODO] コード側の実装ができていないです。
101
+
102
+ ## Verified
103
+
104
+ 問題のリンクです。コードはないですが、Verified済みです。
105
+ - [AIZU ONLINE JUDGE DSL\_2\_F RMQ and RUQ](http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_2_F)
106
+ - [AIZU ONLINE JUDGE DSL\_2\_G RSQ and RAQ](http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_2_G)
107
+
108
+ 以下の問題は、Rubyでは実行時間が厳しくTLEになりACできてないです。
109
+ - [ALPC: K \- Range Affine Range Sum](https://atcoder.jp/contests/practice2/tasks/practice2_k)
110
+ - [ALPC: L \- Lazy Segment Tree](https://atcoder.jp/contests/practice2/tasks/practice2_l)
111
+
112
+ ## 参考リンク
113
+
114
+ - 当ライブラリ
115
+ - [当ライブラリの実装コード lazy_segtree.rb](https://github.com/universato/ac-library-rb/blob/master/lib/lazy_segtree.rb)
116
+ - [当ライブラリのテストコード lazy_segtree_test.rb](https://github.com/universato/ac-library-rb/blob/master/test/lazy_segtree_test.rb)
117
+ - 本家ライブラリ
118
+ - [本家ライブラリのドキュメント lazysegtree.md(GitHub)](https://github.com/atcoder/ac-library/blob/master/document_ja/lazysegtree.md)
119
+ - [本家のドキュメント appendix.md(GitHub)](https://github.com/atcoder/ac-library/blob/master/document_ja/appendix.md)
120
+ - [本家ライブラリの実装コード lazysegtree.hpp(GitHub)](https://github.com/atcoder/ac-library/blob/master/atcoder/lazysegtree.hpp)
121
+ - [本家ライブラリのテストコード lazysegtree_test.cpp(GitHub)](https://github.com/atcoder/ac-library/blob/master/test/unittest/lazysegtree_test.cpp)
122
+ - セグメントツリーについて
123
+ - [セグメント木をソラで書きたいあなたに \- hogecoder](https://tsutaj.hatenablog.com/entry/2017/03/29/204841)
124
+ - [遅延評価セグメント木をソラで書きたいあなたに \- hogecoder](https://tsutaj.hatenablog.com/entry/2017/03/30/224339)
125
+ - AtCooderLibrary(ACL)のLazySegtreeについて
126
+ - [使い方 \- ARMERIA 2020/9/22](https://betrue12.hateblo.jp/entry/2020/09/22/194541)
127
+ - [チートシート \- ARMERIA 2020/9/23](https://betrue12.hateblo.jp/entry/2020/09/23/005940)
128
+ - [ACLPC: K–Range Affine Range Sumの解説 \|optのブログ 2020/9/27](https://opt-cp.com/cp/lazysegtree-aclpc-k/)
129
+ - [ACL 基礎実装例集 \- buyoh\.hateblo\.jp 2020/9/27](https://buyoh.hateblo.jp/entry/2020/09/27/190144)
130
+
131
+ ## 本家ライブラリとの違い
132
+
133
+ ### `ceil_pow2`ではなく、`bit_length`
134
+
135
+ 本家C++ライブラリは独自定義の`internal::ceil_pow2`関数を用いてますが、本ライブラリではRuby既存のメソッド`Integer#bit_length`を用いています。そちらの方が計測した結果、高速でした。
@@ -0,0 +1,116 @@
1
+ # Math
2
+
3
+ 数学的アルゴリズムの詰め合わせです。
4
+
5
+ - `pow_mod`
6
+ - `inv_mod`
7
+ - `crt`
8
+ - `floor_sum`
9
+
10
+ ## pow_mod
11
+
12
+ ```ruby
13
+ pow_mod(x, n, m)
14
+ ```
15
+
16
+ `(x**n) % m` を返します。
17
+
18
+ Rubyには、もともと `Integer#pow` があるため、そちらを利用した方がいいです。
19
+
20
+ 基本的に、`pow_mod(x, n, m)` は、 `x.pow(n, m)`と等しいです。
21
+
22
+ ただ、Ruby 2.7.1の時点で、`Integer#pow` は、`x.pow(0, 1)`で「mod 1」なのに `1` を返す小さなバグがあります。
23
+
24
+ **制約**
25
+
26
+ - 引数`n`, `m`は、整数。
27
+ - `0 ≦ n`, `1 ≦ m`
28
+
29
+ **計算量**
30
+
31
+ - `O(log n)`
32
+
33
+ ## inv_mod
34
+
35
+ ```ruby
36
+ inv_mod(x, n, m)
37
+ ```
38
+
39
+ `xy ≡ 1 (mod m)` なる `y` のうち、`0 ≦ y < m` を満たすものを返します。
40
+
41
+ mが素数のとき、フェルマーの小定理より `x.pow(m - 2, m)` を使えます。
42
+
43
+ **制約**
44
+
45
+ - `gcd(x, m) = 1`, `1 ≦ m`
46
+
47
+ **計算量**
48
+
49
+ - `O(log m)`
50
+
51
+ ### Verified
52
+ - [ABC186 E - Throne](https://atcoder.jp/contests/abc186/tasks/abc186_e)
53
+ [ACコード(59ms) 2020/12/20](https://atcoder.jp/contests/abc186/submissions/18898186)
54
+
55
+ ## crt(r, m) -> [rem , mod] or [0, 0]
56
+
57
+ 中国剰余定理
58
+ Chinese remainder theorem
59
+
60
+ 同じ長さ`n`の配列`r`, `m`を渡したとき、
61
+
62
+ `x ≡ r[i] (mod m[i]), ∀i ∈ {0, 1, …… n - 1}`
63
+
64
+ を解きます。答えが存在するとき、`x ≡ rem(mod)`という形で書け、
65
+
66
+ 答えが存在するとき、`[rem ,mod]`を返します。
67
+
68
+ 答えがないとき、`[0, 0]`を返します。
69
+
70
+ ### Verified
71
+
72
+ 問題
73
+ - [No\.187 中華風 \(Hard\) \- yukicoder](https://yukicoder.me/problems/no/187)
74
+
75
+ ## floor_sum(n, m, a, b)
76
+
77
+ $\sum_{i = 0}^{n - 1} \mathrm{floor}(\frac{a \times i + b}{m})$
78
+
79
+ `Σ[k = 0 → n - 1] floow((a * i + b) / m)`
80
+
81
+ を計算量を工夫して計算して返します。
82
+
83
+ **計算量**
84
+
85
+ - `O(log(n + m + a + b))`
86
+
87
+ ### Verified
88
+
89
+ [ALPC: C \- Floor Sum](https://atcoder.jp/contests/practice2/tasks/practice2_c)
90
+ - [ACコード 426ms 2020/9/14](https://atcoder.jp/contests/practice2/submissions/16735215)
91
+
92
+ ## 参考リンク
93
+
94
+ - 当ライブラリ
95
+ - 当ライブラリの実装コード
96
+ - [当ライブラリの実装コード pow_mod.rb](https://github.com/universato/ac-library-rb/blob/master/lib/pow_mod.rb)
97
+ - [当ライブラリの実装コード inv_mod.rb](https://github.com/universato/ac-library-rb/blob/master/lib/inv_mod.rb)
98
+ - [当ライブラリの実装コード crt.rb](https://github.com/universato/ac-library-rb/blob/master/lib/crt.rb)
99
+ - [当ライブラリの実装コード floor_sum.rb](https://github.com/universato/ac-library-rb/blob/master/lib/floor_sum.rb)
100
+ - テストコード
101
+ - [当ライブラリのテストコード pow_mod_test.rb](https://github.com/universato/ac-library-rb/blob/master/test/pow_mod.rb)
102
+ - [当ライブラリのテストコード inv_mod_test.rb](https://github.com/universato/ac-library-rb/blob/master/test/inv_mod.rb)
103
+ - [当ライブラリのテストコード crt_test.rb](https://github.com/universato/ac-library-rb/blob/master/test/crt.rb)
104
+ - [当ライブラリのテストコード floor_sum_test.rb](https://github.com/universato/ac-library-rb/test/master/lib/floor_sum.rb)
105
+ - 本家ライブラリ
106
+ - [本家ライブラリの実装コード math.hpp](https://github.com/atcoder/ac-library/blob/master/atcoder/math.hpp)
107
+ - [本家ライブラリの実装コード internal_math.hpp](https://github.com/atcoder/ac-library/blob/master/atcoder/internal_math.hpp)
108
+ - [本家ライブラリのテストコード math_test.cpp](https://github.com/atcoder/ac-library/blob/master/test/unittest/math_test.cpp)
109
+ - [本家ライブラリのドキュメント math.md](https://github.com/atcoder/ac-library/blob/master/document_ja/math.md)
110
+ - [Relax the constraints of floor\_sum? · Issue \#33 · atcoder/ac\-library](https://github.com/atcoder/ac-library/issues/33)
111
+
112
+ ## Q&A
113
+
114
+ ### ファイルは個別なの?
115
+
116
+ 本家側に合わせて`math`ファイルなどにまとめてもいいかもしれないです。
@@ -0,0 +1,129 @@
1
+ # MaxFlow
2
+
3
+ 最大フロー問題を解くライブラリです。
4
+
5
+ ## 特異メソッド
6
+
7
+ ### new(n = 0) -> MaxFlow
8
+
9
+ ```ruby
10
+ graph = Maxflow.new(10)
11
+ ```
12
+
13
+ n頂点0辺のグラフを作ります。
14
+
15
+ 頂点の番号は、0-based indexです。
16
+
17
+ ## インスタンスメソッド
18
+
19
+ ### add_edge(from, to, cap) -> Integer
20
+
21
+ ```ruby
22
+ graph.add_edge(0, 1, 5)
23
+ ```
24
+
25
+ 頂点`from`から頂点`to`への最大容量`cap`, 流量`0`の辺を追加します。
26
+
27
+ 返り値は、0-based indexで何番目に追加された辺かを返します。
28
+
29
+ ### flow(start, to, flow_limit = 1 << 64) -> Integer
30
+
31
+ ```ruby
32
+ (1) graph.flow(0, 3)
33
+ (2) graph.flow(0, 3, flow_limit)
34
+ ```
35
+
36
+ **エイリアス** `max_flow`, `flow`
37
+
38
+ ### min_cut(start) -> Array(boolean)
39
+
40
+ ```ruby
41
+ graph.min_cut(start)
42
+ ```
43
+
44
+ 返り値は、長さ`n`の配列。
45
+
46
+ 返り値の`i`番目の要素には、頂点`start`から`i`へ残余グラフで到達可能なときに`true`が入り、そうでない場合に`false`が入る。
47
+
48
+ **計算量** `O(n + m)` ※ `m`は辺数
49
+
50
+ ### get_edge(i) -> [from, to, cap, flow]
51
+
52
+ ```ruby
53
+ graph.get_edge(i)
54
+ graph.edge(i)
55
+ graph[i]
56
+ ```
57
+
58
+ 辺の状態を返します。
59
+
60
+ **計算量** `O(1)`
61
+
62
+ **エイリアス** `get_edge`, `edge`, `[]`
63
+
64
+ ### edges -> Array([from, to, cap, flow])
65
+
66
+ ```ruby
67
+ graph.edges
68
+ ```
69
+
70
+ 全ての辺の情報を含む配列を返します。
71
+
72
+ **計算量** `O(m)` ※`m`は辺数です。
73
+
74
+ #### `edges`メソッドの注意点
75
+
76
+ `edges`メソッドは全ての辺の情報を生成するため、`O(m)`です。
77
+
78
+ 生成コストがあるため、`get_edge(i)`を用いる方法や、`edges = graph.edges`と一度別の変数に格納する方法も考慮してください。
79
+
80
+ ### change_edge(i, new_cap, new_flow)
81
+
82
+ `i`番目に変更された辺の容量、流量を`new_cap`, `new_flow`に変更します。
83
+
84
+ **制約** `0 ≦ new_fow ≦ new_cap`
85
+
86
+ **計算量** `O(1)`
87
+
88
+ ## Verified
89
+
90
+ [ALPC: D \- Maxflow](https://atcoder.jp/contests/practice2/tasks/practice2_d)
91
+ - [ACコード 211ms 2020/9/17](https://atcoder.jp/contests/practice2/submissions/16789801)
92
+ - [ALPC: D解説 \- Qiita](https://qiita.com/magurofly/items/bfaf6724418bfde86bd0)
93
+
94
+ ## 参考リンク
95
+
96
+ - 当ライブラリ
97
+ - [当ライブラリの実装コード max_flow.rb(GitHub)](https://github.com/universato/ac-library-rb/blob/master/lib/max_flow.rb)
98
+ - [当ライブラリのテストコード max_flow_test.rb(GitHub)](https://github.com/universato/ac-library-rb/blob/master/test/max_flow_test.rb)
99
+ - 本家ライブラリ
100
+ - 本家のコード
101
+ - [本家の実装コード maxflow.hpp(GitHub)](https://github.com/atcoder/ac-library/blob/master/atcoder/maxflow.hpp)
102
+ - [本家のテストコード maxflow_test.cpp(GitHub)](https://github.com/atcoder/ac-library/blob/master/test/unittest/maxflow_test.cpp)
103
+ - 本家ドキュメント
104
+ - [本家のドキュメント maxflow.md(GitHub)](https://github.com/atcoder/ac-library/blob/master/document_ja/maxflow.md)
105
+ - [本家のドキュメント appendix.md(GitHub)](https://github.com/atcoder/ac-library/blob/master/document_ja/appendix.md)
106
+
107
+ ## Q&A
108
+
109
+ ### `flow`メソッドの`flow_limit`のデフォルトは 何故`1 << 64`
110
+
111
+ 記憶になく、深い意味はないです。
112
+ `Float::MAX`や`Float::INFINITY`でも良さそうですが、遅くならないでしょうか。
113
+
114
+ ### 辺にStructは使わないの
115
+
116
+ Structを使った方がコードがスリムになって上級者ぽくもあり見栄えは良いです。
117
+
118
+ しかし、計測した結果、Strucだと遅いので、配列を使用しています。
119
+
120
+ ### `_`始まりの変数の意図は
121
+
122
+ ちょっと紛らわしいのですが、別々に2種類の意図があります。
123
+
124
+ 1. `_e`や`_re`は、本家ACLコードと一緒に読みやすいように、変数名を合わせています。
125
+ 2. `_rev`は、`each`で回す都合上吐き出すけれど使わないので、「使わない」という意図で`_`始まりにしています。
126
+
127
+ ```ruby
128
+ @g[q].each do |(to, _rev, cap)|
129
+ ```