#P10254. Sum of Weighted Permutations with Exact Inversions
Sum of Weighted Permutations with Exact Inversions
Sum of Weighted Permutations with Exact Inversions
ZHY has a peculiar stuttering style: when reading a sequence of n characters, he reads the first character once, the second twice, the third three times, and so on; that is, the i-th character is read i times. For example, if the word is “Genshin”, with characters [原, 神, 启, 动] then ZHY
will read it as “原神神启启启动动动动”.
YHZ has n characters. Each character has an appealing value, and these values form a permutation of the numbers from 1 to n (each value appears exactly once). YHZ wants to rearrange these characters into a sequence such that the number of inversions exactly equals k. (Recall: the number of inversions of a permutation a1, a2, ..., an is defined as \[ \text{Inv}(a)=\sum_{1\le ia_j], \] where [P] is 1 if proposition P is true and 0 otherwise.)
After YHZ arranges the characters, ZHY will read the entire sequence in order. Note that if a character appears multiple times in ZHY’s reading (due to his stutter), its appealing value is counted each time it is read. Formally, for a permutation a1,a2,…,an, define its weight as \[ W(a)=\sum_{i=1}^n i\cdot a_i. \] Your task is: given n and k, consider all permutations of 1 ~ n that have exactly k inversions, and compute the sum of their weights. Since the answer can be huge, output it modulo 998244353.
Input Format: Two integers n and k.
Output Format: A single integer representing the sum of the weights of all valid permutations modulo 998244353.
Note: The input guarantees that at least one valid permutation exists.
inputFormat
The first line contains two integers n and k separated by a space.
Constraints: For this problem, you may assume n is small (e.g. n ≤ 10).
outputFormat
Output a single integer: the sum of the weights of all permutations of 1 ~ n with exactly k inversions, modulo 998244353.
sample
3 0
14