#P10547. [THUPC 2024 决赛] 排列游戏
[THUPC 2024 决赛] 排列游戏
Background
An extra 1-second time limit is provided.
Problem Description
There are cells arranged in a row, numbered from left to right as . Each cell has a number card. Initially, the number on the card in cell is .
The shuffler will perform swap operations to permute these cards: each time, they choose two cells (), and then swap the cards in cell and cell . After the swap operations, the permutation of the cards is completed.
Then it is the player's turn. The player also uses swap operations, swapping two cards each time, with the goal of restoring the order of these cards to the initial state.
The time needed to swap the cards in cell and cell is . The player plans to restore the permutation in the shortest total time. Ask: how many possible permutations are there such that the player can complete the restoration with a total time of at most ? Two permutations are different if and only if there exists at least one number card whose cell position differs between the two permutations.
Input Format
Each test point consists of multiple test cases.
The first line contains a positive integer , indicating the number of test cases. It is guaranteed that .
Then follow lines. Each line contains one test case with two positive integers and . It is guaranteed that and .
Output Format
For each test case, output one line with one integer representing the answer.
Since the answer may be very large, output the result modulo .
6
2 1
3 1
5 2
7 5
10 20
15 24
1
2
7
331
1570446
73880648
Hint
In the first test case, the shuffler's operations can only be swapping the cards in cell and cell , so there is only possible permutation, namely the initial state .
In the second test case, there are possible permutations: and . Note that the initial state is not a possible permutation, because after the shuffler performs the first swaps, either all cards are still in the initial state (the first swaps are on the same pair of cards), or none of the cards are in their initial positions (the first swaps are not on the same pair of cards). After the third swap, it is impossible to return to the initial state.
In the third test case, there are possible permutations: , , , , , , .
Source and Acknowledgements
From the THUPC2024 Finals (Tsinghua University Student Programming Contest and Intercollegiate Invitational, 2024). Thanks to THUSAA for providing the problem.
For the testdata, statement, reference solution, editorial, etc., please refer to the THUPC official repository https://thusaac.com/public.
Translated by ChatGPT 5