您的位置:首页 > 编程语言

CodeForces 177(div1)B

2016-07-25 20:32 393 查看
B. Polo the Penguin and Houses

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

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:

When the penguin starts walking from any house indexed from 1 to k, inclusive, he can walk to house number 1.

When the penguin starts walking from any house indexed from k + 1 to n,
inclusive, he definitely cannot walk to house number 1.

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个城市,每个城市上有一个值p,到城市i上,然后就要走到城市pi上,并且重复这个过程,求满足(1,从城市1-k出发,总能回到1,;2,从城市k+1到n出发,绝对回不了n;3,从城市1出发后,经过任意步总能回到1)条件的pi的设置方法数。

思路:从题意可以知道,第二条件只需要后面的n-k个数字和前面的k个数字没关系就行了,也就是n-k的n-k次方。再就是前面的k个数,根据它的要求暴力一下就行了,最大是8

,把8个数都算出来,后来算出来其实就是k的k-1次方,所以直接一个矩阵快速幂就够了#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll mod = 1000000007;
ll quickmod(ll a,ll b)
{
ll ans = 1;
while(b)
{
if(b&1)
{
ans = (ans*a)%mod;
}
a = (a*a)%mod;
b >>= 1;
}
return ans%mod;
}
int main()
{
int n,k;
scanf("%d%d",&n,&k);
ll ans = quickmod(k,k-1);
ll sum = quickmod(n-k,n-k);
ans = (ll(ans*sum))%mod;
printf("%I64d\n",ans);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  CodeForces 代码