-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModulo.cpp
More file actions
147 lines (129 loc) · 2.5 KB
/
Modulo.cpp
File metadata and controls
147 lines (129 loc) · 2.5 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
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)) {}
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);
}
Mod &operator +=(const Mod &rhs)
{
return *this = *this + rhs;
}
Mod &operator -=(const Mod &rhs)
{
return *this = *this - rhs;
}
Mod &operator *=(const Mod &rhs)
{
return *this = *this * rhs;
}
Mod pow(value_type p) const;
Mod inv() const
{
return pow(MODULO-2);
}
const Mod operator /(const Mod &rhs) const
{
return *this * rhs.inv();
}
Mod &operator /=(const Mod &rhs)
{
return *this = *this / rhs;
}
constexpr bool operator ==(const Mod &rhs)
{
return value == rhs.value;
}
};
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;
}
};
class Factorial
{
private:
vector<Mod> ary;
public:
explicit Factorial(const size_t size):ary(vector<Mod>(size))
{
ary[0]=1;
for(size_t i=1;i<size;i++)
ary[i]=ary[i-1]*i;
}
size_t size() const { return ary.size(); }
Mod operator[] (const int id) const
{
return ary[id];
}
};
class FactorialInv
{
private:
vector<Mod> ary;
public:
explicit FactorialInv(const Factorial &fact):ary(vector<Mod>(fact.size()))
{
for(size_t i=0;i<ary.size();i++)
ary[i]=fact[i].inv();
}
//FactorialInv& operator=(FactorialInv&&)=default;
Mod operator[] (const int id) const
{
return ary[id];
}
};
class Combination
{
private:
const Factorial *fact;
const FactorialInv *fact_inv;
public:
Combination(const Factorial &fact_, const FactorialInv &fact_inv_):fact(&fact_),fact_inv(&fact_inv_)
{}
Mod operator()(const int n, const int m) const
{
return (*fact)[n] * (*fact_inv)[m] * (*fact_inv)[n-m];
}
};