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是分离的,前者的方案数就是(n?k)(n?k)

然后对于1,有k种方案,对于2->k, k很小,77,直接搜吧

/*************************************************************************
    > File Name: CF-177-D.cpp
    > Author: ALex
    > Mail: [email protected]
    > 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;
}
时间: 2024-10-19 21:14:21

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

Codeforces Round #177 (Div. 1) C. Polo the Penguin and XOR operation(贪心)

题目地址:http://codeforces.com/problemset/problem/288/C 思路:保证位数尽量大的情况下使得每二进制位均为一的情况下结果最大,从后向前枚举(保证位数尽量大),num表示该数i二进制有几位,x即为使得与i异或后每二进制位均为一的数,v[x]标记是否使用过该数,若未使用则与i搭配. #include<cstdio> #include<cmath> #include<cstring> #include<iostream>

Codeforces Round #177 (Div. 2)---E. Polo the Penguin and XOR operation(贪心)

Little penguin Polo likes permutations. But most of all he likes permutations of integers from 0 to n, inclusive. For permutation p?=?p0,?p1,?-,?pn, Polo has defined its beauty - number . Expression means applying the operation of bitwise excluding "

Codeforces Round #177 (Div. 2)---E. Polo the Penguin and XOR operation

题意:让你构造一个序列,使得序列异或和最大,序列为n 的全排列 ,序列和计算方式为 SUM = a[1] ^ 0 + a[2] ^ 1 + a[3] ^ 2 + .......a[n] ^ n 感想 :之前没做过有关位运算的题,对这一块很陌生,两个数异或以后,如果二进制每一位都为1,那么一定最大,找规律发现当n为偶数时 除开0以外,其他的都是成对出现 当n为奇数时 都是成对出现 例如n=4, 0 1 2 3 4 分别对应0 2 1 4 3 :n=5时 0 1 2 3 4 5 分别对应1 0 5

Codeforces Round #177 (Div. 2) 题解

[前言]咦?现在怎么流行打CF了?于是当一帮大爷在执着的打div 1的时候,我偷偷的在刷div 2.至于怎么决定场次嘛,一般我报一个数字A,随便再拉一个人选一个数字B.然后开始做第A^B场.如果觉得机密性不高,来点取模吧.然后今天做的这场少有的AK了.(其实模拟赛只做完了4题,最后1题来不及打了) 等等,话说前面几题不用写题解了?算了,让我难得风光一下啦. [A] A. Polo the Penguin and Segments time limit per test 2 seconds mem

Codeforces Round #286 (Div. 2)B. Mr. Kitayuta&#39;s Colorful Graph(dfs,暴力)

数据规模小,所以就暴力枚举每一种颜色的边就行了. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<string> #include<cmath> #include<map> #include<set> #include<vector> #include<algorithm>

CodeCraft-19 and Codeforces Round #537 (Div. 2) - D. Destroy the Colony(动态规划+组合数学)

Problem  CodeCraft-19 and Codeforces Round #537 (Div. 2) - D. Destroy the Colony Time Limit: 2000 mSec Problem Description Input Output For each question output the number of arrangements possible modulo 10^9+7. Sample Input abba21 41 2 Sample Output

Codeforces Round #324 (Div. 2) D. Dima and Lisa (哥德巴赫猜想 + 暴力)

D. Dima and Lisa Dima loves representing an odd number as the sum of multiple primes, and Lisa loves it when there are at most three primes. Help them to represent the given number as the sum of at most than three primes. More formally, you are given

Codeforces Round #354 (Div. 2) ABCD

Codeforces Round #354 (Div. 2) Problems # Name     A Nicholas and Permutation standard input/output 1 s, 256 MB    x3384 B Pyramid of Glasses standard input/output 1 s, 256 MB    x1462 C Vasya and String standard input/output 1 s, 256 MB    x1393 D T

Codeforces Round #279 (Div. 2) ABCD

Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name     A Team Olympiad standard input/output 1 s, 256 MB  x2377 B Queue standard input/output 2 s, 256 MB  x1250 C Hacking Cypher standard input/output 1 s, 256 MB  x740 D Chocolate standard input/