Sort the strings using the sorting criteria .
If there is , you can swap and to get a better solution.
Can the case that and happen?
Find the minimum such that the binary shifting generator can produce all numbers from to .
Suppose in binary.
smallest group representation (SGR) = smallest number that start from 1 and can be used to make by binary shifting.
In this case, SGR = 1000100100
We can see that
Consider 1000111111
Consider 1001000011, its SGR = 1000011100 .
Number bigger than will be covered by
Given matrix , , and .
Construct the matrix such that
Consider some example
0 0 0
0 0 2
0 1 2
So from the and matrices, we can determine the relationship between elements in the matrix
Create a directed graph and do topological sort.
The problem is how do we actually build the relationship.
Cannot do naive brute-force because
Maintain a BIT
Consider the number from bottom to top (or from right to left)
0 1 0 2
Our BIT Tree: 0 0 0 0
must be placed in a position that there is exactly two holes before it the third place.
Our BIT Tree: 0 0 1 0
must be placed in a position that there is exactly zero holes the first element.
Our BIT Tree: 1 0 1 0
must be placed in a position that there is exactly one hole before it the fourth position.
Can easily find the position by maintaining a BIT of holes and do binary search.
Total complexity:
Count the number of pair of permutations in which two permutations (guess permution and solution permutation) in a pair has exactly matches ( fixed points).
Since the probability of each solution is the same we can fixed the solution to 1 2 3 … .
Can do dynamic programming to find the number of permutation that has exactly fixed points.
Using inclusion-exclusion principle.
// (# of ways to choose fixed point # of ways to choose other points (new fixed points can occur, but we are over counting))
Need BigInteger in Java or coding in Python.