#P8859. 冒泡排序

    ID: 9808 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>动态规划 DP贪心洛谷原创O2优化组合数学洛谷月赛

冒泡排序

Problem Description

There is a permutation or circular permutation AA whose values and indices are both in 1n1\sim n. Define f(A)f(A) as the minimum number of operations needed to sort AA 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 [3,5,2,1,4][3,5,2,1,4], in one operation you can choose the element 11 and obtain [3,5,1,2,4][3,5,1,2,4], [3,1,5,2,4][3,1,5,2,4], or [1,3,5,2,4][1,3,5,2,4].

For a circular permutation [2,1,4,3][2,1,4,3], after choosing the element 11 you can obtain the circular permutation [1,2,4,3][1,2,4,3], [3,2,4,1][3,2,4,1], or [3,2,1,4][3,2,1,4]. Note that in a circular permutation, the previous element of the 11-st element is the nn-th element.

A permutation or circular permutation is sorted in increasing order if and only if for all 2in2 \leq i \leq n, the previous element of element ii is element i1i-1.

Given n,k,typen,k,type, you need to:

  • When type=1type=1, compute how many permutations AA of length nn satisfy f(A)=kf(A)=k.
  • When type=2type=2, compute how many circular permutations AA of length nn satisfy f(A)=kf(A)=k.

Output the answer modulo 109+710^9+7.

Input Format

Input one line with three positive integers n,k,typen,k,type. Their meanings are described above.

Output Format

Output one line with one integer, the answer modulo 109+710^9+7.

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:

  1. [1,4,2,3][1,4,2,3]
  2. [1,4,3,2][1,4,3,2]
  3. [2,1,4,3][2,1,4,3]
  4. [2,4,1,3][2,4,1,3]
  5. [2,4,3,1][2,4,3,1]
  6. [3,1,2,4][3,1,2,4]
  7. [3,1,4,2][3,1,4,2]
  8. [3,2,1,4][3,2,1,4]
  9. [3,2,4,1][3,2,4,1]
  10. [3,4,1,2][3,4,1,2]
  11. [3,4,2,1][3,4,2,1]

Sample Explanation #2.

The following circular permutations are valid:

  1. [1,2,5,3,4][1,2,5,3,4]
  2. [1,2,5,4,3][1,2,5,4,3]
  3. [1,3,2,5,4][1,3,2,5,4]
  4. [1,3,5,2,4][1,3,5,2,4]
  5. [1,3,5,4,2][1,3,5,4,2]
  6. [1,4,2,3,5][1,4,2,3,5]
  7. [1,4,2,5,3][1,4,2,5,3]
  8. [1,4,3,2,5][1,4,3,2,5]
  9. [1,4,3,5,2][1,4,3,5,2]
  10. [1,4,5,3,2][1,4,5,3,2]
  11. [1,5,2,4,3][1,5,2,4,3]
  12. [1,5,3,2,4][1,5,3,2,4]
  13. [1,5,3,4,2][1,5,3,4,2]
  14. [1,5,4,2,3][1,5,4,2,3]

Note that we consider [1,2,5,3,4][1,2,5,3,4] and [2,5,3,4,1][2,5,3,4,1] to be the same circular permutation.

That is, we consider two circular permutations to be different if and only if there exists some 2in2 \leq i \leq n such that the previous element of element ii is different in the two circular permutations.

Constraints

Test Point ID nn \leq kk \leq type=type=
121 \sim 2 77 11
343 \sim 4 22
565 \sim 6 1515 11
787 \sim 8 22
9129 \sim 12 5050 11
131613 \sim 16 22
1717 500500 1010 11
1818 22
1919 500500 11
2020 22

For all testdata, 1k<n5001 \leq k < n \leq 500 and 1type21 \leq type \leq 2.

Translated by ChatGPT 5