CodeForces 177(div1)B

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:

  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個城市,每個城市上有一個值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);
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章