1. 程式人生 > >【Polo the Penguin and Houses】【CodeForces - 288B】(purfer序列+思維)

【Polo the Penguin and Houses】【CodeForces - 288B】(purfer序列+思維)

題目:

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

5 2

Output

54

Input

7 4

Output

1728

解題報告:當時做的時候就是自己入坑,拼命趙數學規律,排列組合,最後找出來一個規律:k^(k-1)*(n-k)^(n-k)。但是當時沒有找到一個合適的解釋對於k^(k-1)。最後結束之後大佬告訴我是purfer序列,我也沒有細研究,以後待補。

ac程式碼:

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
#define mod 1000000007
int main()
{
	int n,k;
	scanf("%d%d",&n,&k);
	ll res=1;
	for(int i=0;i<k-1;i++)
	{
		res=res*k%mod;
	}
	for(int i=1;i<=n-k;i++)
	{
		res=res*(n-k)%mod;
	}
	printf("%lld\n",res);
	return 0;
}