Editorial for Canada Day Contest 2021 - 0-1


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Author: Aaeria

Chapter 1

There are (nx)=n!x!(nx)! ways of choosing x of the n bits. Each set of x bits has a x!(nx)!n! chance of being flipped.

For Subtask 1, use dynamic programming. Let dp[i][s] be the probability of having the string s after i moves. Use the transition dp[i][s]=x!(nx)!n!dp[i1][s] for every s generated by flipping x bits on s. Complexity: O((2n)2K)

For Subtask 2, generate a matrix p where p[s][t] is the chance of s becoming t in one second. Find the answer by calculating pK. Complexity: O((2n)3logK)

Chapter 2

Solution by arvindr9

p[s][t] depends only on the value of s xor t. Let pi[x] be the probability going from any s to (s xor x) after i seconds. Now you've reduced p from a 2D matrix into a 1D array of length 2n.

You can do an xor convolution of pa and pb to get pa+b. Now you can find pK and solve the problem with complexity O((2n)2logK), solving Subtask 3.

To solve Subtask 4, use the Fast Walsh–Hadamard transform to speed up the xor convolutions and find the answer in O(2nnlogK).

Chapter 3

p[s] is the same for all s that share a value of popcount(s). So you can actually compress p further into an array of length n. This optimization leads to an O(n2K) DP solution which alone is enough to solve subtasks 1-3.

Next, you'll need to return to the matrix technique from subtask 2. But this time, you have an algorithm with a complexity of O(n3logK) - enough to complete the problem.

Bonus: Even now you can take advantage of symmetries in the matrix for constant time improvements.


Comments

There are no comments at the moment.