编程介的小学生 2017-10-04 02:07 采纳率: 20.5%
浏览 887
已采纳

PERMS

Description

Count the number of permutations that have a specific number of inversions.
Given a permutation a1, a2, a3,..., an of the n integers 1, 2, 3, ..., n, an inversion is a pair (ai, aj) where i < j and ai > aj. The number of inversions in a permutation gives an indication on how "unsorted" a permutation is. If we wish to analyze the average running time of a sorting algorithm, it is often useful to know how many permutations of n objects will have a certain number of inversions.

In this problem you are asked to compute the number of permutations of n values that have exactly k inversions.

For example, if n = 3, there are 6 permutations with the indicated inversions as follows:
123 0 inversions

132 1 inversion (3 > 2)

213 1 inversion (2 > 1)

231 2 inversions (2 > 1, 3 > 1)

312 2 inversions (3 > 1, 3 > 2)

321 3 inversions (3 > 2, 3 > 1, 2 > 1)

Therefore, for the permutations of 3 things
1 of them has 0 inversions
2 of them have 1 inversion
2 of them have 2 inversions
1 of them has 3 inversions
0 of them have 4 inversions
0 of them have 5 inversions
etc.

Input

The input consists one or more problems. The input for each problem is specified on a single line, giving the integer n (1 <= n <= 18) and a non-negative integer k (0 <= k <= 200). The end of input is specified by a line with n = k = 0.
Output

For each problem, output the number of permutations of {1, ..., n}with exactly k inversions.
Sample Input

3 0
3 1
3 2
3 3
4 2
4 10
13 23
18 80
0 0
Sample Output

1
2
2
1
5
0
46936280
184348859235088

  • 写回答

1条回答 默认 最新

  • threenewbee 2017-10-23 10:37
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装