Codeforces Round #177 (Div. 2)---D. Polo the Penguin and Houses (组合数学+暴力)

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).
Sample test(s)
Input

5 2

Output

54

Input

7 4

Output

1728

一开始以为标签上的数字都不相同
后来才读懂题意
k+1->n 和 1 -> n是分离的,前者的方案数就是 (nk)(nk)
然后对于1,有k种方案,对于2->k, k很小, 77 ,直接搜吧

/*************************************************************************
    > File Name: CF-177-D.cpp
    > Author: ALex
    > Mail: zchao1995@gmail.com 
    > Created Time: 2015年04月08日 星期三 19时09分18秒
 ************************************************************************/

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

static const int mod = 1000000007;

LL res;
int n, k;
int arr[10];
bool vis[10];

void dfs(int cur)
{
    if (cur > k)
    {
        bool flag = 1;
        for (int i = 2; i <= k; ++i)
        {
            memset(vis, 0, sizeof(vis));
            vis[i] = 1;
            int u = arr[i];
            while (u != 1)
            {
                if (vis[u])
                {
                    break;
                }
                vis[u] = 1;
                u = arr[u];
            }
            if (u != 1)
            {
                flag = 0;
                break;
            }
        }
        if (flag)
        {
            ++res;
            res %= mod;
        }
        return;
    }
    for (int i = 1; i <= k; ++i)
    {
        arr[cur] = i;
        dfs(cur + 1);
    }
}

int main()
{
    while (~scanf("%d%d", &n, &k))
    {
        LL ans = 1;
        for (int i = 1; i <= n - k; ++i)
        {
            ans *= (n - k);
            ans %= mod;
        }
        ans *= k;
        ans %= mod;
        res = 0;
        dfs(2);
        ans *= res;
        ans %= mod;
        printf("%lld\n", ans);
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值