Codeforces 382E Ksenia and Combinatorics 【组合计数】*
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 【组合计数】*的更多相关文章
- 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 ...
- codeforces 691F F. Couple Cover(组合计数)
题目链接: F. Couple Cover time limit per test 3 seconds memory limit per test 512 megabytes input standa ...
- bzoj 2281 [Sdoi2011]黑白棋(博弈+组合计数)
黑白棋(game) [问题描述] 小A和小B又想到了一个新的游戏. 这个游戏是在一个1*n的棋盘上进行的,棋盘上有k个棋子,一半是黑色,一半是白色. 最左边是白色棋子,最右边是黑色棋子,相邻的棋子颜色 ...
- 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是第二类斯特林 ...
- 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是第二类斯特林 ...
- 【BZOJ5491】[HNOI2019]多边形(模拟,组合计数)
[HNOI2019]多边形(模拟,组合计数) 题面 洛谷 题解 突然特别想骂人,本来我考场现切了的,结果WA了几个点,刚刚拿代码一看有个地方忘记取模了. 首先发现终止态一定是所有点都向\(n\)连边( ...
- [总结]数论和组合计数类数学相关(定理&证明&板子)
0 写在前面 0.0 前言 由于我太菜了,导致一些东西一学就忘,特开此文来记录下最让我头痛的数学相关问题. 一些引用的文字都注释了原文链接,若侵犯了您的权益,敬请告知:若文章中出现错误,也烦请告知. ...
- 【BZOJ5323】[JXOI2018]游戏(组合计数,线性筛)
[BZOJ5323][JXOI2018]游戏(组合计数,线性筛) 题面 BZOJ 洛谷 题解 显然要考虑的位置只有那些在\([l,r]\)中不存在任意一个约数的数. 假设这样的数有\(x\)个,那么剩 ...
- 【BZOJ5305】[HAOI2018]苹果树(组合计数)
[BZOJ5305][HAOI2018]苹果树(组合计数) 题面 BZOJ 洛谷 题解 考虑对于每条边计算贡献.每条边的贡献是\(size*(n-size)\). 对于某个点\(u\),如果它有一棵大 ...
随机推荐
- 《用 Python 学微积分》笔记 2
<用 Python 学微积分>原文见参考资料 1. 13.大 O 记法 比较两个函数时,我们会想知道,随着输入值 x 的增长或减小,两个函数的输出值增长或减小的速度究竟谁快谁慢.通过绘制函 ...
- JVM与垃圾回收机制(GC)和类的生命周期
JVM运行时数据区 GC(垃圾回收机制) 什么是垃圾回收机制: 在系统运行过程中,会产生一些无用的对象,这些对象占据着一定的内存,如果不对这些对象清理回收无用的是对象,可能会导致内存的耗尽,所以垃圾回 ...
- 浏览器DOM操作
HTML Node 节点 常用API 高效遍历 DOM Repaint and reflow 插入大量内容避免重绘和回流 style 样式操作 DOM事件 HTML - innerHTML:内部HTM ...
- python 中str format 格式化数字补0方法
>>> "{0:03d}".format(1)'001'>>> "{0:03d}".format(10)'010'> ...
- cocos2d3.0rc编译android工程
1. 在CMakeLists.txt中配置所有的cpp文件 2. 在proj.android/jni 下的Android.mk中配置所有的cpp文件 3.新建工程 cocos new mygame1 ...
- C++(二十九) — new 和 delete
1.基本用法,定义变量.数组.对象 class test { public: test(int a_, int b_) { a = a_; b = b_; cout << "构造 ...
- Regression 手动实现Gradient Descent
import numpy as np import matplotlib.pyplot as plt x_data = [338.,333.,328.,207.,226.,25.,179.,60.,2 ...
- 解决在for循环内判断条件多次执行
最近遇到的这个问题,就是在for循环内if判断的条件会多次执行. 例如,在返回的30数据中,a条目是第7条则会进行30次判断,弹出29次查无数据,也就是要点击29次关闭alert,很是让人不爽. 有了 ...
- Python -- 使用pickle 和 CPickle对数据对象进行归档和解析
经常遇到在Python程序运行中得到了一些字符串.列表.字典.对象等数据,想要长久的保存下来,方便以后使用,而不是简单的放入内存中关机断电就丢失数据. 这个时候Pickle模块就派上用场了,它可以将对 ...
- C++:获取指定目录下的所有文件
1.获得指定目录下的所有文件(不搜索子文件夹) 需要包含的头文件 #include <io.h> #include <string> #include <vector&g ...