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 | #pragma once
#include <cassert><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <valarray><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <atcoder/math><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include "snippet/aliases.hpp"
#include "snippet/iterations.hpp"
#include "numeric/modular/modint_interface.hpp"
#include "numeric/modular/barrett_reduction.hpp"
#include "numeric/modular/montgomery_reduction.hpp"
#include "numeric/modular/binary_reduction.hpp"
// Thanks to: https://nyaannyaan.github.io/library/modulo/arbitrary-mod-binomial.hpp
namespace uni {
namespace internal {
template<class T, class R = T, class Reduction = barrett_reduction_32bit>
requires (std::numeric_limits<R>::digits > 30) || modint_family<R>
struct binomial_coefficient_prime_power_mod {
using value_type = T;
using mod_type = R;
static constexpr i64 MOD_SUP = (i64{1} << 30) - 1;
static constexpr u32 MAX_BUFFER_SIZE = 30'000'000;
private:
using self = binomial_coefficient_prime_power_mod;
using reduction = Reduction;
u32 _p, _q, _m;
value_type _max;
std::valarray<u32> _fact, _inv_fact, _inv;
u32 _delta;
barrett_reduction_32bit _barrett_m, _barrett_p;
reduction _reduction_m;
u32 _one;
static constexpr std::pair<u32,u32> _factorize(u32 m) {
for(u32 i=2; i*i<=m; ++i) {
if(m % i == 0) {
u32 cnt = 0;
while(m % i == 0) m /= i, ++cnt;
assert(m == 1);
return { i, cnt };
}
}
return { m, 1 };
}
void _init() {
const u32 size = std::min(this->_m, static_cast<u32>(this->_max) + 1);
assert(size < self::MAX_BUFFER_SIZE);
this->_barrett_m = barrett_reduction_32bit(this->_m);
this->_barrett_p = barrett_reduction_32bit(this->_p);
this->_reduction_m = self::reduction(this->_m);
this->_delta = this->_reduction_m.convert_raw((this->_p == 2 && this->_q >= 3) ? 1 : this->_m - 1);
this->_one = this->_reduction_m.convert_raw(1);
this->_fact.resize(size);
this->_inv_fact.resize(size);
this->_inv.resize(size);
this->_fact[0] = this->_inv_fact[0] = this->_inv[0] = this->_one;
this->_fact[1] = this->_inv_fact[1] = this->_inv[1] = this->_one;
REP(i, 2, size) {
if(this->_barrett_p.reduce(i) == 0) {
this->_fact[i] = this->_fact[i - 1];
this->_fact[i + 1] = this->_reduction_m.multiply(this->_fact[i - 1], this->_reduction_m.convert_raw(this->_barrett_m.reduce(i + 1)));
++i;
}
else {
this->_fact[i] = this->_reduction_m.multiply(this->_fact[i - 1], this->_reduction_m.convert_raw(this->_barrett_m.reduce(i)));
}
}
this->_inv_fact[size - 1] = this->_reduction_m.pow(this->_fact[size - 1], this->_m / this->_p * (this->_p - 1) - 1);
REPD(i, 2, size - 1) {
this->_inv_fact[i] = this->_reduction_m.multiply(this->_inv_fact[i + 1], this->_reduction_m.convert_raw(this->_barrett_m.reduce(i + 1)));
if(this->_barrett_p.reduce(i) == 0) {
this->_inv_fact[i - 1] = this->_inv_fact[i];
--i;
}
}
}
public:
binomial_coefficient_prime_power_mod() noexcept = default;
explicit binomial_coefficient_prime_power_mod(const value_type max = 20'000'000) noexcept(NO_EXCEPT)
requires (1 < mod_type::mod() && mod_type::mod() < self::MOD_SUP)
: _m(mod_type::mod()), _max(max)
{
constexpr std::pair<u32,u32> pq = self::_factorize(mod_type::mod());
std::tie(this->_p, this->_q) = pq;
this->_init();
}
explicit binomial_coefficient_prime_power_mod(const mod_type p, mod_type q = 1, const value_type max = 20'000'000) noexcept(NO_EXCEPT)
requires (not modint_family<mod_type>)
: _p(static_cast<u32>(p)), _q(static_cast<u32>(q)), _max(max)
{
assert(1 < p && p < self::MOD_SUP);
assert(0 < q);
u64 m = 1;
while(q--) {
m *= this->_p;
assert(m < MOD_SUP);
}
this->_m = static_cast<u32>(m);
this->_init();
}
inline mod_type mod() const noexcept(NO_EXCEPT) { return this->_m; }
mod_type lucus(value_type n, value_type r) const noexcept(NO_EXCEPT) {
assert(0 <= n);
assert(0 <= r);
if(n < r) return 0;
u32 res = this->_one;
while(n > 0) {
u32 n0, k0;
std::tie(n, n0) = this->_barrett_p.divide(n);
std::tie(r, k0) = this->_barrett_p.divide(r);
if(n0 < k0) return 0;
res = this->_reduction_m.multiply(res, this->_fact[n0]);
res = this->_reduction_m.multiply(res, this->_reduction_m.multiply(this->_inv_fact[n0 - k0], this->_inv_fact[k0]));
}
return static_cast<mod_type>(this->_reduction_m.revert(res));
}
mod_type comb(value_type n, value_type r) const noexcept(NO_EXCEPT) {
assert(0 <= n);
assert(0 <= r);
if(this->_q == 1) return this->lucus(n, r);
if(n < r) return 0;
value_type k = n - r;
u32 e0 = 0, eq = 0, i = 0;
u32 res = this->_one;
while(n > 0) {
res = this->_reduction_m.multiply(res, this->_fact[this->_barrett_m.reduce(n)]);
res = this->_reduction_m.multiply(res, this->_inv_fact[this->_barrett_m.reduce(r)]);
res = this->_reduction_m.multiply(res, this->_inv_fact[this->_barrett_m.reduce(k)]);
n = this->_barrett_p.quotient(n);
r = this->_barrett_p.quotient(r);
k = this->_barrett_p.quotient(k);
u32 eps = static_cast<u32>(n - r - k);
e0 += eps;
if(e0 >= this->_q) return 0;
if(++i >= this->_q) eq += eps;
}
if(eq & 1) res = this->_reduction_m.multiply(res, this->_delta);
res = this->_reduction_m.multiply(res, this->_reduction_m.pow(this->_reduction_m.convert_raw(this->_p), e0));
return static_cast<mod_type>(this->_reduction_m.revert(res));
}
};
} // namespace internal
using internal::binomial_coefficient_prime_power_mod;
// (md < 10^7 && N < 2^30) || (md < 2^30 && N < 2 * 10^7)
template<class T, class R = T>
requires (std::numeric_limits<R>::digits > 30) || internal::modint_family<R>
struct binomial_coefficient {
using value_type = T;
using mod_type = R;
private:
template<class reduction = montgomery_reduction_32bit>
using internal_binomial = internal::binomial_coefficient_prime_power_mod<value_type, long long, reduction>;
u32 _mod;
value_type _max;
std::vector<u32> _mods;
std::vector<internal_binomial<>> _internals;
internal_binomial<binary_reduction_32bit> _internal_2p;
void _init() noexcept(NO_EXCEPT) {
u32 md = this->_mod;
if(md == 1) return;
assert((md < 20'000'000 && this->_max < (1 << 30)) || (md < (1 << 30) && this->_max < 30'000'000));
assert(1 <= md);
assert(md <= internal_binomial<>::MOD_SUP);
for(u32 i=2; i*i<=md; ++i) {
if(md % i == 0) {
u32 j = 0, k = 1;
while(md % i == 0) md /= i, ++j, k *= i;
this->_mods.push_back(k);
if(i == 2) {
this->_internal_2p = internal_binomial<binary_reduction_32bit>(2, j, this->_max);
this->_internals.emplace_back();
}
else {
this->_internals.emplace_back(i, j, this->_max);
assert(this->_mods.back() == this->_internals.back().mod());
}
}
}
if(md != 1) {
this->_mods.push_back(static_cast<u32>(md));
if(md == 2) this->_internal_2p = internal_binomial<binary_reduction_32bit>(2, 1, this->_max);
else this->_internals.emplace_back(md, 1, this->_max);
}
}
public:
binomial_coefficient(const value_type max = 20'000'000) noexcept(NO_EXCEPT)<--- Struct 'binomial_coefficient' has a constructor with 1 argument that is not explicit. [+]Struct 'binomial_coefficient' has a constructor with 1 argument that is not explicit. Such, so called "Converting constructors", should in general be explicit for type safety reasons as that prevents unintended implicit conversions. <--- Struct 'binomial_coefficient < std :: int64_t , std :: int64_t >' has a constructor with 1 argument that is not explicit. [+]Struct 'binomial_coefficient < std :: int64_t , std :: int64_t >' has a constructor with 1 argument that is not explicit. Such, so called "Converting constructors", should in general be explicit for type safety reasons as that prevents unintended implicit conversions. <--- Struct 'binomial_coefficient < std :: int32_t , std :: int32_t >' has a constructor with 1 argument that is not explicit. [+]Struct 'binomial_coefficient < std :: int32_t , std :: int32_t >' has a constructor with 1 argument that is not explicit. Such, so called "Converting constructors", should in general be explicit for type safety reasons as that prevents unintended implicit conversions.
requires internal::modint_family<mod_type>
: _mod(static_cast<u32>(mod_type::mod())), _max(max)
{
this->_init();
}
binomial_coefficient(const mod_type mod, const value_type max = 20'000'000) noexcept(NO_EXCEPT)<--- Struct 'binomial_coefficient' has a constructor with 1 argument that is not explicit. [+]Struct 'binomial_coefficient' has a constructor with 1 argument that is not explicit. Such, so called "Converting constructors", should in general be explicit for type safety reasons as that prevents unintended implicit conversions. <--- Struct 'binomial_coefficient < std :: int64_t , std :: int64_t >' has a constructor with 1 argument that is not explicit. [+]Struct 'binomial_coefficient < std :: int64_t , std :: int64_t >' has a constructor with 1 argument that is not explicit. Such, so called "Converting constructors", should in general be explicit for type safety reasons as that prevents unintended implicit conversions. <--- Struct 'binomial_coefficient < std :: int32_t , std :: int32_t >' has a constructor with 1 argument that is not explicit. [+]Struct 'binomial_coefficient < std :: int32_t , std :: int32_t >' has a constructor with 1 argument that is not explicit. Such, so called "Converting constructors", should in general be explicit for type safety reasons as that prevents unintended implicit conversions.
requires (not internal::modint_family<mod_type>)
: _mod(static_cast<u32>(mod)), _max(max)
{
this->_init();
}
mod_type comb(const value_type n, const value_type r) const noexcept(NO_EXCEPT) {
assert(n >= 0), assert(r >= 0);
if(this->_mod == 1) return 0;
if(n < r) return 0;
if(this->_mods.size() == 1) {
if((this->_mods.back() & 1) == 0) return static_cast<mod_type>(this->_internal_2p.comb(n, r));
else return static_cast<mod_type>(this->_internals[0].comb(n, r));
}
std::vector<long long> rem, div;
REP(i, 0, std::ranges::ssize(this->_mods)) {
div.push_back(this->_mods[i]);
if((this->_mods[i] & 1) == 0) rem.push_back(this->_internal_2p.comb(n, r));
else {
rem.push_back(this->_internals[i].comb(n, r));
}
}
return static_cast<mod_type>(atcoder::crt(rem, div).first);
}
};
} // namespace uni
|