#P4561. [JXOI2018] 排序问题
[JXOI2018] 排序问题
Description
Kujou Karen has recently been studying the properties of various sorting algorithms, and she discovered a very interesting sorting method: Gobo sort!
The algorithm of Gobo sort is roughly as follows:
- Suppose we want to sort a sequence of size .
- Randomly and uniformly generate a permutation of size .
- Construct a sequence of size such that , check whether is sorted. If is already sorted, end the algorithm and return ; otherwise, go back to step 2.
Obviously, the expected time complexity of this algorithm is . However, to her surprise, by using the magic of quantum mechanics, in a quantum system this algorithm can be optimized to linear time.
Kujou Karen studied this sorting algorithm further and found that if a sequence satisfies certain properties, Gobo sort will quickly compute the correct result. To quantify this speed, she defines the number of rounds of Gobo sort as the number of times step 2 is executed.
Then she came up with the following problem:
Now there is a sequence of length . Kujou Karen will append elements to the end of this sequence, each element being a positive integer in . She wants the expected number of rounds for Gobo sort on the new sequence of length to be as large as possible. She wants to obtain this maximum expected number of rounds.
Kujou Karen is very smart and quickly computed the answer. She wants to check it with you. Since this expected number is too large, she only asks you to output the result modulo .
Input Format
The first line contains an integer , the number of test cases.
The next lines describe the test cases.
Each test case consists of two lines. The first line contains four positive integers , representing the length of the sequence, the number of added numbers, and the range of the added numbers. The second line contains positive integers; the -th is .
Output Format
Output integers, the answers.
2
3 3 1 2
1 3 4
3 3 5 7
1 3 4
180
720
Hint
Sample Explanation
For the first test case, we can append to the end of the sequence, making it 1 3 4 1 2 2. Then the success probability in one round is , so the expected number of rounds is .
For the second test case, we can append to the end of the sequence, making it 1 3 4 5 6 7. Then the success probability in one round is , so the expected number of rounds is .
Constraints
- For 30% of the testdata, , .
- For 50% of the testdata, , .
- For 60% of the testdata, .
- For 70% of the testdata, .
- For 90% of the testdata, .
- For 100% of the testdata, , , , , , .
Translated by ChatGPT 5
京公网安备 11011102002149号