Number Theoretic Transform
Calculates the convolution of two polynomials modulo a prime number p which is NTT-friendly (mostly p=998244353).
Given two arrays A0,⋯,An−1 and B0,⋯,Bm−1 representing f(x)=i=0∑n−1Aixi and g(x)=i=0∑m−1Bixi respectively, the convolution
f(x)g(x)=i=0∑n+m−2Cixi
is defined as Ck=i=0∑kAiBk−imodp.
convolve
Arguments
f: list[int]: first array of coefficients of the polynomial f(x)
g: list[int]: second array of coefficients of the polynomial g(x)
Returns
list[int]: array of coefficients of the polynomial f(x)g(x) modulo p
Complexities
- time: O((n+m)log(n+m))
- space: O(n+m)
Code Test