File tree Expand file tree Collapse file tree 2 files changed +42
-42
lines changed Expand file tree Collapse file tree 2 files changed +42
-42
lines changed Original file line number Diff line number Diff line change 3
3
#include " common.hpp"
4
4
#include < cassert>
5
5
namespace cp_algo ::math {
6
+ // fact/rfact/small_inv are caching
7
+ // Beware of usage with dynamic mod
8
+ template <typename T>
9
+ T fact (int n) {
10
+ static std::vector<T> F (maxn);
11
+ static bool init = false ;
12
+ if (!init) {
13
+ F[0 ] = T (1 );
14
+ for (int i = 1 ; i < maxn; i++) {
15
+ F[i] = F[i - 1 ] * T (i);
16
+ }
17
+ init = true ;
18
+ }
19
+ return F[n];
20
+ }
21
+ // Only works for modint types
22
+ template <typename T>
23
+ T rfact (int n) {
24
+ static std::vector<T> F (maxn);
25
+ static bool init = false ;
26
+ if (!init) {
27
+ int t = std::min<int64_t >(T::mod (), maxn) - 1 ;
28
+ F[t] = T (1 ) / fact<T>(t);
29
+ for (int i = t - 1 ; i >= 0 ; i--) {
30
+ F[i] = F[i + 1 ] * T (i + 1 );
31
+ }
32
+ init = true ;
33
+ }
34
+ return F[n];
35
+ }
36
+ template <typename T>
37
+ T small_inv (int n) {
38
+ static std::vector<T> F (maxn);
39
+ static bool init = false ;
40
+ if (!init) {
41
+ for (int i = 1 ; i < maxn; i++) {
42
+ F[i] = rfact<T>(i) * fact<T>(i - 1 );
43
+ }
44
+ init = true ;
45
+ }
46
+ return F[n];
47
+ }
6
48
template <typename T>
7
49
T binom_large (T n, int r) {
8
50
assert (r < maxn);
Original file line number Diff line number Diff line change @@ -29,47 +29,5 @@ namespace cp_algo::math {
29
29
T bpow (T const & x, int64_t n) {
30
30
return bpow (x, n, T (1 ));
31
31
}
32
- // fact/rfact/small_inv are caching
33
- // Beware of usage with dynamic mod
34
- template <typename T>
35
- T fact (int n) {
36
- static std::vector<T> F (maxn);
37
- static bool init = false ;
38
- if (!init) {
39
- F[0 ] = T (1 );
40
- for (int i = 1 ; i < maxn; i++) {
41
- F[i] = F[i - 1 ] * T (i);
42
- }
43
- init = true ;
44
- }
45
- return F[n];
46
- }
47
- // Only works for modint types
48
- template <typename T>
49
- T rfact (int n) {
50
- static std::vector<T> F (maxn);
51
- static bool init = false ;
52
- if (!init) {
53
- int t = std::min<int64_t >(T::mod (), maxn) - 1 ;
54
- F[t] = T (1 ) / fact<T>(t);
55
- for (int i = t - 1 ; i >= 0 ; i--) {
56
- F[i] = F[i + 1 ] * T (i + 1 );
57
- }
58
- init = true ;
59
- }
60
- return F[n];
61
- }
62
- template <typename T>
63
- T small_inv (int n) {
64
- static std::vector<T> F (maxn);
65
- static bool init = false ;
66
- if (!init) {
67
- for (int i = 1 ; i < maxn; i++) {
68
- F[i] = rfact<T>(i) * fact<T>(i - 1 );
69
- }
70
- init = true ;
71
- }
72
- return F[n];
73
- }
74
32
}
75
33
#endif // CP_ALGO_MATH_COMMON_HPP
You can’t perform that action at this time.
0 commit comments