CodeForces - 288B Polo the Penguin and Houses

在一座由n个房子组成的村庄中,每个房子有一块含有整数的牌匾,代表从该房子可到达的房子编号。题目要求找出所有可能的牌匾数值配置方案,使得从1到k的房子出发能回到1号房子,而从k+1到n的房子出发则无法到达1号房子。解答使用了深度优先搜索和快速幂算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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个点,给每个点x定义一个px值,表示从x点会到px点,现在有两个要求:

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

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

问满足条件的p1,p2,...,pn个数

题解:分析到 k+1到n 门牌上肯定是 k+1到n的  前k个 就相当于是:1 是根节点 其余点不断往下放 dfs搜一遍即可

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int N=1e6+10;
typedef long long ll;
const int mod=1e9+7;
ll n,k;
ll C[10][10];
ll ksm(ll a,ll b)
{
    ll ans=1;
    while(b)
    {
        if(b&1) ans=(ans*a)%mod;
        b>>=1;
        a=a*a%mod;
    }
    return ans;
}
ll dfs(ll z,ll up)
{
    if(z<=0) return 1;
    ll ans=0;
    for(int i=1;i<=z;i++)
        ans=(ans+(C[z][i]*ksm(up,i)%mod)*dfs(z-i,i)%mod)%mod;
    return ans;
}
int main()
{
    C[0][0]=C[1][0]=C[1][1]=1;
    for(int i=2;i<=8;i++)
    {
        C[i][0]=1;
        for(int j=1;j<=8;j++)
            C[i][j]=(C[i-1][j]+C[i-1][j-1])%mod;
    }
    while(~scanf("%lld%lld",&n,&k))
    {
        ll m=k-1;
        ll ans=k,cnt=0;
        cnt=dfs(k-1,1)%mod;
        ans=(ans*cnt)%mod;
        for(int i=1;i<=n-k;i++)
            ans=(ans*(n-k))%mod;
        cout<<ans<<endl;
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值