Codeforces 177 D

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).

Sample test(s)
input
5 2
output
54
input
7 4
output
1728
#include <iostream>

using namespace std;

const long long MOD=1e9+7;

long long pow_mod(int n,int k)
{
    long  long ret=1,p=n;
    while(k)
    {
        if(k&1)
        {
            ret*=p;
            ret%=MOD;
        }
        p*=p;
        p%=MOD;
        k>>=1;
    }
    return ret;
}

int main()
{
    int N,K;
    cin>>N>>K;
    cout<<((pow_mod(K,K-1)*pow_mod(N-K,N-K))%MOD)<<endl;
    return 0;
}

本文链接:Codeforces 177 D

转载声明:本站文章若无特别说明,皆为原创,转载请注明来源:Rexdf,谢谢!^^


カテゴリー: ACM タグ: パーマリンク

コメントを残す

メールアドレスが公開されることはありません。

*

:zsmilebig: :zsadbig: :zwiredbig: :zgreenhappy: more »

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください