luogu#P12112. [NWRRC2024] Hanoi Towers Reloaded
[NWRRC2024] Hanoi Towers Reloaded
题目描述
The is a famous mathematical puzzle consisting of three rods and disks with diameters . Each of the three rods contains some disks, stacked in order of decreasing diameter from bottom to top, so that the smallest disk is always at the top. A valid move consists of taking the smallest disk from a rod and putting it on top of another rod. This move must preserve the sorted order: you can't put a larger disk onto a smaller one. The original puzzle's goal is to transfer all disks from one rod to another.
In this variation of the puzzle, you can only move the disks : you can move a disk between rods and , and between rods and , but not between rods and .
Given two configurations of this puzzle, find the minimum number of moves required to reach the second configuration starting from the first one. As this number might be large, print it modulo .
输入格式
Each test contains multiple test cases. The first line contains the number of test cases (). Descriptions of the test cases follow.
The first line of each test case contains an integer , denoting the number of disks involved ().
The second line contains integers , describing the initial configuration of the puzzle, where is the rod that contains the -th disk ().
The third line describes the final configuration of the puzzle in the same format.
It is guaranteed that the sum of over all test cases does not exceed .
输出格式
For each test case, print the minimum number of moves required to reach the second configuration from the first one, modulo .
It can be shown that any two configurations are reachable from each other in this variation of the puzzle.
4
1
1
3
2
3 3
2 1
3
3 2 1
1 2 3
4
2 1 3 2
2 1 3 2
2
7
20
0