-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNTT.cpp
More file actions
74 lines (69 loc) · 2.22 KB
/
Copy pathNTT.cpp
File metadata and controls
74 lines (69 loc) · 2.22 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
template< typename Mint >
struct NumberTheoreticTransformFriendlyModInt {
vector< Mint > dw, idw;
int max_base;
Mint root;
NumberTheoreticTransformFriendlyModInt(){
auto tmp = mod - 1;
max_base = 0;
while(tmp % 2 == 0) tmp >>= 1, max_base++;
root = 2;
while(root.pow(root,(mod - 1) >> 1) == 1) root += 1;
assert(root.pow(root,mod - 1) == 1);
dw.resize(max_base);
idw.resize(max_base);
for(int i = 0; i < max_base; i++) {
dw[i] = -root.pow(root,(mod - 1) >> (i + 2));
idw[i] = Mint(1) / dw[i];
}
}
void ntt(vector< Mint > &a){
const int n = (int) a.size();
assert((n & (n - 1)) == 0);
assert(__builtin_ctz(n) <= max_base);
for(int m = n; m >>= 1;) {
Mint w = 1;
for(int s = 0, k = 0; s < n; s += 2 * m) {
for(int i = s, j = s + m; i < s + m; ++i, ++j) {
auto x = a[i], y = a[j] * w;
a[i] = x + y, a[j] = x - y;
}
w *= dw[__builtin_ctz(++k)];
}
}
}
void intt(vector< Mint > &a, bool f = true){
const int n = (int) a.size();
assert((n & (n - 1)) == 0);
assert(__builtin_ctz(n) <= max_base);
for(int m = 1; m < n; m *= 2) {
Mint w = 1;
for(int s = 0, k = 0; s < n; s += 2 * m) {
for(int i = s, j = s + m; i < s + m; ++i, ++j) {
auto x = a[i], y = a[j];
a[i] = x + y, a[j] = (x - y) * w;
}
w *= idw[__builtin_ctz(++k)];
}
}
if(f) {
Mint inv_sz = Mint(1) / n;
for(int i = 0; i < n; i++) a[i] *= inv_sz;
}
}
vector< Mint > multiply(vector< Mint > a, vector< Mint > b) {
int need = a.size() + b.size() - 1;
int nbase = 1;
while((1 << nbase) < need) nbase++;
int sz = 1 << nbase;
a.resize(sz, 0);
b.resize(sz, 0);
ntt(a);
ntt(b);
Mint inv_sz = Mint(1) / sz;
for(int i = 0; i < sz; i++) a[i] *= b[i] * inv_sz;
intt(a, false);
a.resize(need);
return a;
}
};