-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMontgomeryInteger.cpp
More file actions
165 lines (140 loc) · 3.97 KB
/
MontgomeryInteger.cpp
File metadata and controls
165 lines (140 loc) · 3.97 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
160
161
162
163
164
165
#ifndef _LIB_MONTGOMERY_INTEGER
#define _LIB_MONTGOMERY_INTEGER
#include <bits/stdc++.h>
#if __cplusplus < 201300
#error required(c++14)
#endif
namespace lib {
using namespace std;
namespace {
template <typename U, U Mod>
struct MontgomeryIntegerImpl {
using S = make_signed_t<U>;
using T = make_unsigned_t<U>;
typedef MontgomeryIntegerImpl<U, Mod> type;
constexpr static T mod = (T)Mod;
T x;
typedef U type_int;
typedef uint64_t large_int;
constexpr static T get_r() {
T ret = Mod;
for(int i = 0; i < 4; i++)
ret *= 2 - mod * ret;
return ret;
}
constexpr static T r = get_r();
constexpr static T n2 = -large_int(mod) % mod;
static_assert(r * mod == 1, "assert(r * mod == 1)");
static_assert(mod < (1 << 30), "assert(mod < 2^30)");
static_assert(mod % 2 == 1, "assert(mod % 2 == 1)");
constexpr MontgomeryIntegerImpl() : x(0) {}
constexpr MontgomeryIntegerImpl(large_int y)
: x(reduce(large_int(y % mod + mod) * n2)) {}
constexpr inline static T reduce(large_int y) {
return (y + large_int(T(y) * T(-r)) * mod) >> 32;
}
constexpr inline type &operator+=(const type &rhs) {
if(S(x += rhs.x - 2 * mod) < 0) x += 2 * mod;
return *this;
}
constexpr inline type &operator-=(const type &rhs) {
if(S(x -= rhs.x) < 0) x += 2 * mod;
return *this;
}
constexpr inline type &operator*=(const type &rhs) {
x = reduce(large_int(x) * rhs.x);
return *this;
}
constexpr inline type &operator/=(const type &rhs) {
return *this *= rhs.inverse();
}
constexpr inline type inverse() const {
return (*this).power(large_int(mod - 2));
}
constexpr type &operator^=(large_int p) {
return *this = power(p);
}
constexpr type &operator++() {
return *this += type(1);
}
constexpr type &operator--() {
return *this -= type(1);
}
constexpr type operator++(int unused) {
type res = *this;
++(*this);
return res;
}
constexpr type operator--(int unused) {
type res = *this;
--(*this);
return res;
}
friend constexpr type operator+(const type &lhs, const type &rhs) {
type res = lhs;
return res += rhs;
}
friend constexpr type operator-(const type &lhs, const type &rhs) {
type res = lhs;
return res -= rhs;
}
friend constexpr type operator*(const type &lhs, const type &rhs) {
type res = lhs;
return res *= rhs;
}
friend constexpr type operator/(const type &lhs, const type &rhs) {
type res = lhs;
return res /= rhs;
}
friend constexpr type operator^(const type &lhs, large_int rhs) {
type res = lhs;
return res ^= rhs;
}
friend constexpr type power(const type &lhs, large_int rhs) {
return lhs.power(rhs);
}
constexpr type operator-() const {
return type() - *this;
}
constexpr type power(large_int rhs) const {
type ret(1), mul(*this);
while(rhs > 0) {
if(rhs&1) ret *= mul;
mul *= mul;
rhs /= 2;
}
return ret;
}
constexpr T get() const {
T ret = reduce(x);
return ret >= mod ? ret - mod : ret;
}
friend bool operator==(const type &lhs, const type &rhs) {
return lhs.get() == rhs.get();
}
friend bool operator!=(const type &lhs, const type &rhs) {
return !(lhs == rhs);
}
explicit operator int() const { return get();; }
explicit operator int64_t() const { return get(); }
explicit operator long long() const { return get(); }
explicit operator double() const { return get(); }
explicit operator long double() const { return get(); }
friend ostream &operator<<(ostream &output, const type &var) {
return output << var.get();
}
friend istream &operator>>(istream &input, type &var) {
T y;
cin >> y;
var = type(y);
return input;
}
};
} // namespace
template <typename T, T... Mods>
using MontgomeryInteger = MontgomeryIntegerImpl<T, Mods...>;
template <int32_t... Mods> using Mont32 = MontgomeryInteger<int32_t, Mods...>;
using MontP = Mont32<(int32_t)1e9+7>;
using MontNTT = Mont32<998244353>;
} // namespace lib
#endif