-
Notifications
You must be signed in to change notification settings - Fork 6
/
convolution.rb
145 lines (121 loc) · 2.91 KB
/
convolution.rb
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
# usage :
#
# conv = Convolution.new(mod, [primitive_root])
# conv.convolution(a, b) #=> convolution a and b modulo mod.
#
class Convolution
def initialize(mod = 998_244_353, primitive_root = nil)
@mod = mod
cnt2 = bsf(@mod - 1)
e = (primitive_root || calc_primitive_root(mod)).pow((@mod - 1) >> cnt2, @mod)
ie = e.pow(@mod - 2, @mod)
es = [0] * (cnt2 - 1)
ies = [0] * (cnt2 - 1)
cnt2.downto(2){ |i|
es[i - 2] = e
ies[i - 2] = ie
e = e * e % @mod
ie = ie * ie % @mod
}
now = inow = 1
@sum_e = [0] * cnt2
@sum_ie = [0] * cnt2
(cnt2 - 1).times{ |i|
@sum_e[i] = es[i] * now % @mod
now = now * ies[i] % @mod
@sum_ie[i] = ies[i] * inow % @mod
inow = inow * es[i] % @mod
}
end
def convolution(a, b)
n = a.size
m = b.size
return [] if n == 0 || m == 0
h = (n + m - 2).bit_length
raise ArgumentError if h > @sum_e.size
z = 1 << h
a = a + [0] * (z - n)
b = b + [0] * (z - m)
batterfly(a, h)
batterfly(b, h)
c = a.zip(b).map{ |a, b| a * b % @mod }
batterfly_inv(c, h)
iz = z.pow(@mod - 2, @mod)
return c[0, n + m - 1].map{ |c| c * iz % @mod }
end
def batterfly(a, h)
1.upto(h){ |ph|
w = 1 << (ph - 1)
p = 1 << (h - ph)
now = 1
w.times{ |s|
offset = s << (h - ph + 1)
offset.upto(offset + p - 1){ |i|
l = a[i]
r = a[i + p] * now % @mod
a[i] = l + r
a[i + p] = l - r
}
now = now * @sum_e[bsf(~s)] % @mod
}
}
end
def batterfly_inv(a, h)
h.downto(1){ |ph|
w = 1 << (ph - 1)
p = 1 << (h - ph)
inow = 1
w.times{ |s|
offset = s << (h - ph + 1)
offset.upto(offset + p - 1){ |i|
l = a[i]
r = a[i + p]
a[i] = l + r
a[i + p] = (l - r) * inow % @mod
}
inow = inow * @sum_ie[bsf(~s)] % @mod
}
}
end
def bsf(x)
(x & -x).bit_length - 1
end
def calc_primitive_root(mod)
return 1 if mod == 2
return 3 if mod == 998_244_353
divs = [2]
x = (mod - 1) / 2
x /= 2 while x.even?
i = 3
while i * i <= x
if x % i == 0
divs << i
x /= i while x % i == 0
end
i += 2
end
divs << x if x > 1
g = 2
loop{
return g if divs.none?{ |d| g.pow((mod - 1) / d, mod) == 1 }
g += 1
}
end
private :batterfly, :batterfly_inv, :bsf, :calc_primitive_root
end
# [EXPERIMENTAL]
def convolution(a, b, mod: 998_244_353, k: 35, z: 99)
n = a.size
m = b.size
return [] if n == 0 || m == 0
raise ArgumentError if a.min < 0 || b.min < 0
format = "%0#{k}x" # "%024x"
sa = ""
sb = ""
a.each{ |x| sa << (format % x) }
b.each{ |x| sb << (format % x) }
zero = '0'
s = zero * z + ("%x" % (sa.hex * sb.hex))
i = -(n + m - 1) * k - 1
Array.new(n + m - 1){ (s[i + 1..i += k] || zero).hex % mod }
end