【CodeForces - 288B】Polo the Penguin and Houses (组合数学+Purfer序列)

11 篇文章 0 订阅
2 篇文章 0 订阅

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

题意:

有n个房间,每个房间都会告诉你下一个去的房间是哪一个,即到第i个房间后,下一去得房间是p_i

并且要遵循一下的条件,问p_1 p_2 ...p_n序列有多少种。

1、从1到k的任意一点出发可以走到1点

2、从k+1到n的任意一点出发走不到1点

首先从k这个点分开,后面的一部分只要求走不到1就可以,因此只要不与前k个点有联系就可以,因此每个点都有(n-k)种选择。因此后面的选择种类是(n-k)^{(n-k)}

前面k个把1单独拿出来看,1可以指向的点有k种。然后,这k个点之间相互连线最终会形成一棵树(与1点的连线,是到1号点,从一号点出来能到达的前面考虑过了),因此我们只要看看这颗树有多少种就可以了。

但是,在做题时,只想到这里,接下来就不会了,之后听zxf大佬讲了,这里是与有标号的无根树,与purfer编码有关。然后在查了题解后,知道这里应该是有个公式的。

前k个节点的p_x值唯一对应一个以1为根的带标号树,由带标号树与其Purfer序列的一一对应得知k个点的带标号树有个k^{(k-2)},故答案为k^{k-1}*(n-k)^{n-k}。(这段话来自这个博客

一道看起来普通的思维题,里面竟然有一个新的知识点。不能小看每一道题目啊,感觉对于一道题目A掉好想已经不是最重要的了,能从里面发现新的知识点、思维点、技巧,看透出题人想要考什么,这或许才是最重要的吧。

说真的,这道题的题干有点怪怪的,总感觉像是说从1不能直接到自己,可是看别人的题解感觉是能到自己,但其实不重要了,只要从这里面学到知识点,有进步,这就够了。

#include<stdio.h>
#include<string.h>
#include<queue>
#include<set>
#include<iostream>
#include<map>
#include<stack>
#include<cmath>
#include<algorithm>
#define ll long long
#define mod 1000000007
#define eps 1e-8
using namespace std;    
ll qsm(ll a,ll b)
{
	ll t=1;
	while(b)
	{
		if(b&1)
		{
			t=(t*a)%mod;
		}
		a=(a*a)%mod;
		b>>=1;
	}
	return t%mod;
}
int main()
{
	ll n,k;
	scanf("%lld%lld",&n,&k);
	ll p=n-k;
	ll ans=qsm(p,p);
	ans=(ans*qsm(k,k-1))%mod;
	printf("%lld\n",ans%mod);
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值