DP for counting permutations

Revision en1, by SinsAries, 2025-10-29 18:47:40

Counting the number of permutations b of an array a such that: $$$S = \sum_{j=2}^{n} (\max(a[b_j], a[b_{j-1}]) - 1)$$$ equals a given value X.

Know :

  • $$$n \le 50$$$

  • $$$a_i \le n$$$

Example: Computing S for a given permutation

Let

$$$ a = [3, 5, 6, 2, 10] $$$

and define

$$$ S = \sum_{j=2}^{n} (\max(a[b_j], a[b_{j-1}]) - 1) $$$

Example 1

Take the permutation
[ b = [2, 4, 1, 3, 5] ]

Then

$$$ a[b] = [a_2, a_4, a_1, a_3, a_5] = [5, 2, 3, 6, 10] $$$
j $$$Pair(a[b_{j-1}], a[b_j])$$$ max max−1 Cumulative (S)
2 (5, 2) 5 4 4
3 (2, 3) 3 2 6
4 (3, 6) 6 5 11
5 (6, 10) 10 9 20

Result:

$$$ S = 4 + 2 + 5 + 9 = 20 $$$

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English SinsAries 2025-10-29 18:47:40 847 Initial revision (published)