#P8859. 冒泡排序
冒泡排序
Problem Description
There is a permutation or circular permutation whose values and indices are both in . Define as the minimum number of operations needed to sort in increasing order.
In each operation, you may choose one element and bubble it forward (to the left) several times. One bubble is defined as follows: if this element is smaller than the previous element, you may swap it with the previous element. When it cannot bubble at some point, this operation stops immediately. Otherwise, it may bubble any number of times continuously.
For example, given the permutation , in one operation you can choose the element and obtain , , or .
For a circular permutation , after choosing the element you can obtain the circular permutation , , or . Note that in a circular permutation, the previous element of the -st element is the -th element.
A permutation or circular permutation is sorted in increasing order if and only if for all , the previous element of element is element .
Given , you need to:
- When , compute how many permutations of length satisfy .
- When , compute how many circular permutations of length satisfy .
Output the answer modulo .
Input Format
Input one line with three positive integers . Their meanings are described above.
Output Format
Output one line with one integer, the answer modulo .
4 2 1
11
5 2 2
14
50 10 1
808620624
50 10 2
578144115
Hint
Sample Explanation #1.
The following permutations are valid:
Sample Explanation #2.
The following circular permutations are valid:
Note that we consider and to be the same circular permutation.
That is, we consider two circular permutations to be different if and only if there exists some such that the previous element of element is different in the two circular permutations.
Constraints
| Test Point ID | |||
|---|---|---|---|
For all testdata, and .
Translated by ChatGPT 5