luogu#P4769. [NOI2018] 冒泡排序
[NOI2018] 冒泡排序
Background
Please note that the testdata includes cases with .
Problem Description
Recently, Xiao S has become very interested in bubble sort. To simplify the problem, Xiao S only studies bubble sorting permutations of to .
Below is the algorithm description of bubble sort.
Input: a permutation p[1...n] of length n
Output: the sorted result of p.
for i = 1 to n do
for j = 1 to n - 1 do
if(p[j] > p[j + 1])
swap the values of p[j] and p[j + 1]
The number of swaps in bubble sort is defined as the number of times the swap operation is executed. It can be proven that a lower bound of the swap count is , where is the number at position in the permutation . If you are interested in the proof, you can read the hint.
Xiao S starts focusing on permutations of length that satisfy swap count (later, for convenience, we call all such permutations “good” permutations). He further wonders: are there many such permutations? Are they dense?
For a given permutation of length , Xiao S wants to compute the number of “good” permutations that are strictly greater than in lexicographical order. However, he cannot do it, so he asks you for help. Since the answer may be very large, you only need to output the result modulo .
Input Format
The first line contains a positive integer , indicating the number of test cases.
For each test case, the first line contains a positive integer , with .
The next line contains positive integers, corresponding to in the statement, and it is guaranteed that the input is a permutation of to .
Output Format
Output lines, each containing one integer.
For each test case, output one integer: the number of “good” permutations that are strictly greater than in lexicographical order, modulo .
1
3
1 3 2
3
1
4
1 4 2 3
9
Hint
More Samples
Please download more samples from the attachment.
Sample 3
See inverse3.in and inverse3.ans in the attachment.
Explanation for Sample 1
Among the permutations that are lexicographically greater than , all of them are “good” except , so the answer is .
Constraints
Below is a description of the input size for each test point.
For all testdata, it holds that (samples may not satisfy this).
Let be the maximum among the test cases, and let be the sum of over all test cases.
::cute-table{tuack}
| Test Point | Special Properties | ||
|---|---|---|---|
| None | |||
| ^ | |||
| ^ | None | ||
| ^ | |||
| ^ | None | ||
| ^ | |||
| ^ | None | ||
| ^ | |||
Hint
Below is the proof that a lower bound of the swap count is .
Sorting is essentially moving numbers, so the number of swaps in sorting can be described by the total distance that numbers move. For position , suppose the number at this position in the initial permutation is . Then we need to move this number to position , and the moving distance is . Therefore, the total moving distance is . Each step of bubble sort swaps two adjacent numbers, and each swap can reduce the total moving distance by at most . Hence, is a lower bound of the swap count in bubble sort.
Not all permutations reach this lower bound. For example, when , consider the permutation . The number of swaps after bubble sorting this permutation is , but is only .
Translated by ChatGPT 5