Codeforces Gym 101933 K King's Colors(二项式反演)

K. King's Colors

time limit per test

1.0 s

memory limit per test

256 MB

input

standard input

output

standard output

Far, far away, there is the mystical Kingdom of Trees (more formally, "Royal Commonwealth of Connected Undirected Simple Acyclic Graphs"). The King there is very sad because his kingdom is not accepted as a sovereign state in the United Nations. In order to become a member, he needs to make a flag the UN can put on their website.

The flag will of course consist of the King's favorite tree, which contains nn nodes. The King would be happy to just have the tree colored in black and white, but for the sake of his wife the Queen, he decided that the tree will contain all the favorite colors of their kk children (they all have distinct favorite colors). Clearly, no two neighboring nodes can have the same color, but otherwise any coloring of the tree using exactly the kk colors would make a feasible flag candidate.

How many different flags are possible?

Input

The first line contains two integers nn and kk (2 ≤ k ≤ n ≤ 2500 2 ≤ k ≤ n ≤ 2500), where nn is the number of nodes in the King's favorite tree and kk is the number of children. Then follow n−1 lines describing the edges in the tree; the ii'th of these lines contains a non-negative integer pi<ipi<i, meaning that node pipi is the parent of ii.

The nodes are numbered from 0 to n−1 and the tree is rooted at node 0. Note that the embedding of the tree on the flag is already fixed, the only thing that remains is to assign colors.

Output

Output the number of different possible color assignments. The number can be quite big, so the King has requested to know the answer modulo 10000000071000000007.

Examples

input

Copy

4 3
0
1
1

output

Copy

18

input

Copy

6 4
0
1
1
3
4

output

Copy

600

 

【思路】

题意是国王有一棵树,他的孩子们喜欢K种颜色,希望国王能把这K种颜色涂到树的N个节点上,而且要保证相邻节点不同色,问涂色方案数。

我们令f(i)表示把至多i种颜色涂到树上的方案数,g(i)表示正好i种颜色涂到树上的方案数,显然这就有以下两项事实:

  • f(k) = \sum_{i = 0}^{k} \binom{k}{i} g(i)
  • f(i) = i \cdot (i - 1)^{n - 1}

我们得到f(i)是很容易的,要求g(k)却很难,那就需要反推g(i)的求法。

然后照搬二项式反演:f_n = \sum_{i = 0}^{n} \binom{n}{i} g_i \Rightarrow g_n = \sum_{i = 0}^{n}{(-1)}^{n - i}\binom{n}{i}f_i

 

【代码】

//******************************************************************************
// File Name: K.cpp
// Author: Shili_Xu
// E-Mail: shili_xu@qq.com
// Created Time: Sat 27 Oct 2018 06:36:50 PM CST
//******************************************************************************

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <cmath>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;

const int MAXN = 2505, MOD = 1e9 + 7;

int n, k;
ll C[MAXN][MAXN];

void init()
{
    C[0][0] = 1; C[0][1] = 0;
    for (int i = 1; i <= 2500; i++) {
        C[i][0] = 1; C[i][i + 1] = 0;
        for (int j = 1; j <= i; j++) C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % MOD;
    }
}

ll qpow(ll a, ll b, ll c)
{
    ll ans = 1;
    while (b) {
        if (b & 1) ans = ans * a % c;
        a = a * a % c;
        b >>= 1;
    }
    return ans;
}

int main()
{
    scanf("%d %d", &n, &k);
    for (int i = 1; i < n; i++) {
        int x;
        scanf("%d", &x);
    }
    init();
    ll ans = 0;
    for (int i = k, j = 1; i >= 0; i--, j = -j)
        ans = ((ans + (ll)j * C[k][i] * i % MOD * qpow(i - 1, n - 1, MOD) % MOD) % MOD + MOD) % MOD;
    printf("%lld\n", ans);
	return 0;
}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值