-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModulo_constexpr.cpp
More file actions
159 lines (137 loc) · 2.74 KB
/
Modulo_constexpr.cpp
File metadata and controls
159 lines (137 loc) · 2.74 KB
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
//c++14
class Mod
{
public:
using value_type = long long;
private:
static const value_type MODULO = 1e9+7;
value_type value;
constexpr value_type Normalize(value_type x) const
{
return x<0?(x%MODULO+MODULO):(x%MODULO);
}
public:
constexpr Mod():value(0){}
constexpr Mod(const value_type &val):value(Normalize(val)) {}
constexpr explicit operator value_type () const
{
return value;
}
constexpr const Mod operator -() const
{
return Mod(MODULO - value);
}
constexpr const Mod operator +(const Mod &rhs) const
{
return Mod(value + rhs.value);
}
constexpr const Mod operator -(const Mod &rhs) const
{
return Mod(value + (-rhs).value);
}
constexpr const Mod operator *(const Mod &rhs) const
{
return Mod(value * rhs.value);
}
constexpr Mod &operator +=(const Mod &rhs)
{
value = Normalize(value + rhs.value);
return *this;
}
constexpr Mod &operator -=(const Mod &rhs)
{
value = Normalize(value - rhs.value);
return *this;
}
constexpr Mod &operator *=(const Mod &rhs)
{
value = Normalize(value * rhs.value);
return *this;
}
constexpr Mod pow(value_type p) const;
constexpr Mod inv() const
{
return pow(MODULO-2);
}
constexpr const Mod operator /(const Mod &rhs) const
{
return *this * rhs.inv();
}
constexpr Mod &operator /=(const Mod &rhs)
{
return *this = *this / rhs;
}
constexpr bool operator ==(const Mod &rhs)
{
return value == rhs.value;
}
};
constexpr Mod Mod::pow(value_type p) const
{
Mod tmp=1, mult=*this;
while(p)
{
if((p&1)>0) tmp*=mult;
p>>=1;
mult*=mult;
}
return tmp;
}
namespace std
{
ostream& operator<<(ostream& os, const Mod mod)
{
os<<(typename Mod::value_type)mod;
return os;
}
};
template<size_t N>
struct Factorial
{
private:
Mod ary[N];
public:
constexpr explicit Factorial():ary()
{
ary[0]=1;
for(size_t i=1;i<N;i++)
ary[i]=ary[i-1]*i;
}
constexpr size_t size() const { return N; }
constexpr Mod operator[] (const int id) const
{
return ary[id];
}
};
template<size_t N>
class FactorialInv
{
private:
Mod ary[N];
public:
constexpr explicit FactorialInv(const Factorial<N> &fact):ary()
{
for(size_t i=0;i<N;i++)
ary[i]=fact[i].inv();
}
//FactorialInv& operator=(FactorialInv&&)=default;
constexpr Mod operator[] (const int id) const
{
return ary[id];
}
};
template<size_t N, size_t M = N>
class Combination
{
private:
const Factorial<N> fact;
const FactorialInv<M> fact_inv;
public:
constexpr Combination(const Factorial<N> fact_, const FactorialInv<M> fact_inv_)
:fact(fact_),fact_inv(fact_inv_)
{}
constexpr Mod operator()(const int n, const int m) const
{
return (fact)[n] * (fact_inv)[m] * (fact_inv)[n-m];
}
};