Codeforces 382E Ksenia and Combinatorics


Ksenia has her winter exams. Today she is learning combinatorics. Here’s one of the problems she needs to learn to solve.

How many distinct trees are there consisting of n vertices, each with the following properties:

the tree is marked, that is, the vertices of the tree are numbered from 1 to n;

each vertex of the tree is connected with at most three other vertices, and at the same moment the vertex with number 1 is connected with at most two other vertices;

the size of the tree’s maximum matching equals k.

Two trees are considered distinct if there are such two vertices u and v, that in one tree they are connected by an edge and in the other tree they are not.

Help Ksenia solve the problem for the given n and k. As the answer to the problem can be very huge you should output it modulo 1000000007 (109 + 7).

Input

The first line contains two integers n, k (1 ≤ n, k ≤ 50).

Output

Print a single integer — the answer to the problem modulo 1000000007 (109 + 7).

Examples

input

1 1

output

0

input

2 1

output

1

input

3 1

output

3

input

4 2

output

12

Note

If you aren’t familiar with matchings, please, read the following link: http://en.wikipedia.org/wiki/Matching_(graph_theory).


简洁版题意:有n个节点的数,每个节点有不同的标号,求生成树匹配大小为k的方案数,其中除了根节点所有节点的度数不超过3,根节点1度数不超过2

考虑DP计数吧

dp[i][j][0/1]" role="presentation">dp[i][j][0/1]dp[i][j][0/1]表示大小为i的子树,最大匹配是j,是否匹配当前节点的方案数

然后当i==n的时候和左右子树大小相同的情况需要特判掉

然后就是常规DP

#include<bits/stdc++.h>
using namespace std;
#define yyf 1000000007
#define N 60
#define LL long long
LL dp[N][N][2];
LL C[N][N];
void update(LL &a,LL b){a=(a+b)%yyf;}
int main(){
LL n,k;scanf("%lld%lld",&n,&k);
if(k*2>n){printf("0");return 0;}
for(LL i=0;i<=n;i++)C[i][0]=1;
for(LL i=1;i<=n;i++)
for(LL j=1;j<=i;j++)C[i][j]=(C[i-1][j-1]+C[i-1][j])%yyf;
dp[1][0][0]=dp[0][0][1]=1;
for(LL siz=2;siz<=n;siz++)
for(LL mac=1;mac<=k;mac++){
//不匹配当前节点
for(LL ls=0,rs=siz-1;ls<=rs;ls++,rs--){
for(LL lm=0,rm=mac;lm<=mac;lm++,rm--){
if(lm*2>ls||rm*2>rs)continue;
LL tmp=(ls==rs?C[siz-2][ls-1]:C[siz-1][ls])*(siz==n?1:siz)%yyf;//特判掉左边大小等于右边
update(dp[siz][mac][0],dp[ls][lm][1]*dp[rs][rm][1]%yyf*tmp%yyf);
}
}
//匹配当前节点
for(LL ls=0,rs=siz-1;ls<=rs;ls++,rs--){
for(LL lm=0,rm=mac-1;lm<mac;lm++,rm--){
if(lm*2>ls||rm*2>rs)continue;
LL tmp=(ls==rs?C[siz-2][ls-1]:C[siz-1][ls])*(siz==n?1:siz)%yyf;
update(dp[siz][mac][1],dp[ls][lm][1]*dp[rs][rm][0]%yyf*tmp%yyf);
update(dp[siz][mac][1],dp[ls][lm][0]*dp[rs][rm][1]%yyf*tmp%yyf);
update(dp[siz][mac][1],dp[ls][lm][0]*dp[rs][rm][0]%yyf*tmp%yyf);
}
}
}
printf("%lld",(dp[n][k][0]+dp[n][k][1])%yyf);
return 0;
}

Codeforces 382E Ksenia and Combinatorics 【组合计数】*的更多相关文章

  1. Codeforces 932E Team work 【组合计数+斯特林数】

    Codeforces 932E Team work You have a team of N people. For a particular task, you can pick any non-e ...

  2. codeforces 691F F. Couple Cover(组合计数)

    题目链接: F. Couple Cover time limit per test 3 seconds memory limit per test 512 megabytes input standa ...

  3. bzoj 2281 [Sdoi2011]黑白棋(博弈+组合计数)

    黑白棋(game) [问题描述] 小A和小B又想到了一个新的游戏. 这个游戏是在一个1*n的棋盘上进行的,棋盘上有k个棋子,一半是黑色,一半是白色. 最左边是白色棋子,最右边是黑色棋子,相邻的棋子颜色 ...

  4. BZOJ 4555: [Tjoi2016&Heoi2016]求和 [分治FFT 组合计数 | 多项式求逆]

    4555: [Tjoi2016&Heoi2016]求和 题意:求\[ \sum_{i=0}^n \sum_{j=0}^i S(i,j)\cdot 2^j\cdot j! \\ S是第二类斯特林 ...

  5. BZOJ 4555: [Tjoi2016&Heoi2016]求和 [FFT 组合计数 容斥原理]

    4555: [Tjoi2016&Heoi2016]求和 题意:求\[ \sum_{i=0}^n \sum_{j=0}^i S(i,j)\cdot 2^j\cdot j! \\ S是第二类斯特林 ...

  6. 【BZOJ5491】[HNOI2019]多边形(模拟,组合计数)

    [HNOI2019]多边形(模拟,组合计数) 题面 洛谷 题解 突然特别想骂人,本来我考场现切了的,结果WA了几个点,刚刚拿代码一看有个地方忘记取模了. 首先发现终止态一定是所有点都向\(n\)连边( ...

  7. [总结]数论和组合计数类数学相关(定理&证明&板子)

    0 写在前面 0.0 前言 由于我太菜了,导致一些东西一学就忘,特开此文来记录下最让我头痛的数学相关问题. 一些引用的文字都注释了原文链接,若侵犯了您的权益,敬请告知:若文章中出现错误,也烦请告知. ...

  8. 【BZOJ5323】[JXOI2018]游戏(组合计数,线性筛)

    [BZOJ5323][JXOI2018]游戏(组合计数,线性筛) 题面 BZOJ 洛谷 题解 显然要考虑的位置只有那些在\([l,r]\)中不存在任意一个约数的数. 假设这样的数有\(x\)个,那么剩 ...

  9. 【BZOJ5305】[HAOI2018]苹果树(组合计数)

    [BZOJ5305][HAOI2018]苹果树(组合计数) 题面 BZOJ 洛谷 题解 考虑对于每条边计算贡献.每条边的贡献是\(size*(n-size)\). 对于某个点\(u\),如果它有一棵大 ...

随机推荐

  1. java 资源文件夹下的MEAT-INF

    META-INF文件夹是干啥的,META-INF文件夹的作用, META-INF文件夹能删吗 https://www.cnblogs.com/demingblog/p/5653844.html Jar ...

  2. 解决VS2015中出现类似于error C4996: 'scanf': This function or variable may be unsafe的安全检查错误

    用习惯了VS老版本的人当刚使用VS2013的时候可能总遇到类似于这样的错误: error C4996: 'scanf': This function or variable may be unsafe ...

  3. [javascript]Dom操作笔记

    1.为一个节点同时设置多个属性 $("div[aria-describedby='F53_batch_history']").attr({"display":& ...

  4. es6 nodejs compose

    const compose = (...fns) => { let len = fns.length; let fn_index = len - 1; let fn_result; functi ...

  5. javascript curry 柯里化函数 仿lodash的curry

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. Day34 设计模式

    参考博客: http://www.cnblogs.com/alex3714/articles/5760582.html 什么是设计模式 Christopher Alexander:“每一个模式描述了一 ...

  7. 【BZOJ】3389: [Usaco2004 Dec]Cleaning Shifts安排值班(贪心)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3389 显然左端点排序后,依次取. 要考虑下一次取的方案: 待选点为a[j].x<=a[now] ...

  8. C++多线程2.beginthread

    C++ 多线程2 beginthread 启动线程知识 20131021 Reference: http://blog.csdn.net/laoyang360/article/details/7720 ...

  9. 【51nod-1315】合法整数集(数位)

    [思路] 既然是or操作,将数转化为二进制,数位是1,对应的数组元素+1,再将x转为成二进制,只要查找X为1的位置,将之前存放的数组数字找个最小的输出就可以了. 但是并不是所有的数都要参与or,因为有 ...

  10. C# 调用颜色的RGB值_RGB颜色转换十六进制颜色

    调用方法: 如:btn_FangTai.BackColor = Color.FromArgb(135, 206, 250); 十六进制颜色查询 颜   色 英文代码 形象描述 十六进制 RGB   L ...