You have a quantum computer containing two binary strings ss and tt, both of length nn. Every second, xx of the nn bits on ss are randomly chosen and flipped. Find the probability that after KK seconds, ss will become tt.
See Also : Canada Day Contest 2021
The first line contains three integers, nn, xx, and KK.
The next line contains the string ss.
The last line contains the string tt.
Find the probability of turning ss into tt. If the probability is ABAB, print AB−1(mod109+7)AB−1(mod109+7). It can be proven that the result is rational.
Sample Input 1
4 1 2 0000 0000
Sample Output 1
Explanation For Sample 1
The probability is 1414.
Sample Input 2
4 2 1 0000 0000
Sample Output 2
Sample Input 3
6 4 3 010000 100000
Sample Output 3
Sample Input 4
6 1 43 010000 011110
Sample Output 4
Sample Input 5
6 3 0 010000 010000
Sample Output 5
Sample Input 6
1 1 9 0 1
Sample Output 6
There are (nx)=n!x!(n−x)!(nx)=n!x!(n−x)! ways of choosing xx of the nn bits. Each set of xx bits has a x!(n−x)!n!x!(n−x)!n! chance of being flipped.
For Subtask 1, use dynamic programming: let dp[i][s]dp[i][s] be the probability of having the string ss after ii moves. Use the transition dp[i][s]=∑x!(n−x)!n!dp[i−1][s′]dp[i][s]=∑x!(n−x)!n!dp[i−1][s′] for every s′s′ generated by flipping xx bits on ss. Complexity: O((2n)2K)O((2n)2K)
For Subtask 2, generate a matrix pp where p[s][t]p[s][t] is the chance of ss becoming tt in one second. Find the answer by calculating pKpK. Complexity: O((2n)3logK)O((2n)3logK)
p[s][t]p[s][t] depends only on the value of ss xor tt. Let Let pi[x]pi[x] be the probability going from any ss to (ss xor xx) after ii seconds. Now you’ve reduced pp from a 2d matrix into an 1d array of length 2n2n.
You can do an xor convolution of papa and pbpb to get pa+bpa+b. Now you can find pKpK and solve the problem with complexity O((2n)2logK)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)O(2nnlogK).
p[s]p[s] is the same for all ss that share a value of popcount(s)popcount(s). So you can actually compress pp further into an array of length nn. This optimization leads to an O(n2K)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)O(n3logK) – enough to complete the problem.
Bonus: Even now you can take advantage of symmetries in the matrix for constant time improvements.