ac-library-rb 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,263 @@
1
+ # ModInt
2
+
3
+ This is a library that can be used for problems where the answer is divided by a certain number and the remainder is output.
4
+
5
+ It automatically takes the remainder when calculating, thus reducing errors.
6
+
7
+ ## Caution
8
+
9
+ The use of this library ModInt may slow down the execution time.
10
+
11
+ Please be careful when using it.
12
+
13
+ ## Example usage
14
+
15
+ First, set the remainder law with `ModInt.set_mod` or `ModInt.mod =`.
16
+
17
+ ```ruby
18
+ ModInt.set_mod(11)
19
+ ModInt.mod #=> 11
20
+
21
+ a = ModInt(10)
22
+ b = 3.to_m
23
+
24
+ p -b # 8 mod 11
25
+
26
+ p a + b # 2 mod 11
27
+ p 1 + a # 0 mod 11
28
+ p a - b # 7 mod 11
29
+ p b - a # 4 mod 11
30
+
31
+ p a * b # 8 mod 11
32
+ p b.inv # 4 mod 11
33
+
34
+ p a / b # 7 mod 11
35
+
36
+ a += b
37
+ p a # 2 mod 11
38
+ a -= b
39
+ p a # 10 mod 11
40
+ a *= b
41
+ p a # 8 mod 11
42
+ a /= b
43
+ p a # 10 mod 11
44
+
45
+ p ModInt(2)**4 # 5 mod 11
46
+
47
+ puts a #=> 10
48
+
49
+ p ModInt.raw(3) #=> 3 mod 11
50
+ ```
51
+
52
+ ## output with puts and p
53
+
54
+ ```ruby
55
+ ModInt.mod = 11
56
+ a = 12.to_m
57
+
58
+ puts a #=> 1
59
+ p a #=> 1 mod 11
60
+ ```
61
+
62
+ `ModInt` defines `to_s` and `inspect`.
63
+
64
+ This causes `puts` to use `to_s` and `p` to use `inspect` internally, so the output is modified.
65
+
66
+ Note that the `puts` method is useful because it behaves no differently than the output of `Integer`, but conversely, it is indistinguishable.
67
+
68
+ ## Singular methods.
69
+
70
+ ### new(val = 0) -> ModInt
71
+
72
+ ```ruby
73
+ a = ModInt.new(10)
74
+ b = ModInt(3)
75
+ ```
76
+
77
+ We have a syntax sugar `Kernel#ModInt` that does not use `new`.
78
+
79
+ #### [Reference] Integer#to_m, String#to_m
80
+
81
+ ```ruby
82
+ 5.to_m
83
+ '2'.to_m
84
+ ```
85
+
86
+ Converts `Integer` and `String` instances to `ModInt`.
87
+
88
+ **aliases** `to_modint`, `to_m`.
89
+
90
+ ### set_mod(mod)
91
+
92
+ ```ruby
93
+ ModInt.set_mod(10**9 + 7)
94
+ ModInt.mod = 10**9 + 7
95
+ ```
96
+
97
+ Use this method first to set the mod.
98
+
99
+ This is set to the global variable `$_mod` as an internal implementation.
100
+
101
+ It also internally checks whether `$_mod` is prime or not, and assigns a boolean value to the global variable `$_mod_is_prime`.
102
+
103
+ Note that you may not have many chances to use the return value of this method, but `mod=` returns the assigned argument as it is, while `set_mod` returns `[mod, mod.prime?]
104
+
105
+ **aliases** `set_mod`, `mod=`
106
+
107
+ ### mod -> Integer
108
+
109
+ ```ruby
110
+ ModInt.mod
111
+ ```
112
+ Returns mod.
113
+
114
+ This returns the global variable `$_mod` as an internal implementation.
115
+
116
+ ### raw(val) -> ModInt
117
+
118
+ Returns
119
+
120
+ ````ruby
121
+ ModInt.raw(2, 11) # 2 mod 11
122
+ ````
123
+
124
+ Returns ModInt instead of taking mod for `val`.
125
+
126
+ This is a constructor for constant-fold speedup.
127
+
128
+ If `val` is guaranteed to be greater than or equal to `0` and not exceed `mod`, it is faster to generate `ModInt` here.
129
+
130
+ ## Instance methods.
131
+
132
+ ### val -> Integer
133
+
134
+ ```ruby
135
+ ModInt.mod = 11
136
+ m = 12.to_m # 1 mod 11
137
+ n = -1.to_m # 10 mod 11
138
+
139
+ p i = m.val #=> 1
140
+ p j = n.to_i #=> 10
141
+ ```
142
+
143
+ Returns the value of the body from an instance of the ModInt class.
144
+
145
+ As an internal implementation, it returns `@val`.
146
+
147
+ Use it to convert to `integer`.
148
+
149
+ **Aliases**
150
+
151
+ - `val`
152
+ - `to_i`.
153
+
154
+ #### Additional information about aliases
155
+
156
+ There is no difference between `val` and `to_i` as an alias for the instance method of `ModInt`. However, the `Integer` class also has a `to_i` method, so `to_i` is more flexible and Ruby-like. In the internal implementation, `to_i` is used so that either argument can be used. Note that `val` is the name of the function in the original, and `@val` is the name of the instance variable in the internal implementation.
157
+
158
+ ### inv -> ModInt
159
+
160
+ ```rb
161
+ ModInt.mod = 11
162
+ m = 3.to_m
163
+
164
+ p m.inv #=> 4 mod 11
165
+ ```
166
+
167
+ It returns `y` such that `xy ≡ 1 (mod ModInt.mod)`.
168
+
169
+ That is, it returns the modulus inverse.
170
+
171
+ ### pow(other) -> ModInt
172
+
173
+ ```ruby
174
+ ModInt.mod = 11
175
+ m = 2.to_m
176
+
177
+ p m**4 #=> 5 mod 11
178
+ p m.pow(4) #=> 5 mod 11
179
+ ```
180
+
181
+ Only `integer` can be taken as argument.
182
+
183
+ ### Various operations.
184
+
185
+ As with the `Integer` class, you can perform arithmetic operations between `ModInt` and `Integer`, or between `Integer` and `ModInt`.
186
+ See the usage examples for details.
187
+
188
+ ```ruby
189
+ ModInt.mod = 11
190
+
191
+ p 5.to_m + 7.to_m #=> 1 mod 11
192
+ p 0 + -1.to_m #=> 10 mod 11
193
+ p -1.to_m + 0 #=> 10 mod 11
194
+
195
+ p 12.to_m == 23.to_m #=> true
196
+ p 12.to_m == 1 #=> true
197
+ ```
198
+
199
+ #### [Note] How to compare Integer and ModInt
200
+
201
+ `Integer` and `ModInt` can be compared by `==`, `! =`, but some behaviors are different from the original ACL.
202
+
203
+ The `Integer` to be compared returns true or false in the range of [0, mod), but always returns `false` in other ranges. This library has a restriction in the range of [0, mod), but the original library does not, so it differs in this point.
204
+
205
+ ## Verified
206
+
207
+ - [ABC156: D \- Bouquet](https://atcoder.jp/contests/abc156/tasks/abc156_d) held on 2020/2/22
208
+ - [AC Code 138ms 2020/10/5](https://atcoder.jp/contests/abc156/submissions/17205940)
209
+ - [ARC009: C \- Takahashi, 24 years old](https://atcoder.jp/contests/arc009/tasks/arc009_3) held on 2012/10/20
210
+ - [AC Code 1075ms 2020/10/5](https://atcoder.jp/contests/arc009/submissions/17206081)
211
+ - [AC code 901ms 2020/10/5](https://atcoder.jp/contests/arc009/submissions/17208378)
212
+
213
+ ## Speedup Tips.
214
+
215
+ The methods `+`, `-`, `*`, and `/` use the methods `add!`, `sub!`, `mul!`, and `div!` for the ones that are internally duplicated with `dup`, respectively. If you can destructively change the receiver's `ModInt`, this method may be faster on a constant-duple basis.
216
+
217
+ ## Reference links
218
+
219
+ - This library
220
+ - [The code of this library modint.rb(GitHub)](https://github.com/universato/ac-library-rb/blob/master/lib/modint.rb)
221
+ - Our library [Our code modint_test.rb(GitHub)](https://github.com/universato/ac-library-rb/blob/master/lib/modint.rb)
222
+ - The original library
223
+ - [Our implementation code modint.hpp(GitHub)](https://github.com/atcoder/ac-library/blob/master/atcoder/modint.hpp)
224
+ - Test code of the main library [modint_test.cpp(GitHub)](https://github.com/atcoder/ac-library/blob/master/atcoder/modint.hpp)
225
+ - Documentation of the original modint.md(GitHub)](https://github.com/atcoder/ac-library/blob/master/document_ja/modint.md)
226
+ - Ruby Reference Manual
227
+ - [class Numeric \(Ruby 2\.7\.0 Reference Manual\)](https://docs.ruby-lang.org/ja/latest/class/Numeric.html)
228
+ - Others
229
+ - [Why don't you try using the modint struct? \(C\cH000000)}Noshi91's Notes](https://noshi91.hatenablog.com/entry/2019/03/31/174006)(Mar 31, 2019)
230
+ - [Implementing modint in Python \- Qiita](https://qiita.com/wotsushi/items/c936838df992b706084c)(Apr 1, 2019)
231
+ - [Documentation of the C# version of ModInt](https://github.com/key-moon/ac-library-cs/blob/master/document_ja/modint.md)
232
+
233
+
234
+ ## Q&A.
235
+
236
+ ### Why include it if it may slow down the execution time?
237
+
238
+ - Reproduction of the original library.
239
+ - Reproduction to the essential part of the code.
240
+ - To measure how slow it really is. As a benchmark.
241
+ - To increase the number of users, to check the demand, and to appeal for inclusion in Ruby itself.
242
+
243
+ ### Advantages of ModInt
244
+
245
+ Ruby, unlike C/C++, has the following features.
246
+
247
+ - Definition of a negative number divided by a positive number that still returns a positive number
248
+ - Multiple length integers, no overflow (* If the number is too large, the calculation becomes slower)
249
+
250
+ Therefore, compared to C/C++, the advantages of using ModInt are diminished in some areas, but
251
+
252
+ - Improved readability
253
+ - There is no need to worry about forgetting to take ModInt.
254
+
255
+ The following are some of the advantages.
256
+
257
+ ### The intention of using global variables is
258
+
259
+ We are using global variables `$_mod` and `$_mod_is_prime`.
260
+
261
+ Global variables are to be avoided, especially in practice.
262
+
263
+ Translated with www.DeepL.com/Translator (free version)
@@ -0,0 +1,119 @@
1
+ # PriorityQueue
2
+
3
+ PriorityQueue
4
+
5
+ **Alias**
6
+
7
+ - `HeapQueue`
8
+
9
+ ## Class Methods
10
+
11
+ ### new(array = []) -> PriorityQueue
12
+
13
+ ```ruby
14
+ PriorityQueue.new
15
+ pq = PriorityQueue.new([1, -1, 100])
16
+ pq.pop # => 100
17
+ pq.pop # => 1
18
+ pq.pop # => -1
19
+ ```
20
+
21
+ It creates a `PriorityQueue` to initialized to `array`.
22
+
23
+ **Complexity**
24
+
25
+ - `O(n)` (`n` is the number of elements of `array`)
26
+
27
+ ### new(array = []) {|x, y| ... } -> PriorityQueue
28
+
29
+ ```ruby
30
+ PriorityQueue.new([1, -1, 100]) {|x, y| x < y }
31
+ pq.pop # => -1
32
+ pq.pop # => 1
33
+ pq.pop # => 100
34
+ ```
35
+
36
+ Constructs a prioritized queue from an array given as an argument.
37
+ A comparison function can be passed in the block. When a comparison function is passed, it is used to calculate the priority of the elements.
38
+
39
+ Any object can be an element of the queue if it can be compared with the comparison function.
40
+
41
+ **Complexity**
42
+
43
+ - `O(n)` (`n` is the number of elements of `array`)
44
+
45
+ ## Instance Methods
46
+
47
+ ### push(item)
48
+
49
+ ```ruby
50
+ pq.push(10)
51
+ ```
52
+
53
+ It adds the element.
54
+
55
+ **Alias**
56
+
57
+ - `<<`
58
+ - `append`
59
+
60
+ **Complexity**
61
+
62
+ - `O(log n)` (`n` is the number of elements of priority queue)
63
+
64
+ ### pop -> Highest priority element | nil
65
+
66
+ ```ruby
67
+ pq.pop # => 100
68
+ ```
69
+
70
+ It removes the element with the highest priority from the queue and returns it. If the queue is empty, it rerurns `nil`.
71
+
72
+ **Complexity**
73
+
74
+ - `O(log n)` (`n` is the number of elements of priority queue)
75
+
76
+ ### get -> Highest priority element | nil
77
+
78
+ ```ruby
79
+ pq.get # => 10
80
+ ```
81
+
82
+ It returns the value of the highest priority element without removing it.
83
+
84
+ If the queue is empty, it returns `nil`.
85
+
86
+ **Alias**
87
+
88
+ - `top`
89
+
90
+ **Complexity**
91
+
92
+ - `O(1)`
93
+
94
+ ### empty? -> bool
95
+
96
+ ```ruby
97
+ pq.empty? # => false
98
+ ```
99
+
100
+ It returns `true` if the queue is empty, `false` otherwise.
101
+
102
+ **Complexity** `O(1)`
103
+
104
+ ### heap -> Array[elements of priority queue]
105
+
106
+ ```ruby
107
+ pq.heap
108
+ ```
109
+
110
+ It returns the binary heap that the priority queue has internally.
111
+
112
+ ## Verified
113
+
114
+ - [Aizu Online Judge ALDS1_9_C Priority Queue](https://onlinejudge.u-aizu.ac.jp/problems/ALDS1_9_C)
115
+ - [AC code](https://onlinejudge.u-aizu.ac.jp/solutions/problem/ALDS1_9_C/review/4835681/qsako6/Ruby)
116
+
117
+ ## Links
118
+
119
+ - [cpython/heapq.py at master - python/cpython](https://github.com/python/cpython/blob/master/Lib/heapq.py)
@@ -0,0 +1,134 @@
1
+ # Segtree
2
+
3
+ Segment Tree
4
+
5
+ ## Class Methods
6
+
7
+ ### new(n, e, &op) -> Segtree
8
+
9
+ ```rb
10
+ seg = Segtree.new(n, e) { |x, y| ... }
11
+ ```
12
+
13
+ It creates an array `a` of length `n`. All the elements are initialized to `e`.
14
+
15
+ - `block`: returns `op(x, y)`
16
+ - `e`: identity element.
17
+
18
+ ### new(ary, e, &op) -> Segtree
19
+
20
+ ```rb
21
+ seg = Segtree.new(ary, e) { |x, y| ... }
22
+ ```
23
+
24
+ It creates an array `seg` of length `n` = `ary.size`, initialized to `ary`.
25
+
26
+ - `block`: returns `op(x, y)`
27
+ - `e`: identity element.
28
+
29
+ **complexty**
30
+
31
+ - `O(n)`
32
+
33
+ <details>
34
+ <summary>Monoid Exmaples</summary>
35
+
36
+ ```rb
37
+ n = 10**5
38
+ inf = (1 << 60) - 1
39
+
40
+ Segtree.new(n, 0) { |x, y| x.gcd y } # gcd
41
+ Segtree.new(n, 1) { |x, y| x.lcm y } # lcm
42
+ Segtree.new(n, -inf) { |x, y| [x, y].max } # max
43
+ Segtree.new(n, inf) { |x, y| [x, y].min } # min
44
+ Segtree.new(n, 0) { |x, y| x | y } # or
45
+ Segtree.new(n, 1) { |x, y| x * y } # prod
46
+ Segtree.new(n, 0) { |x, y| x + y } # sum
47
+ ```
48
+
49
+ </details>
50
+
51
+ ## Instance Methods
52
+
53
+ ### set
54
+
55
+ ```rb
56
+ seg.set(pos, x)
57
+ ```
58
+
59
+ It assigns `x` to `a[pos]`.
60
+
61
+ **Complexity** `O(logn)`
62
+
63
+ ### get
64
+
65
+ ```rb
66
+ seg.get(pos)
67
+ ```
68
+
69
+ It returns `a[pos]`
70
+
71
+ **Complexity** `O(1)`
72
+
73
+ ### prod
74
+
75
+ ```rb
76
+ seg.prod(l, r)
77
+ ```
78
+
79
+ It returns `op(a[l], ..., a[r - 1])` .
80
+
81
+ **Constraints**
82
+
83
+ - `0 ≦ l ≦ r ≦ n`
84
+
85
+ **Complexity**
86
+
87
+ - `O(logn)`
88
+
89
+ ### all_prod
90
+
91
+ ```rb
92
+ seg.all_prod
93
+ ```
94
+
95
+ It returns `op(a[0], ..., a[n - 1])`.
96
+
97
+ **Complexity**
98
+
99
+ - `O(1)`
100
+
101
+ ### max_right(l, &f) -> Integer
102
+
103
+ ```ruby
104
+ seg.max_right(l, &f)
105
+ ```
106
+
107
+ It applies binary search on the segment tree.
108
+
109
+ ### min_left(r, &f) -> Integer
110
+
111
+ ```ruby
112
+ seg.min_left(r, &f)
113
+ ```
114
+
115
+ It applies binary search on the segment tree.
116
+
117
+ ## Verified
118
+
119
+ - [ALPC: J \- Segment Tree](https://atcoder.jp/contests/practice2/tasks/practice2_j)
120
+ - [F \- Range Xor Query](https://atcoder.jp/contests/abc185/tasks/abc185_f)
121
+ - [AC Code(1538ms)](https://atcoder.jp/contests/abc185/submissions/18746817): Segtree(xor)
122
+ - [AC Code(821ms)](https://atcoder.jp/contests/abc185/submissions/18769200): FenwickTree(BIT) xor
123
+
124
+ ## 参考リンク
125
+
126
+ - ac-library
127
+ - [Code segtree.rb](https://github.com/universato/ac-library-rb/blob/master/lib/segtree.rb)
128
+ - [Test segtree_test.rb](https://github.com/universato/ac-library-rb/blob/master/test/segtree_test.rb)
129
+ - AtCoder Library
130
+ - [Document HTML](https://atcoder.github.io/ac-library/document_en/segtree.html)
131
+ - [Documetn appendix.md(GitHub)](https://github.com/atcoder/ac-library/blob/master/document_en/appendix.md)
132
+ - [Code segtree.hpp(GitHub)](https://github.com/atcoder/ac-library/blob/master/atcoder/segtree.hpp)
133
+ - [Test segtree_test.cpp(GitHub)](https://github.com/atcoder/ac-library/blob/master/test/unittest/segtree_test.cpp)
134
+ - [Sample code segtree_practice.cpp(GitHub)](https://github.com/atcoder/ac-library/blob/master/test/example/segtree_practice.cpp)