【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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮你解决这个问题。首先,我们需要定义一个Penguin类,该类需要实现Serializable接口,以便进行序列化和反序列化。以下是一个简单的Penguin类的示例代码: ```java import java.io.Serializable; public class Penguin implements Serializable { private String name; private int age; private String gender; public Penguin(String name, int age, String gender) { this.name = name; this.age = age; this.gender = gender; } public String getName() { return this.name; } public int getAge() { return this.age; } public String getGender() { return this.gender; } public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } public void setGender(String gender) { this.gender = gender; } public String toString() { return "Penguin [name=" + name + ", age=" + age + ", gender=" + gender + "]"; } } ``` 现在我们可以创建一个Penguin对象,将其序列化为二进制文件,并使用反序列化将其读取回来并输出到控制台。以下是示例代码: ```java import java.io.*; public class Main { public static void main(String[] args) { // 创建一个Penguin对象 Penguin penguin = new Penguin("Penny", 2, "Female"); try { // 将Penguin对象序列化为二进制文件 ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("D:/penguin.bin")); out.writeObject(penguin); out.close(); // 从二进制文件中读取Penguin对象 ObjectInputStream in = new ObjectInputStream(new FileInputStream("D:/penguin.bin")); Penguin newPenguin = (Penguin) in.readObject(); in.close(); // 输出读取的Penguin对象 System.out.println(newPenguin); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } } ``` 当你运行该程序时,它将创建一个名为“penguin.bin”的二进制文件,并将Penguin对象序列化到该文件中。然后,它将从该文件中读取Penguin对象,将其输出到控制台上。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值