CF 288B Polo the Penguin and Houses(purfer序列公式)

探讨在特定条件下,如何为鹅村的房子进行编号,确保鹅从指定房间出发能按规则到达并返回1号房。解析从k+1到n的自由编号方式及1到k的编号遵循的规律。

Little penguin Polo loves his home village. The village has n houses, indexed by integers from 1 to n. Each house has a plaque containing an integer, the i-th house has a plaque containing integer pi (1 ≤ pi ≤ n).

Little penguin Polo loves walking around this village. The walk looks like that. First he stands by a house number x. Then he goes to the house whose number is written on the plaque of house x (that is, to house px), then he goes to the house whose number is written on the plaque of house px (that is, to house ppx), and so on.

We know that:

  1. When the penguin starts walking from any house indexed from 1 to k, inclusive, he can walk to house number 1.
  2. When the penguin starts walking from any house indexed from k + 1 to n, inclusive, he definitely cannot walk to house number 1.
  3. When the penguin starts walking from house number 1, he can get back to house number 1 after some non-zero number of walks from a house to a house.

You need to find the number of ways you may write the numbers on the houses' plaques so as to fulfill the three above described conditions. Print the remainder after dividing this number by 1000000007 (109 + 7).

Input

The single line contains two space-separated integers n and k (1 ≤ n ≤ 1000, 1 ≤ k ≤ min(8, n)) — the number of the houses and the number k from the statement.

Output

In a single line print a single integer — the answer to the problem modulo 1000000007 (109 + 7).

Examples

input

Copy

5 2

output

Copy

54

input

Copy

7 4

output

Copy

1728 

题意:

给定 n 和k,n 表示有n个房子,然后每个有一个编号,一只鹅要从一个房间中开始走,下一站就是房间的编号,现在要你求出有多少

种方法编号并满足下面的要求:


1.如果从1到k开始走,一定能走到 1。

2.如果从k+1到n 开始走,一定走不到 1.

3.如果从 1 开始走,那么一定能回到1,并且走过房间数不为0.

思路:分类来看,

1、首先是从k+1到n,每个的p(i)只要∈(k+1,n)就行,所以一共有(n-k)^(n-k)种;

2、接下来是从1到k,这里就要用到我们的purfer序列的结论了:n个点的 有标号有根树的计数:n^(n-2)*n = n^(n-1)

https://www.cnblogs.com/dirge/p/5503289.html (原博主)

所以(1,k)有k^(k-1)种,

最后把这两类的结果乘起来就是我们要的答案

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值