0. Contents
1. What is FWT
FWT called "Fast Walsh Transform: Fast Walsh Transform". Can be used to solve the problem bit computing convolution.
What bit computing convolution it? We consider the general convolution, namely:
Bitwise subscript is the convolution operation convolution bits (here, or C ++, and the mark, with the exclusive-OR \ (\ Oplus \) ):
2. FWT how do
For convenience, we will assume that all lengths are equal vectors as \ (2 \) is the integer power, i.e. the length \ (n-m = 2 ^ \) . High filled with 0.
Provided a vector \ (A \) after FWT been \ (FWT (A) \) , the ultimate goal of the FWT is satisfied: \ (FWT (C) = FWT (A) \ CDOT FWT (B) \) , wherein the dot represents each multiplied vectors: \ (a \ cdot B = (A_0B_0, A_1B_1, ..., A_iB_i, ...) \) .
FWT with respective processing method for the three bit operation:
2.1. Or convolution
We found that the presence or nature of operations as follows:
If the binary understood as a set of 01, we can understand the nature of the collection and use of the above method. I discover this is a pseudo-distributive law.
With this property we can be constructed:
You can find:
After this is done can be obtained corresponding to the multiplication \ (C \) , then one can get the inverse transform \ (C \) .
So the question becomes how to carry out such a transformation.
Consider a partition (or referred to as DP) approach:
\ (F (I, J) \) : satisfying only low \ (I \) bits may \ (J \) different, and the \ (J \) to give after or \ (J \) and the number of the corresponding subscript.
Or it may be described as:
Consider how the transfer, i.e. from \ (f (i-1) \) was transferred to \ (F (I) \) . In this case only the first \ (i \) position to lift the restrictions. The nature or, if the first \ (I \) bit is 0, then the first \ (I \) or after operations remains a need for a 0 bit, it is only from the \ (I \) bit 0 \ (F ( i, j) \) transferred to; if the first \ (I \) bit is 1, then the first \ (I \) bit or the operation of the total obtained is 1, may not consider section \ (I \) bit from \ ( f (i, j) \) and \ (f (i, j + 2 ^ i) \) transferred to.
Put a picture to understand this:
the way can be transferred as follows:
Transformation:
Inverse transform is a special contribution deduction:
It can be found in \ (F (0, I) = A_i \) , and the transfer may scroll array optimization.
Such FWT is \ (O (the n-\ log_2n) \) .
2.2. Convolution
Convolution with a convolution or very similar, and therefore can be analyzed in a similar way. Here are only the states and transitions:
Transformation:
Inverse transformation:
2.3. Convolution XOR
We also consider the nature of XOR.
Set \ (count (i) \) of \ (I \) a binary \ (1 \) digit, \ (I \ otimes J = COUNT (I \ & J) \ BMOD 2 \) of the exclusive or properties :
I.e. equal to the parity. Set \ (COUNT (J \ & I) = A, COUNT (K \ & I) = B, COUNT (J \ & K) = C \) , then the left side of parity by the \ (a + b \) decision, the right parity of the \ (a + b-2c \ ) determined, the parity can be found on both sides is equal.
Speaking of parity, we can think of \ (--1 \) of power. So set:
Then you can take a look at the result of this conversion up:
We reached the goal. Then take a look at how transformation. Continue to consider DP:
It can be found from \ (f (i-1) \) to \ (f (i) \) when only the first \ (i \) bits are \ (1 \) will it take for \ (j \ otimes K \) change parity, i.e. multiplied by a plurality -1.
Thus transferred, the final \ (j \ otimes k = 0 \) case will be multiplied by -1 even number of times, and \ (j \ otimes k = 1 \) case will be multiplied by -1 odd number of times, the final the answer is correct.
According to this, the forward transform:
Inverse transform, use the "problems and poor" Mathematical Olympiad primary conclusions :
It should be noted that the inverse transformation or convolution there is a different "class FFT" wording, namely inverse transform more than just a positive transformation in the last multi except \ (the n-\) (XOR FWT and in fact there are many similarities FFT place, can K-band FWT find the answers).
2.4. Example
Luo Gu P4717 . Three kinds of FWT family bucket.
Reference Code:
const int mod = 998244353, inv2 = 499122177;
const int MAXSIZ = 5e5 + 5;
int A[MAXSIZ], B[MAXSIZ], C[MAXSIZ], a[MAXSIZ], b[MAXSIZ];
int N, M;
int fix( const int x ) { return ( x % mod + mod ) % mod; }
namespace OR
{
void FWT( int *f, const int m )
{
for( int s = 2 ; s <= M ; s <<= 1 )
for( int i = 0, t = s >> 1 ; i < M ; i += s )
for( int j = i ; j < i + t ; j ++ )
f[j + t] = fix( f[j + t] + f[j] * m ) % mod;
}
}
namespace AND
{
void FWT( int *f, const int m )
{
for( int s = 2 ; s <= M ; s <<= 1 )
for( int i = 0, t = s >> 1 ; i < M ; i += s )
for( int j = i ; j < i + t ; j ++ )
f[j] = fix( f[j] + f[j + t] * m ) % mod;
}
}
namespace XOR
{
void FWT( int *f, const int m )
{
int t1, t2;
for( int s = 2 ; s <= M ; s <<= 1 )
for( int i = 0, t = s >> 1 ; i < M ; i += s )
for( int j = i ; j < i + t ; j ++ )
{
t1 = f[j], t2 = f[j + t];
if( m > 0 )
f[j] = ( t1 + t2 ) % mod,
f[j + t] = fix( t1 - t2 );
else
f[j] = 1ll * ( t1 + t2 ) % mod * inv2 % mod,
f[j + t] = 1ll * fix( t1 - t2 ) * inv2 % mod;
}
}
}
void cal( void ( *fwt ) ( int*, int ) ) //函数指针的写法,主要是方便。
{
for( int i = 0 ; i < M ; i ++ ) A[i] = a[i], B[i] = b[i];
fwt( A, 1 ), fwt( B, 1 );
for( int i = 0 ; i < M ; i ++ ) C[i] = 1ll * A[i] * B[i] % mod;
fwt( C, -1 );
for( int i = 0 ; i < M ; i ++ ) write( C[i] ), putchar( i == M - 1 ? '\n' : ' ' );
}
3. FST
3.1. FST how do
It is not good listening.
FST resolved quickly transform a subset of the class is a subset of questions convolution, namely:
This convolution or convolution of the difference is that there may be an intersection or convolutional (not required \ (J \ & K = 0 \) ), but not have a subset of the convolution.
Note that this is equivalent to a restricted subset convolution \ (| S | + | T | = | the U-| \) . So we can add a dimension to limit the size of the state:
\ (f (i, U) \) : size \ (i \) the set \ (U \) the contribution of all subsets, \ (G \) and \ (h \) Similarly conversion.
This information may be obtained directly by the forward transform FWT.
Therefore Transfer:
After the transfer is complete inverse transform FWT need to come back, then do not meet the clearance requirements (set size mismatch) is.
3.2. Example
[CF914G] Sum at The Fibonacci , FWT + FST, comes the question a solution .