1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321 | #pragma once
#include <cassert><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <vector><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include "snippet/iterations.hpp"
#include "internal/dev_env.hpp"
#include "internal/concepts.hpp"
#include "structure/grid.hpp"
#include "numeric/modular/modint_interface.hpp"
namespace uni {
namespace internal {
template<class Base>
struct matrix_core : Base {
using Base::Base;
using value_type = typename Base::value_type;
using size_type = typename Base::size_type;
using vector_type = typename Base::row_type;
static constexpr auto identity(const size_type n, const value_type& val = 1) noexcept(NO_EXCEPT) {
matrix_core res(n);
REP(i, n) res(i, i) = val;
return res;
}
inline constexpr bool square() const noexcept(NO_EXCEPT) { return this->height() == this->width(); }
constexpr auto& operator+=(const value_type& rhs) noexcept(NO_EXCEPT) {
REP(i, this->height()) REP(j, this->width()) this->operator()(i, j) += rhs;
return *this;
}
template<class... Ts>
constexpr auto& operator+=(const matrix_core<Ts...>& rhs) noexcept(NO_EXCEPT) {
REP(i, this->height()) REP(j, this->width()) this->operator()(i, j) += rhs(i, j);
return *this;
}
template<weakly_addition_assignable<matrix_core> T>
inline constexpr auto operator+(const T& rhs) const noexcept(NO_EXCEPT) {
return matrix_core(*this) += rhs;
}
constexpr auto& operator-=(const value_type& rhs) noexcept(NO_EXCEPT) {
REP(i, this->height()) REP(j, this->width()) this->operator()(i, j) -= rhs;
return *this;
}
template<class... Ts>
constexpr auto& operator-=(const matrix_core<Ts...>& rhs) noexcept(NO_EXCEPT) {
REP(i, this->height()) REP(j, this->width()) this->operator()(i, j) -= rhs(i, j);
return *this;
}
template<weakly_subtraction_assignable<matrix_core> T>
inline constexpr auto operator-(const T& rhs) const noexcept(NO_EXCEPT) {
return matrix_core(*this) -= rhs;
}
template<class... Ts>
constexpr auto operator*(const matrix_core<Ts...>& rhs) const noexcept(NO_EXCEPT) {
assert(this->width() == rhs.height());
matrix_core res(this->height(), rhs.width());
REP(i, this->height()) REP(j, rhs.width()) REP(k, this->width()) {
res(i, j) += (*this)(i, k) * rhs(k, j);
}
return res;
}
template<std::ranges::input_range Vector>
requires
(!internal::derived_from_template<Vector, matrix_core>) &&
internal::weakly_multipliable<value_type, std::ranges::range_value_t<Vector>>
constexpr auto operator*(const Vector& rhs) const noexcept(NO_EXCEPT) {
debug(*this, rhs);
assert(this->width() == std::ranges::ssize(rhs));
Vector res(this->height());
REP(i, this->height()) REP(j, this->width()) res[i] += this->operator()(i, j) * rhs[j];
return res;
}
template<std::ranges::input_range Vector>
requires
(!internal::derived_from_template<Vector, matrix_core>) &&
internal::weakly_multipliable<std::ranges::range_value_t<Vector>, value_type>
friend constexpr auto operator*(const Vector& lhs, const matrix_core& rhs) noexcept(NO_EXCEPT) {
debug(lhs, rhs);
assert(std::ranges::ssize(lhs) == rhs.height());
Vector res(rhs.width());
REP(i, rhs.height()) REP(j, rhs.width()) res[j] += lhs[j] * rhs[i][j];
return res;
}
constexpr auto operator*(const value_type& rhs) noexcept(NO_EXCEPT) {
auto res = *this;
REP(i, res.height()) REP(j, res.width()) res(i, j) *= rhs;
return res;
}
template<multipliable<matrix_core> T>
constexpr auto& operator*=(const T& rhs) noexcept(NO_EXCEPT) {
matrix_core res = *this * rhs;
this->assign(res);
return *this;
}
constexpr auto& operator/=(const value_type& rhs) noexcept(NO_EXCEPT) {
REP(i, this->height()) REP(j, this->width()) this->operator()(i, j) /= rhs;
return *this;
}
inline constexpr auto operator/(const value_type& rhs) const noexcept(NO_EXCEPT) {
return matrix_core(*this) /= rhs;
}
constexpr auto& operator%=(const value_type& rhs) noexcept(NO_EXCEPT) {
REP(i, this->height()) REP(j, this->width()) this->operator()(i, j) %= rhs;
return *this;
}
inline constexpr auto operator%(const value_type& rhs) const noexcept(NO_EXCEPT) {
return matrix_core(*this) %= rhs;
}
template<std::integral K>
constexpr auto pow(const K n) const noexcept(NO_EXCEPT) {
assert(this->height() == this->width());
if constexpr(std::signed_integral<K>) assert(n >= 0);
return uni::pow(*this, n, {}, matrix_core::identity(this->height()));
}
constexpr auto find_pivot(const size_type rank, const size_type col) const noexcept(NO_EXCEPT) {
size_type pivot = -1;
REP(i, rank, this->height()) {
if(this->operator()(i, col) != 0) {
pivot = i;
break;
}
}
return pivot;
}
template<bool DIAGONALIZE = false>
constexpr void sweep(const size_type rank, const size_type col, const size_type pivot) noexcept(NO_EXCEPT)
requires modint_family<value_type>
{
std::swap(this->operator[](pivot), this->operator[](rank));
if constexpr(DIAGONALIZE) {
const auto inv = this->operator()(rank, col).inv();
REP(j, this->width()) this->operator()(rank, j) *= inv;
}
REP(i, (DIAGONALIZE ? 0 : rank + 1), this->height()) {
if(i != rank && this->operator()(i, col) != 0) {
const auto fac = this->operator()(i, col);
REP(j, this->width()) this->operator()(i, j) -= this->operator()(rank, j) * fac;
}
}
}
template<size_type IGNORE_WIDTH = 0, bool DIAGONALIZE = false>
inline constexpr auto transform_to_upper_triangular() noexcept(NO_EXCEPT)
requires modint_family<value_type>
{
size_type rank = 0;
REP(j, this->width() - IGNORE_WIDTH) {
const auto pivot = this->find_pivot(rank, j);
if(pivot == -1) continue;
this->template sweep<DIAGONALIZE>(rank++, j, pivot);<--- Prefer prefix ++/-- operators for non-primitive types. [+]Prefix ++/-- operators should be preferred for non-primitive types. Pre-increment/decrement can be more efficient than post-increment/decrement. Post-increment/decrement usually involves keeping a copy of the previous value around and adds a little extra code.
}
return rank;
}
template<size_type IGNORE_WIDTH>
inline constexpr auto transform_to_lower_triangular() noexcept(NO_EXCEPT)
requires modint_family<value_type>
{
const auto rank = this->template transform_to_upper_triangular<IGNORE_WIDTH>();
this->transpose();
return rank;
}
template<std::ranges::input_range Vector, class Sol = void, class System = void>
constexpr std::optional<size_type> solve_linear_equation(
const Vector& v,
Sol *const sol = nullptr,
System *const system = nullptr
)
const noexcept(NO_EXCEPT)
requires modint_family<value_type>
{
static_assert(!(std::same_as<Sol, void>) || (std::same_as<System, void>));
assert(this->height() == std::ranges::ssize(v));
matrix_core mat(this->height(), this->width() + 1);
REP(i, this->height()) {
REP(j, this->width()) mat(i, j) = this->operator()(i, j);
mat(i, this->width()) = v[i];
}
const auto rank = mat.transform_to_upper_triangular<1, true>();
REP(i, rank, this->height()) if(mat(i, this->width()) != value_type::zero) return {};
if constexpr(!std::same_as<Sol, void>) {
static_assert(std::ranges::output_range<Sol, value_type>);
static_assert(std::ranges::output_range<std::ranges::range_value_t<System>, value_type>);
assert(!sol || system);
if(sol) {
sol->assign(this->width(), value_type::zero);
std::vector<size_type> pivots(system ? this->width() : 0, -1);
for(size_type i = 0, j = 0; i < rank; ++i) {
while(mat(i, j) == value_type::zero) ++j;
sol->operator[](j) = mat(i, this->width());
if constexpr(!std::same_as<System, void>) if(system) {
pivots[j] = i;
}
}
if constexpr(!std::same_as<System, void>) if(system) {
REP(j, this->width()) {
if(pivots[j] != -1) continue;
typename System::vector_type x(this->width());
x[j] = value_type::one;
REP(k, j) if(pivots[k] != -1) x[k] = -mat(pivots[k], j);
system->push_back(x);
}
system->resize(system->size(), this->width());
}
}
}
return rank;
}
value_type determinant() const noexcept(NO_EXCEPT)
requires modint_family<value_type>
{
assert(this->square());
auto mat = *this;
value_type det = value_type::one;
REP(j, this->height()) {
const auto pivot = mat.find_pivot(j, j);
if(j < pivot) std::swap(mat[pivot], mat[j]), det = -det;
if(mat(j, j) == 0) return 0;
REP(i, j + 1, this->height()) {
while(mat(i, j) != 0) {
const auto c = static_cast<value_type>(-to_signed(mat(j, j).val() / mat(i, j).val()));
REP(k, j, this->height()) mat(j, k) += mat(i, k) * c;
std::swap(mat[i], mat[j]), det = -det;
}
}
}
REP(i, mat.height()) det *= mat(i, i);
return det;
}
};
} // namespace internal
template<class T, class Base = grid<T>>
using matrix = internal::matrix_core<Base>;
template<class T>
using valmatrix = internal::matrix_core<valgrid<T>>;
template<class T>
using unfolded_matrix = internal::matrix_core<unfolded_grid<T>>;
template<class T>
using unfolded_valmatrix = internal::matrix_core<unfolded_valgrid<T>>;
} // namespace uni
|