Power Swapper (Small)
Time limit5sMemory limit512 MB
Count the ordered swap sequences that sort the permutation when each block size is used at most once on aligned ranges.
- Level
Medium6 of 10
- Topics
- Backtracking, Brute force, Simulation
- Solved
- No attempts yet
Problem
In a parallel universe people are crazy about powers of two, and they sort permutations of the numbers 1 to with the following swap rule.
- A range of adjacent numbers is valid only when its starting position is a multiple of . Positions are counted from 0.
- A swap of size exchanges two distinct valid ranges whose lengths are both .
To sort a permutation you may use a swap of size at most once for each with . Swapping a range with itself is not allowed.
For example, the permutation of the numbers 1 to can be sorted like this.
- : exchange the ranges and with a swap of size 2.
- : exchange and with a swap of size 0.
- : exchange and with a swap of size 1.
- : sorting is done.
This run used the sizes 0, 1 and 2 at most once each, and every exchanged range started at a position that is a multiple of its own length.
Count the ways to sort the given permutation under these rules. A way is an ordered sequence of swaps, and two ways are the same only when the sequences are identical. When the permutation is already sorted, the empty sequence that performs no swap counts as one way.
Input
The first line contains the number of test cases . test cases follow. The first line of each test case contains a single integer . The second line contains space separated integers, a permutation of .
Limits
Output
For each test case, print one line in the form Case #x: y, where is the test case number starting from 1 and is the number of ways to sort that permutation under the rules above.