-
Notifications
You must be signed in to change notification settings - Fork 0
/
convolution.go
231 lines (221 loc) · 5.02 KB
/
convolution.go
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package convolution
import "math/bits"
func powMod(a, e, mod int) int {
res, m := 1, a
for ; e > 0; e >>= 1 {
if e&1 != 0 {
res = res * m % mod
}
m = m * m % mod
}
return res
}
type Convolution struct {
mod, primeRoot, rank2 int
root, iroot, rate2, irate2, rate3, irate3 []int
}
func NewConvolution(mod, primeRoot int) *Convolution {
rank2 := bits.TrailingZeros(uint(mod - 1))
if rank2 < 3 {
panic("Panic!")
}
root := make([]int, rank2+1)
iroot := make([]int, rank2+1)
rate2 := make([]int, rank2-2+1)
irate2 := make([]int, rank2-2+1)
rate3 := make([]int, rank2-3+1)
irate3 := make([]int, rank2-3+1)
root[rank2] = powMod(primeRoot, (mod-1)>>rank2, mod)
iroot[rank2] = powMod(root[rank2], mod-2, mod)
for i := rank2 - 1; i >= 0; i-- {
root[i] = root[i+1] * root[i+1] % mod
iroot[i] = iroot[i+1] * iroot[i+1] % mod
}
prod, iprod := 1, 1
for i := 0; i <= rank2-2; i++ {
rate2[i] = root[i+2] * prod % mod
irate2[i] = iroot[i+2] * iprod % mod
prod = prod * iroot[i+2] % mod
iprod = iprod * root[i+2] % mod
}
prod, iprod = 1, 1
for i := 0; i <= rank2-3; i++ {
rate3[i] = root[i+3] * prod % mod
irate3[i] = iroot[i+3] * iprod % mod
prod = prod * iroot[i+3] % mod
iprod = iprod * root[i+3] % mod
}
return &Convolution{mod, primeRoot, rank2, root, iroot, rate2, irate2, rate3, irate3}
}
func ceilPow2(n int) int {
x := 0
for 1<<x < n {
x++
}
return x
}
func (q *Convolution) butterfly(a []int) {
mod := q.mod
n := len(a)
h := ceilPow2(n)
l := 0
for l < h {
if h-l == 1 {
p := 1 << (h - l - 1)
rot := 1
for s := 0; s < (1 << l); s++ {
offset := s << (h - l)
for i := 0; i < p; i++ {
l := a[i+offset]
r := a[i+offset+p] * rot % mod
a[i+offset] = (l + r) % mod
a[i+offset+p] = (l - r + mod) % mod
}
if s+1 != (1 << l) {
rot = rot * q.rate2[bits.TrailingZeros(^uint(s))] % mod
}
}
l++
} else {
p := 1 << (h - l - 2)
rot := 1
imag := q.root[2]
for s := 0; s < (1 << l); s++ {
rot2 := rot * rot % mod
rot3 := rot2 * rot % mod
offset := s << (h - l)
for i := 0; i < p; i++ {
mod2 := mod * mod
a0 := a[i+offset]
a1 := a[i+offset+p] * rot
a2 := a[i+offset+2*p] * rot2
a3 := a[i+offset+3*p] * rot3
a1na3imag := (a1 + mod2 - a3) % mod * imag
na2 := mod2 - a2
a[i+offset] = (a0 + a2 + a1 + a3) % mod
a[i+offset+p] = (a0 + a2 + (2*mod2 - a1 - a3)) % mod
a[i+offset+2*p] = (a0 + na2 + a1na3imag) % mod
a[i+offset+3*p] = (a0 + na2 + (mod2 - a1na3imag)) % mod
}
if s+1 != (1 << l) {
rot = rot * q.rate3[bits.TrailingZeros(^uint(s))] % mod
}
}
l += 2
}
}
}
func (q *Convolution) butterflyInv(a []int) {
mod := q.mod
n := len(a)
h := ceilPow2(n)
l := h
for l > 0 {
if l == 1 {
p := 1 << (h - l)
irot := 1
for s := 0; s < (1 << (l - 1)); s++ {
offset := s << (h - l + 1)
for i := 0; i < p; i++ {
l := a[i+offset]
r := a[i+offset+p]
a[i+offset] = (l + r) % mod
a[i+offset+p] = (l - r + mod) % mod
}
if s+1 != (1 << (l - 1)) {
irot = irot * q.irate2[bits.TrailingZeros(^uint(s))] % mod
}
}
l--
} else {
p := 1 << (h - l)
irot := 1
iimag := q.iroot[2]
for s := 0; s < (1 << (l - 2)); s++ {
irot2 := irot * irot % mod
irot3 := irot2 * irot % mod
offset := s << (h - l + 2)
for i := 0; i < p; i++ {
a0 := a[i+offset]
a1 := a[i+offset+p]
a2 := a[i+offset+2*p]
a3 := a[i+offset+3*p]
a2na3iimag := (mod + a2 - a3) * iimag % mod
a[i+offset] = (a0 + a1 + a2 + a3) % mod
a[i+offset+p] = (a0 + (mod - a1) + a2na3iimag) * irot % mod
a[i+offset+2*p] = (a0 + a1 + (mod - a2) + (mod - a3)) * irot2 % mod
a[i+offset+3*p] = (a0 + (mod - a1) + (mod - a2na3iimag)) * irot3 % mod
}
if s+1 != (1 << (l - 2)) {
irot = irot * q.irate3[bits.TrailingZeros(^uint(s))] % mod
}
}
l -= 2
}
}
iz := powMod(n, mod-2, mod)
for i := 0; i < n; i++ {
a[i] = a[i] * iz % mod
}
}
func (q *Convolution) convolveFFT(a []int, b []int) []int {
mod := q.mod
finalsz := len(a) + len(b) - 1
z := 1
for z < finalsz {
z *= 2
}
lena, lenb := len(a), len(b)
la := make([]int, z)
lb := make([]int, z)
for i := 0; i < lena; i++ {
la[i] = a[i]
}
for i := 0; i < lenb; i++ {
lb[i] = b[i]
}
q.butterfly(la)
q.butterfly(lb)
for i := 0; i < z; i++ {
la[i] *= lb[i]
la[i] %= mod
}
q.butterflyInv(la)
return la[:finalsz]
}
func (q *Convolution) ConvolutionNaive(a []int, b []int) []int {
mod := q.mod
n := len(a)
m := len(b)
ans := make([]int, n+m-1)
if n < m {
for j := 0; j < m; j++ {
for i := 0; i < n; i++ {
ans[i+j] += a[i] * b[j] % mod
ans[i+j] %= mod
}
}
} else {
for i := 0; i < n; i++ {
for j := 0; j < m; j++ {
ans[i+j] += a[i] * b[j] % mod
ans[i+j] %= mod
}
}
}
return ans
}
func (q *Convolution) Convolve(a []int, b []int) []int {
n := len(a)
m := len(b)
if n == 0 || m == 0 {
return []int{}
}
if m < n {
n = m
}
if n <= 60 {
return q.ConvolutionNaive(a, b)
}
return q.convolveFFT(a, b)
}