[UOJ 310]Chocolate
Difficulty Ratings
Idea Creativity: 3/5Techniques Used: 4/5
Implementation : 3/5
This time I will share some standard tricks which I learnt recently.
Simplified Statement
You have N≤1e6 chocolates, each having value ai<2M, where M=20. FInd the number of ways to choose two subsets of the chocolates A and B such that:1) Their intersection is empty set.
2) The xor sum of elements in A∪B is 0.
Find modulo 998244353.
Solution
This problem screams Fast Walsh-Hadamard Transform (FWT). However, it is not obvious how to use it. For simplicity, let's define some symbols.-f(A) is the FWT of A.
-f−1(A) is the inverse FWT of A.
-A⊕B is the xor-convolution of A and B.
-A⋅B is the dot-product of A and B.
So we know that
-f−1(f(A)⋅f(B))=A⊕B
Also some properties are
-f(A+B)=f(A)+f(B) (Same holds for f−1 too.)
-f−1(f(A))=A
So lets see how to use this. Define Ai to be the array where all values are 0 except Ai[0], which is 1 and Ai[ai], which is 2. Then, the answer is the value of the 0−th element in f−1(f(A1)⋅f(A2)⋅f(A3)⋯f(An)). Define U to be f(A1)⋅f(A2)⋅f(A3)⋯f(An). Obviously, directly evaluating that would result in TLE. Lets look into f(Ai). We realise that the values of each element in the array is either 3 or −1. So, lets take a position j, and lets say xj of them has Ai[j]=3 and n−xj of them has Ai[j]=−1. Then we know that U[j]=3xj×−1n−xj.
Let's look at f(A1)+f(A2)+f(A3)⋯f(An). Call it V. So we realise that V=f(A1+A2+A3+⋯An), so V can be evaluated easily. Look closer to V[j]. We see that V[j]=3×xj+(−1)×(n−xj). That is very cool, because we can now find out xj quick, which means we can find U quick too! It remains to compute f−1(U)[0] and we are done!
留言
張貼留言