FWT, FST entry

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:

\[C_k=\sum_{i+j=k}A_iB_j \]

  Bitwise subscript is the convolution operation convolution bits (here, or C ++, and the mark, with the exclusive-OR \ (\ Oplus \) ):

\ [\ Begin {aligned} \ text {convolution:} & C_k = \ sum_ {i \ & j = k} A_iB_j \\\ text {or convolutional:} & C_k = \ sum_ {i | j = k} A_iB_j \ exclusive or convolutional \\ text {:} & C_k = \ sum_ {i \ oplus j = k} A_iB_j \ end {aligned} \]

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:

\[(j|i=i)\land (k|i=i)\Rightarrow (j|k)|i=i \]

  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:

\[a_i=\sum_{j|i=i}A_j\\b_i=\sum_{j|i=i}B_j\\c_i=\sum_{j|i=i}C_j \]

  You can find:

\[\begin{aligned} a_ib_i &=\sum_{j|i=i}A_j\times \sum_{k|i=i}B_k\\ &=\sum_{j|i=i}\sum_{k|i=i}A_jB_k\\ &=\sum_{(j|k)|i=i}A_jB_k\\ &=c_i \end{aligned} \]

  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:

\[f(i,j)=\sum_{\lfloor\frac k{2^i}\rfloor=\lfloor\frac j{2^i}\rfloor,k|j=j} a_k \]

  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:
fwtand.JPG
  the way can be transferred as follows:
  Transformation:

\[\begin{aligned} f(i,j)&=f(i-1,j)\\ f(i,j+2^i)&=f(i-1,j)+f(i-1,j+2^i) \end{aligned} \]

  Inverse transform is a special contribution deduction:

\[\begin{aligned} f(i,j)&=f(i+1,j)\\ f(i,j+2^i)&=f(i+1,j+2^i)-f(i+1,j) \end{aligned} \]

  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:

\[f(i,j)=\sum_{\lfloor\frac k{2^i}\rfloor=\lfloor\frac j{2^i}\rfloor,k\&j=j} a_k \]

  Transformation:

\[\begin{aligned} f(i,j)&=f(i-1,j)+f(i-1,j+2^i)\\ f(i,j+2^i)&=f(i-1,j+2^i) \end{aligned} \]

  Inverse transformation:

\[\begin{aligned} f(i,j)&=f(i+1,j)-f(i+1,j+2^i)\\ f(i,j+2^i)&=f(i+1,j+2^i) \end{aligned} \]

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\otimes j)\oplus(i\otimes k)=i\otimes (j\oplus k) \]

  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:

\[\begin{aligned} a_i=\sum_j(-1)^{i\otimes j}A_j\\ b_i=\sum_j(-1)^{i\otimes j}B_j\\ c_i=\sum_j(-1)^{i\otimes j}C_j \end{aligned} \]

  Then you can take a look at the result of this conversion up:

\[\begin{aligned} a_ib_i &=\sum_j(-1)^{i\otimes j}A_j\times \sum_k(-1)^{i\otimes k}B_k\\ &=\sum_{i\otimes j=0}\sum_{i\otimes k=0}A_jB_k-\sum_{i\otimes j=1}\sum_{i\otimes k=0}A_jB_k-\sum_{i\otimes j=0}\sum_{i\otimes k=1}A_jB_k+\sum_{i\otimes j=1}\sum_{i\otimes k=1}A_jB_k\\ &=\sum_{i\otimes (j\oplus k)=0}A_jB_k-\sum_{i\otimes (j\oplus k)=1}A_jB_k\\ &=\sum_p(-1)^{i\otimes p}\sum_{j\oplus k=p}A_jB_k\\ &=\sum_p(-1)^{i\otimes p}C_p\\ &=c_i \end{aligned} \]

  We reached the goal. Then take a look at how transformation. Continue to consider DP:

\[f(i,j)=\sum_{\lfloor\frac k {2^i}\rfloor=\lfloor \frac j {2^i}\rfloor}(-1)^{j\otimes k}a_k \]

  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:

\[\begin{aligned} f(i,j)&=f(i-1,j)+f(i-1,j+2^i)\\ f(i,j+2^i)&=f(i-1,j)-f(i-1,j+2^i) \end{aligned} \]

  Inverse transform, use the "problems and poor" Mathematical Olympiad primary conclusions :

\[\begin{aligned} f(i,j)&=\frac{f(i+1,j)+f(i+1,j+2^i)} 2\\ f(i,j+2^i)&=\frac{f(i+1,j)-f(i+1,j+2^i)} 2 \end{aligned} \]

  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:

\[f(U)=\sum_{S,T\subseteq U, S\cup T=U, S\cap T=\varnothing} g(S)\times h(T) \]

  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:

\[f(i,U)=\sum_j^i\sum_{S\cup T=U}g(i,S)\times h(i-j,T) \]

  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 .

Guess you like

Origin www.cnblogs.com/crashed/p/12595954.html