#P4778. Counting swaps
Counting swaps
Description
You are given a permutation of the numbers through . In each step you can choose two numbers and swap with .
Let be the minimum number of such swaps needed to sort the given permutation. Compute the number of different sequences of exactly swaps that sort the given permutation. Since this number may be large, compute it modulo .
Input Format
The first line of the input file contains an integer t specifying the number of test cases. Each test case is preceded by a blank line.
Each test case consists of two lines. The first line contains the integer n. The second line contains the sequence : a permutation of .
In the easy subproblem C1, .
In the hard subproblem C2, .
Output Format
For each test case, output a single line with a single integer: , where x is the number of ways to sort the given sequence using as few swaps as possible.
3
3
2 3 1
4
2 1 4 3
2
1 2
3
2
1
Hint
In the first test case, we can sort the permutation in two swaps. We can make the first swap arbitrarily; for each of them, there's exactly one optimal second swap. For example, one of the three shortest solutions is “swap with and then swap with ”.
In the second test case, the optimal solution involves swapping with and swapping with . We can do these two swaps in either order.
The third sequence is already sorted. The optimal number of swaps is , and thus the only optimal solution is an empty sequence of swaps.
题目来源:IPSC2016
京公网安备 11011102002149号