Learning Note on Luogu P10254 (Well-known in China)

Правка en23, от QWQ.Maple, 2024-03-18 17:33:59

Problem Link: https://www.luogu.com.cn/problem/P10254

1. Problem Statement

Given a permutation $$$p$$$ of length $$$n$$$, define $$$Inv(p)$$$ to be the inversion of $$$p$$$ and $$$W(p) := \sum\limits_{i=1}^n ip_i$$$. For example, if $$$p = [1, 2, 3]$$$, the $$$W(p) = 1 \times 1 + 2 \times 2 + 3 \times 3 = 14$$$. Given integers $$$n$$$ and $$$k$$$, compute $$$\sum\limits_{p \in S_n,\,Inv(p) = k} W(p)$$$ modulo $$$998244353$$$. For example, if $$$n = 3, k = 2$$$, then there are two permutations whose inversion is $$$2$$$: $$$[2, 3, 1]$$$ ($$$W = 1 \times 2 + 2 \times 3 + 3 \times 1 = 11$$$) and $$$[3, 1, 2]$$$ ($$$W = 3 \times 1 + 1 \times 2 + 2 \times 3 = 11$$$). Therefore, the answer is the sum of $$$W$$$ of these two permutations, i.e., $$$11 + 11 = 22$$$. Also, the answer for $$$n=7,\,k=5$$$ is $$$22066$$$.

2. Some trials

First, I considered fix $$$p_i = j$$$, and try to figure out:

Q1: How many permutations are there such that $$$p_i = j$$$ and the inversion is $$$k$$$?

If Q1 were solved efficiently, the original problem would be easy. The answer would be $$$\sum\limits_{i, j}ij\text{Answer_to_Q1}(i, j)$$$. If we remove the clause "$$$p_i = j$$$" from $$$Q_1$$$, $$$Q_1$$$ becomes a common Leetcode-hard problem (Leetcode 629). This observation gives me false hope, but I do not know how to push it forward (maybe this approach will work and we can make it as a CF problem!).

After struggling for several hours, I discussed with Tobo. He offered some brilliant ideas. First, we can insert from $$$1$$$ to $$$n$$$. For each integer $$$i$$$ contributing $$$p$$$ inversion, it is located at the $$$(i - p)$$$ — th place. For example, we first insert $$$1$$$. Then we insert $$$i = 2$$$ to the permutation $$$[1]$$$. If the number $$$2$$$ contributes one inversion, the permutation is $$$[2, 1]$$$, and $$$2$$$ is located at the $$$2-1 = 1$$$-th place. If the number $$$2$$$ contributes no inversion, the permutation is $$$[1, 2]$$$, and $$$2$$$ is located at the $$$2-0 = 2$$$-th place. No other possibilities.

Q2: For elements after $$$i$$$, their contributions might change after $$$i$$$ is inserted into the permutation. How to deal with the change?

For example, if we insert $$$2$$$ after $$$1$$$, the contribution of $$$1$$$ is not changed. However, if we insert $$$2$$$ before $$$1$$$, the contribution of $$$1$$$ changes from $$$1$$$ to $$$2$$$. Before the insertion, $$$1$$$ sits on the first place, contributing $$$1$$$. However, after $$$2$$$ is inserted in front of $$$1$$$, $$$1$$$ sites on the second place and its contribution becomes $$$2$$$! I considered the suffix sum of the elements after $$$i$$$, because when $$$i$$$ is inserted, the contribution of elements after $$$i$$$ actually increases the same as their sum ("suffix sum"), but I find the "suffix sum" difficult to deal with. I failed to get an idea until I read zzfantani's blog (in Chinese).

3. zzfantani's contribution splitting idea

What if we ignore the elements located after $$$i$$$? In fact, we are calculating

$$$\sum\limits_{i=1}^n i \sum\limits_{j=1}^{p^{-1}(i)} [p_j \leq p_i]$$$ (1),

i.e., given an element $$$i$$$, for elements sits before $$$i$$$ (including $$$i$$$ itself), only the elements $$$\leq i$$$ are taken into account, the elements $$$>i$$$ are not taken into account. In the above example, if we insert $$$[1]$$$, the contribution of $$$1$$$ is $$$1$$$, if we insert $$$2$$$ before $$$1$$$, the contribution of $$$2$$$ is $$$2$$$, and the contribution of $$$1$$$ is still $$$1$$$. The contribution that $$$2$$$ makes to $$$1$$$, which is $$$1$$$, is omitted from the above calculation. Let's verify Eq. (1): For $$$i = 1$$$, $$$p^{-1}(1) = 2$$$ (because $$$1$$$ sits at the second place in $$$[1, 2]$$$), $$$\sum\limits_{j=1}^{2} [p_j \leq 1] = [p_1 \leq 1] + [p_2 \leq 1] = 0 + 1 = 1$$$, so the contribution of $$$1$$$ is counted as $$$1$$$. Similarly, the contribution of $$$2$$$ is counted as $$$2$$$.

So, instead of consider the annoying "suffix sum" in chapter $$$3$$$, zzfantani considers the dual of (1):

$$$\sum\limits_{i=1}^n i \sum\limits_{j=1}^{p^{-1}(i)} [p_j > p_i]$$$ (2),

which could be calculated via inserting reversely, i.e., from $$$n$$$ to $$$1$$$.

4. Implementation

Let $$$f_{i, j}$$$ be the number of size-$$$i$$$ permutation with inversion $$$j$$$. Let $$$mn := max(0, j - i + 1)$$$, Then, similar to Leetcode 629:

$$$f_{i, j} = \sum\limits_{p = mn} ^ j f_{i - 1, p}$$$.

Code

5. Please comment on the estimated difficulty of this problem

I think it is a $$$2700$$$ level problem. What is your opinion? StarSilk marked it as a $$$2600$$$-level problem.

Теги combinatorics, dp

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en53 Английский QWQ.Maple 2024-04-02 06:44:13 4 Tiny change: ', 2, 3]$, the $W(p) = 1' -> ', 2, 3]$, $W(p) = 1'
en52 Английский QWQ.Maple 2024-04-02 06:43:34 129
en51 Английский QWQ.Maple 2024-03-21 06:19:46 2 Tiny change: 'note $g_{i,j}$ as the' -> 'note $g_{i,j}$ as the'
en50 Английский QWQ.Maple 2024-03-21 06:18:49 14
en49 Английский QWQ.Maple 2024-03-19 17:55:58 9
en48 Английский QWQ.Maple 2024-03-19 08:42:17 27 Tiny change: ') instead.\n\n<spoi' -> ') instead. The complexity is $O(nk)$.\n\n<spoi'
en47 Английский QWQ.Maple 2024-03-18 21:28:24 3 Tiny change: ' make it as a CF prob' -> ' make it a CF prob'
en46 Английский QWQ.Maple 2024-03-18 20:11:38 8 Tiny change: 'mn} ^ {j}pf_{i-1, p}' -> 'mn} ^ {j}p \times f_{i-1, p}'
en45 Английский QWQ.Maple 2024-03-18 20:10:32 1 Tiny change: 'rs-array/)\n\n$f_{i,' -> 'rs-array/),\n\n$f_{i,'
en44 Английский QWQ.Maple 2024-03-18 20:08:37 4 Tiny change: 'j$, and try to figure' -> 'j$, and tried to figure'
en43 Английский QWQ.Maple 2024-03-18 20:08:11 3 Tiny change: 'idered fix $p_i = j$' -> 'idered fixing $p_i = j$'
en42 Английский QWQ.Maple 2024-03-18 19:28:43 8 Tiny change: 'mn} ^ {j}pf_{i-1, p}' -> 'mn} ^ {j}p \times f_{i-1, p}'
en41 Английский QWQ.Maple 2024-03-18 18:15:50 1 Tiny change: '$, $1$ sites on the s' -> '$, $1$ sits on the s'
en40 Английский QWQ.Maple 2024-03-18 18:12:08 12
en39 Английский QWQ.Maple 2024-03-18 18:11:13 12
en38 Английский QWQ.Maple 2024-03-18 18:10:15 1
en37 Английский QWQ.Maple 2024-03-18 18:08:36 0 (published)
en36 Английский QWQ.Maple 2024-03-18 18:08:26 2 (saved to drafts)
en35 Английский QWQ.Maple 2024-03-18 18:06:23 103 (published)
en34 Английский QWQ.Maple 2024-03-18 18:04:47 312
en33 Английский QWQ.Maple 2024-03-18 18:01:38 385
en32 Английский QWQ.Maple 2024-03-18 17:57:18 202
en31 Английский QWQ.Maple 2024-03-18 17:54:09 269
en30 Английский QWQ.Maple 2024-03-18 17:51:35 329
en29 Английский QWQ.Maple 2024-03-18 17:47:05 14
en28 Английский QWQ.Maple 2024-03-18 17:45:41 42
en27 Английский QWQ.Maple 2024-03-18 17:44:28 184
en26 Английский QWQ.Maple 2024-03-18 17:42:21 128
en25 Английский QWQ.Maple 2024-03-18 17:40:35 181
en24 Английский QWQ.Maple 2024-03-18 17:38:12 352
en23 Английский QWQ.Maple 2024-03-18 17:33:59 109
en22 Английский QWQ.Maple 2024-03-18 17:30:34 64
en21 Английский QWQ.Maple 2024-03-18 17:29:17 1883
en20 Английский QWQ.Maple 2024-03-18 17:26:45 22
en19 Английский QWQ.Maple 2024-03-18 17:26:32 1 Tiny change: '"$p_i = j$ from $Q_1' -> '"$p_i = j$" from $Q_1'
en18 Английский QWQ.Maple 2024-03-18 17:26:01 257
en17 Английский QWQ.Maple 2024-03-18 17:23:49 38 Tiny change: '\leq 1] = 1$, so ' -> '\leq 1] = [p_1 \leq 1] + [p_2 \leq 1] = 0 + 1 = 1$, so '
en16 Английский QWQ.Maple 2024-03-18 17:23:06 67
en15 Английский QWQ.Maple 2024-03-18 17:21:29 656
en14 Английский QWQ.Maple 2024-03-18 17:15:00 112
en13 Английский QWQ.Maple 2024-03-18 17:08:22 161
en12 Английский QWQ.Maple 2024-03-18 17:05:41 221
en11 Английский QWQ.Maple 2024-03-18 17:01:18 574
en10 Английский QWQ.Maple 2024-03-18 16:56:06 1106
en9 Английский QWQ.Maple 2024-03-18 16:43:26 7 Tiny change: 's_{i, j}ijAnswer_to_Q1(i, j)$ se' -> 's_{i, j}ij\text{Answer_to_Q1}(i, j)$ se'
en8 Английский QWQ.Maple 2024-03-18 16:43:06 339
en7 Английский QWQ.Maple 2024-03-18 16:35:37 320
en6 Английский QWQ.Maple 2024-03-18 16:33:33 94
en5 Английский QWQ.Maple 2024-03-18 16:32:33 4
en4 Английский QWQ.Maple 2024-03-18 16:32:20 2
en3 Английский QWQ.Maple 2024-03-18 16:32:11 125
en2 Английский QWQ.Maple 2024-03-18 16:29:29 193
en1 Английский QWQ.Maple 2024-03-18 16:24:56 98 Initial revision (saved to drafts)