[UOJ 310]Chocolate


Difficulty Ratings

Idea Creativity: 3/5
Techniques Used: 4/5
Implementation : 3/5
This time I will share some standard tricks which I learnt recently.

Simplified Statement

You have N1e6 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 AB 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.
-f1(A) is the inverse FWT of A.
-AB is the xor-convolution of A and B.
-AB is the dot-product of A and B.
So we know that
-f1(f(A)f(B))=AB
Also some properties are
-f(A+B)=f(A)+f(B) (Same holds for f1 too.)
-f1(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 0th element in f1(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 nxj of them has Ai[j]=1. Then we know that U[j]=3xj×1nxj.

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)×(nxj). That is very cool, because we can now find out xj quick, which means we can find U quick too! It remains to compute f1(U)[0] and we are done!

Comments

I think the idea of constructing V is very interesting. I thought FWT problems are boring... You can also try CF 1119H, which is of similar topic.

留言