Codeforces Round #177 (Div. 2)---D. Polo the Penguin and houses (combinatorial Math + violence)

Source: Internet
Author: User

Little Penguin Polo loves his home village. The village has n houses, indexed by integers from 1 to n. Each house had a plaque containing an integer and the I-th house had 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 was written on the plaque of House X (that's, to house px) and then he goes to the Hou Se whose number is written on the plaque of House px (that's, to house ppx), and so on.

We know that:

When the penguin starts walking is 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 numb ER 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 your may write the numbers on the houses ' plaques so as to fulfill the three above desc ribed conditions. Print the remainder after dividing this number by 1000000007 (109 + 7).
Input

The single line contains the 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

At first I thought the numbers on the labels were different.
Then I read test instructions.
K+1->n and 1, n are separated, the former scheme number is (n−k) (n−k) (n-k) ^ {(n-k)}
Then for 1, there are k schemes, for 2->k, K is very small, 77 7^{7}, direct Search bar

/************************************************************************* > File name:cf-177-d.cpp > Autho R:alex > Mail:zchao1995@gmail.com > Created time:2015 April 08 Wednesday 19:09 18 seconds **************************** /#include <functional> #include <algorithm> #include < iostream> #include <fstream> #include <cstring> #include <cstdio> #include <cmath> #include & lt;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 &LT;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; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.