-
Notifications
You must be signed in to change notification settings - Fork 2
/
NTT convolution and exponentiation (2 mods).cpp
207 lines (184 loc) · 4.34 KB
/
NTT convolution and exponentiation (2 mods).cpp
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
/*8<
@Title:
NTT integer convolution and exponentiation (2
mods) modules)
@Description:
Computes the convolution between the two
polynomials and.
@Time: $O(N \log{N})$
@Warning: This is pure magic !
>8*/
template <int _mod>
struct mint {
ll expo(ll b, ll e) {
ll ret = 1;
while (e) {
if (e % 2) ret = ret * b % _mod;
e /= 2, b = b * b % _mod;
}
return ret;
}
ll inv(ll b) { return expo(b, _mod - 2); }
using m = mint;
ll v;
mint() : v(0) {}
mint(ll v_) {
if (v_ >= _mod or v_ <= -_mod) v_ %= _mod;
if (v_ < 0) v_ += _mod;
v = v_;
}
m &operator+=(const m &a) {
v += a.v;
if (v >= _mod) v -= _mod;
return *this;
}
m &operator-=(const m &a) {
v -= a.v;
if (v < 0) v += _mod;
return *this;
}
m &operator*=(const m &a) {
v = v * ll(a.v) % _mod;
return *this;
}
m &operator/=(const m &a) {
v = v * inv(a.v) % _mod;
return *this;
}
m operator-() { return m(-v); }
m &operator^=(ll e) {
if (e < 0) {
v = inv(v);
e = -e;
}
v = expo(v, e);
// possivel otimizacao:
// cuidado com 0^0
// v = expo(v, e%(p-1));
return *this;
}
bool operator==(const m &a) { return v == a.v; }
bool operator!=(const m &a) { return v != a.v; }
friend istream &operator>>(istream &in, m &a) {
ll val;
in >> val;
a = m(val);
return in;
}
friend ostream &operator<<(ostream &out, m a) {
return out << a.v;
}
friend m operator+(m a, m b) { return a += b; }
friend m operator-(m a, m b) { return a -= b; }
friend m operator*(m a, m b) { return a *= b; }
friend m operator/(m a, m b) { return a /= b; }
friend m operator^(m a, ll e) { return a ^= e; }
};
const ll MOD1 = 998244353;
const ll MOD2 = 754974721;
const ll MOD3 = 167772161;
template <int _mod>
void ntt(vector<mint<_mod>> &a, bool rev) {
int n = len(a);
auto b = a;
assert(!(n & (n - 1)));
mint<_mod> g = 1;
while ((g ^ (_mod / 2)) == 1) g += 1;
if (rev) g = 1 / g;
for (int step = n / 2; step; step /= 2) {
mint<_mod> w = g ^ (_mod / (n / step)),
wn = 1;
for (int i = 0; i < n / 2; i += step) {
for (int j = 0; j < step; j++) {
auto u = a[2 * i + j],
v = wn * a[2 * i + j + step];
b[i + j] = u + v;
b[i + n / 2 + j] = u - v;
}
wn = wn * w;
}
swap(a, b);
}
if (rev) {
auto n1 = mint<_mod>(1) / n;
for (auto &x : a) x *= n1;
}
}
tuple<ll, ll, ll> ext_gcd(ll a, ll b) {
if (!a) return {b, 0, 1};
auto [g, x, y] = ext_gcd(b % a, a);
return {g, y - b / a * x, x};
}
template <typename T = ll>
struct crt {
T a, m;
crt() : a(0), m(1) {}
crt(T a_, T m_) : a(a_), m(m_) {}
crt operator*(crt C) {
auto [g, x, y] = ext_gcd(m, C.m);
if ((a - C.a) % g != 0) a = -1;
if (a == -1 or C.a == -1) return crt(-1, 0);
T lcm = m / g * C.m;
T ans =
a + (x * (C.a - a) / g % (C.m / g)) * m;
return crt((ans % lcm + lcm) % lcm, lcm);
}
};
template <typename T = ll>
struct Congruence {
T a, m;
};
template <typename T = ll>
T chinese_remainder_theorem(
const vector<Congruence<T>> &equations) {
crt<T> ans;
for (auto &[a_, m_] : equations) {
ans = ans * crt<T>(a_, m_);
}
return ans.a;
}
#define int long long
template <ll m1, ll m2>
vll merge_two_mods(const vector<mint<m1>> &a,
const vector<mint<m2>> &b) {
int n = len(a);
vll ans(n);
for (int i = 0; i < n; i++) {
auto cur = crt<ll>();
auto ai = a[i].v;
auto bi = b[i].v;
cur = cur * crt<ll>(ai, m1);
cur = cur * crt<ll>(bi, m2);
ans[i] = cur.a;
}
return ans;
}
vll convolution_2mods(const vll &a,
const vll &b) {
vector<mint<MOD1>> l(all(a)), r(all(b));
int N = len(l) + len(r) - 1, n = 1;
while (n <= N) n *= 2;
l.resize(n), r.resize(n);
ntt(l, false), ntt(r, false);
for (int i = 0; i < n; i++) l[i] *= r[i];
ntt(l, true);
l.resize(N);
vector<mint<MOD2>> l2(all(a)), r2(all(b));
l2.resize(n), r2.resize(n);
ntt(l2, false), ntt(r2, false);
rep(i, 0, n) l2[i] *= r2[i];
ntt(l2, true);
l2.resize(N);
return merge_two_mods(l, l2);
}
vll poly_exp(const vll &xs, ll k) {
vll ret(len(xs));
ret[0] = 1;
auto base = xs;
while (k) {
if (k & 1) ret = convolution_2mods(ret, base);
k >>= 1;
base = convolution_2mods(base, base);
}
return ret;
}