O(n log log n) implements FGT and FLT (Fast GCD/LCM Transformation)

In this article, the author started to entertain himself after he could not understand the divide and conquer FFT.
When he saw a question and asked for the correct solution, he launched a strange transformation and found that this is a very transformation. I am the same as the strange thing derived from the correct solution , but still want to write about the idea. . .
(I hope the author of the original title will not come to D me)

min convolution and max convolution

Consider two convolutions
\(C_{min(i,j)} = \sum_{i = 1}^{n}\sum_{j = 1}^{n} A_{i}B_{j}\)
\ (C_{max(i,j)} = \sum_{i = 1}^{n}\sum_{j = 1}^{n} A_{i}B_{j}\)
Ordinary convolution, that is, we After adding the subscript, add the product of the two numbers to the subscript. Here, the operation of the subscript takes min and max.

Like violent convolution, these two convolutions can be solved violently \(n^2\) , but is there a better way?

The appearance of a transformation is often to turn the convolution into a dot product, and then use the dot product to inversely solve the coefficients of the polynomial.
For example, FFT is to insert the function value, and use the result of the multiplication of the two functions. The value on a certain x coordinate That is, corresponding to the product of two function values, and then use the special properties of the unit root to solve the coefficient.
For example, the XOR convolution of FWT, what we want to achieve is \(tf(A)* tf(B) = tf(C) \) , grouped by the highest bit is 0 or 1, then it is \(C = (A_{0}*B_{0} + A_{1}*B_{1},A_{0} * B_{1} + A_{1} * B_{0})\) , according to the dot product equal to the answer after convolution, construct two multiplications so that each of them can be obtained, which is \((A_{0} + A_{1} )(B_{0} + B_{1}) = A_{0}B_{0} + A_{0}B_{1} + A_{1}B_{0} + A_{1}B_{1}\) and \((A_{0} - A_{1})(B_{0} - B_{1}) = A_{0}B_{0} - A_{1}B_{0} - A_{0}B_{ 1} + A_{1}B_{1}\)
Adding and dividing two is the first half, subtracting and dividing by two is the second half, and the two transformations are \((tf(A_{0}) + tf(A_{1}),tf(A_{0}) - tf(A_{1}))\) Complexity \(O(n \log n)\) Because \(n\) is often \(2 ^{k}\) So the complexity will be written as \(O(2^{k}k)\)
So we try to do the same with this convolution
(However, many transformation methods are not known why they are tried out)

For min convolution ,
we consider the transformation \(C(i)\) as \(SC(i)\) , \(SC(i)\) represents \(x>=i\) \ (C(x)\ ) , \(SA(i)\) is the same as \(SB(i)\) Then
you can find that \(SA(i) * SB(i) = SC(i)\) , and the final difference can be Restore the C array, the complexity is \(O(n)\)

For max convolution ,
we consider transforming \(C(i)\) as \(SC(i)\) , \(SC(i)\) represents \(x<=i\) \ (C(x)\ ) , \(SA(i)\) is the same as \(SB(i)\) Then
you can find that \(SA(i) * SB(i) = SC(i)\) , and the final difference can be Restore the C array, the complexity is \(O(n)\)

Multidimensional suffix/prefix sum in the sense of standard decomposition of arrays

If we take the standard decomposition of a number (that is, prime factorization) as the coordinates of this number, for example
\(p_{1}^{k_{1}}p_{2}^{k_2}p_{3}^{k_{ 3}}...p_{n}^{k_{n}}\)
Each number can be regarded as an infinite dimension, and the subscript of each dimension is a coordinate of the index of the prime factor

The multidimensional prefix sum of the array is
\(f(n) = \sum{d|n}a(d)\)
The multidimensional suffix sum of the array is
\(f(n) = \sum{n|d}a(d) \)
The simple approach can reach \(O(n \log n)\) , which can be used for many problems, that is, for a number to enumerate its multiples (using harmonic series to calculate the complexity is also an important skill)

We consider how to optimize this algorithm to \(O(n \log \log n)\)

Let's take the prefix sum as an example, start with the smallest prime factor 2, fix the exponents of all other prime numbers, and start to consider the following process
1:a(1)
2:a(2)
3:a(3)
4:a(4 )
5:a(5)
6:a(6)
7:a(7)
8:a(8)
9:a(9)
10:a(10)
initialization means that each array position stores the corresponding a(i)
The first operation 2, sweep from back to front, we accumulate each multiple of 2, see the following
1:a(1)
2:a(2) a(1)
3:a(3)
4:a (4) a(2) a(1)
5:a(5)
6:a(6) a(3)
7:a(7)
8:a(8) a(4) a(2) a(1 )
9:a(9)
10:a(10) a(5)

Secondary operation 3
1: a (1)
2: a (2) a (1)
3: a (3) a (1)
4: a (4) a (2) a (1)
5: a (5 ) )
6: a (6) a (3) a (2) a (1)
7: a (7)
8: a (8) a (4) a (2) a (1)
9: a (9) a (3) a (1)
10: a (10) a (5)

Third operation 5
1: a (1)
2: a (2) a (1)
3: a (3) a (1)
4: a (4) a (2) a (1)
5: a (5 ) ) a (1)
6: a (6) a (3) a (2) a (1)
7: a (7)
8: a (8) a (4) a (2) a (1)
9: a (9) a (3) a (1)
10: a (10) a (5) a (2) a (1)

Fourth operation 7
1: a (1)
2: a (2) a (1)
3: a (3) a (1)
4: a (4) a (2) a (1)
5: a (5 ) ) a (1)
6: a (6) a (3) a (2) a (1)
7: a (7) a (1)
8: a (8) a (4) a (2) a (1) )
9: a (9) a (3) a (1)
10: a (10) a (5) a (2) a (1)

The next prime number is 11, which is out of range, so the operation is complete, so far, we have completed the multidimensional prefix sum of the array

The following is the same example to understand the multidimensional suffix of the array and
the first operation 2, sweep 1 from back to front
: a(1) a(2) a(4) a(8)
2:a(2) a(4) a(8)
3:a(3) a(6)
4:a(4) a(8)
5:a(5) a(10)
6:a(6)
7:a(7)
8:a( 8)
9:a(9)
10:a(10)

Secondary operation 3
1: a (1) a (2) a (4) a (8) a (3) a (6) a (9)
2: a (2) a (4) a (8) a (6)
3: a (3) a (6) a (9)
4: a (4) a (8)
5: a (5) a (10)
6: a (6)
7: a (7)
8 : a (8)
9: a (9)
10: a (10)

Third operation 5
1: a (1) a (2) a (4) a (8) a (3) a (6) a (9) a (5) a (10)
2: a (2) a (4) a (8) a (6) a (10)
3: a (3) a (6) a (9)
4: a (4) a (8)
5: a (5) a (10)
6 : a (6)
7: a (7)
8: a (8)
9: a (9)
10: a (10)

Fourth operation 7
1: a (1) a (2) a (4) a (8) a (3) a (6) a (9) a (5) a (10) a (7)
2: a (2) a (4) a (8) a (6) a (10)
3: a (3) a (6) a (9)
4: a (4) a (8)
5: a (5) a (10)
6: a (6)
7: a (7)
8: a (8)
9: a (9)
10: a (10)

Since the enumeration of multiples for each prime number (according to the complexity of the Sieve of Eratosthenes) is \(O(n \log \log n)\) ,
the multidimensional prefix sum in the sense of the standard decomposition of the array is obtained, Suffix sum can be done \(O(n \log \log n)\)

Fast GCD Transformation

Since the GCD of the two numbers is to take min for each prime factor prime number under the standard decomposition of the two numbers, it is
\(p_{1}^{min(a_{1},b_{1}}p_{2} ^{min(a_{2},b_{2})}...p_{k}^{min(a_{k},b_{k})}\)
Similar to min convolution, the array is processed into a multi-dimensional suffix and , which is the positive transformation

Consider how to restore the transformed result back to the
sc array. The result of the dot product multiplication is
1: c(1) c(2) c(3) c(4) c(5) c(6) c(7) c(8) c(9) c(10)
2:c(2) c(4) c(6) c(8) c(10)
3:c(3) c(6) c(9)
4: c(4) c(8)
5:c(5) c(10)
6:c(6)
7:c(7)
8:c(8)
9:c(9)
10:c(10)
Consider how Transforming them back, we find that we can take our steps to accumulate the suffix sum in reverse order

That is, enumerate prime numbers from large to small, and enumerate subscripts from front to back

The result of multiplying the dot products in the first enumeration 7
sc array is
1: c(1) c(2) c(3) c(4) c(5) c(6) c(8) c(9 ) c(10)
2:c(2) c(4) c(6) c(8) c(10)
3:c(3) c(6) c(9)
4:c(4) c(8 )
5:c(5) c(10)
6:c(6)
7:c(7)
8:c(8)
9:c(9)
10:c(10)

Second enumeration 5
1:c(1) c(2) c(3) c(4) c(6) c(8) c(9)
2:c(2) c(4) c(6) c(8)
3:c(3) c(6) c(9)
4:c(4) c(8)
5:c(5) c(10)
6:c(6)
7:c(7)
8:c(8)
9:c(9)
10:c(10)

Third enumeration 3
1:c(1) c(2) c(4) c(8)
2:c(2) c(4) c(8)
3:c(3) c(6)
4: c(4) c(8)
5:c(5) c(10)
6:c(6)
7:c(7)
8:c(8)
9:c(9)
10:c(10)

Fourth enumeration 2
1:c(1)
2:c(2)
3:c(3)
4:c(4)
5:c(5)
6:c(6)
7:c(7)
8: c(8)
9:c(9)
10:c(10)

The complexity is also \(O(n \log \log n)\) , and we can find that the multidimensional suffix difference in the sense of the array standard decomposition is the inverse step of the prefix sum

At the same time, if you understand the Mobius inversion, it will list the equation
\(sc(n) = \sum_{n|d}c(d)\)
\(c(n) = \sum_{n|d} \mu(\frac{d}{n})sc(d)\)
That is, the difference is essentially a Mobius inversion of \(sc(i)\) , and we get a \( O(n \log \log n)\) The way to solve the Möbius inversion, not \(O(n \log n)\)

Fast LCM Transformation Fast least common multiple transformation

We find that LCM takes max for each prime factor of the two subscript standard decomposition, ie
\(p_{1}^{max(a_{1},b_{1})}p_{2}^{max(a_ {2},b_{2})}...p_{k}^{max(a_{k},b_{k})}\)
But due to the nature of LCM, the range of our subscripts will reach \(n^ {2}\) , which is not much better than violence, so it doesn't make much sense to discuss

If we force the limited length to be within the range of N, that is, transform it into a multidimensional prefix sum of the standard decomposition of the array, and finally restore the multidimensional prefix difference back, similar to FGT

Code for FGT

for(int i = 1 ; i <= tot ; ++i) {
    for(int j = N / prime[i] ; j >= 1 ; --j) {
        a[j] += a[j * prime[i]];
        b[j] += b[j * prime[i]]; 
    }    
}
for(int i = 1 ; i <= n ; ++i) {
    c[i] = a[i] * b[i];
}
for(int i = tot ; i >= 1 ; --i) {
    for(int j = 1 ; j <= N / prime[i] ; ++j) {
        c[j] -= c[j * prime[i]];
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326223523&siteId=291194637
log
log