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. C# Winform实现手机号码归属地查询工具

    摘要:本文介绍使用C#开发基于Winform的手机号码归属地查询工具,并提供详细的示例代码供参考. 一.需求描述 输入正确的手机号码,查询该号码的归属地和其他相关信息. 二.需求分析 1.实现手机号码 ...

  2. python之Memcached 安装及操作

    一.Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的 ...

  3. Java 9的JDK中值得期待的:不仅仅是模块化

    在多次延期后,Java 9将于9月21日以Java开发工具包9的形式出现,这是自2014年3月以来,Java标准版的第一次重大升级.官方列出了JDK 9的大约90个新特性,模块化是最主要的一个.将Ja ...

  4. [myeclipse]@override报错问题

    @Override是JDK5 就已经有了,但有个小小的Bug,就是不支持对接口的实现,认为这不是Override 而JDK6 修正了这个Bug,无论是对父类的方法覆盖还是对接口的实现都可以加上@Ove ...

  5. 【Error】Creating Server TCP listening socket *:6379: bind: No such file or directory

    redis 运行服务时报错: Creating Server TCP listening socket *:6379: bind: No such file or directory 解决方法,依次输 ...

  6. Diff Two Arrays

    比较两个数组,然后返回一个新数组,该数组的元素为两个给定数组中所有独有的数组元素.换言之,返回两个数组的差异. 这是一些对你有帮助的资源: Comparison Operators Array.sli ...

  7. C++高级编程1 C++速成

    C++高级编程1 C++速成 1.常用的预处理指令 #include [file] #define key value 这个是在C中经常使用的,定义常量数值,用于字符串替换的工作. #ifndef k ...

  8. eureka-4-eureka server 高可用

    Eureka Server 可以通过运行多个实例并相互注册实现高可用部署.Eureka Server 实例之间会彼此增量同步信息,确保所有节点信息一致. 关键点: 配置: eureka.client. ...

  9. LeetCode OJ:Number of 1 Bits(比特1的位数)

    Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...

  10. JBPM4入门——1.jbpm简要介绍

    本博文只是简要对JBPM4进行介绍,如需更详细内容请自行google 链接: JBPM入门系列文章: JBPM4入门——1.jbpm简要介绍 JBPM4入门——2.在eclipse中安装绘制jbpm流 ...