2-3 Trees

Problem Description

2-3 tree is an elegant data structure invented by John Hopcroft. It is designed to implement the same functionality as the binary search tree. 2-3 tree is an ordered rooted tree with the following properties:

  • the root and each internal vertex have either 2 or 3 children;
  • the distance from the root to any leaf of the tree is the same.

The only exception is the tree that contains exactly one vertex — in this case the root of the tree is the only vertex, and it is simultaneously a leaf, i.e. has no children. The main idea of the described properties is that the tree with l leaves has the height O(log l).
      Given the number of leaves l there can be several valid 2-3 trees that have l leaves. For example, the picture below shows the two possible 2-3 trees with exactly 6 leaves.

Given l find the number of different 2-3 trees that have l leaves. Since this number can be quite large, output it modulo r.

Input

      Input file contains two integer numbers: l and r (1 ≤ l ≤ 5 000, 1 ≤ r ≤ 109).

Output

      Output one number — the number of different 2-3 trees with exactly l leaves modulo r.

Sample Input

6 1000000000
7 1000000000

Sample Output

2
3

Source

Andrew Stankevich Contest 21
 
题意:
  2-3树定义为,每个非叶子节点都有2个或者是3个儿子;根节点到每个叶子的距离都相等
  问你对于给定的n个叶子节点其树不同形态有多少种
题解:
  DP[i] 表示叶子节点数为i的树的方案,枚举2,3儿子的节点的数量,得到下一层,注意了,23排列不同造成形态不同,这个要排列组合计算
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long LL;
const int N=4e5+,mods=,inf=2e9+; int n,mod,c[][];
LL dp[N];
int main() {
while(scanf("%d%d",&n,&mod)!=EOF) {
for(int i = ; i <= ; ++i) {
c[i][] = ;
for(int j = ; j <= i; ++j) {
c[i][j] = (c[i-][j] + c[i-][j-])%mod;
}
}
memset(dp,,sizeof(dp));
dp[] = ;
for(int i = ; i <= n; ++i) {
for(int j = ; j <= i; ++j) {
if(j* + (i-j)* <= )
dp[j* + (i-j)*] += dp[i]*c[i][j],
dp[j* + (i-j)*]%=mod;
}
}
printf("%lld\n",dp[n]%mod);
}
return ;
}

ACdream 1412 DP+排列组合的更多相关文章

  1. 【BZOJ】2111: [ZJOI2010]Perm 排列计数 计数DP+排列组合+lucas

    [题目]BZOJ 2111 [题意]求有多少1~n的排列,满足\(A_i>A_{\frac{i}{2}}\),输出对p取模的结果.\(n \leq 10^6,p \leq 10^9\),p是素数 ...

  2. 【BZOJ】4559: [JLoi2016]成绩比较 计数DP+排列组合+拉格朗日插值

    [题意]n位同学(其中一位是B神),m门必修课,每门必修课的分数是[1,Ui].B神碾压了k位同学(所有课分数<=B神),且第x门课有rx-1位同学的分数高于B神,求满足条件的分数情况数.当有一 ...

  3. G.subsequence 1(dp + 排列组合)

    subsequence 1 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K 64bit IO Format: %lld 题目描述 You are ...

  4. LightOJ1005 Rooks(DP/排列组合)

    题目是在n*n的棋盘上放k个车使其不互相攻击的方案数. 首先可以明确的是n*n最多只能合法地放n个车,即每一行都指派一个列去放车. dp[i][j]表示棋盘前i行总共放了j个车的方案数 dp[0][0 ...

  5. HDU 5816 状压DP&排列组合

    ---恢复内容开始--- Hearthstone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java ...

  6. bzoj 3398 [Usaco2009 Feb]Bullcow 牡牛和牝牛——前缀和优化dp / 排列组合

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3398 好简单呀.而且是自己想出来的. dp[ i ]表示最后一个牡牛在 i 的方案数. 当前 ...

  7. 【noi 2.6_9288】&【hdu 1133】Buy the Ticket(DP / 排列组合 Catalan+高精度除法)

    题意:有m个人有一张50元的纸币,n个人有一张100元的纸币.他们要在一个原始存金为0元的售票处买一张50元的票,问一共有几种方案数. 解法:(学习了他人的推导后~) 1.Catalan数的应用7的变 ...

  8. acdream 1412 2-3Trees (组合+DP)

    题意:2-3树的每个结点(除了叶子外)有2或3个孩子(分支),假设是一个满2-3树,那么给出叶子的数量,求这样的树有多少棵.(注:有2个孩子的结点视为相同,有3个孩子的结点视为相同,比如倒数第2层有4 ...

  9. 【BZOJ-1974】auction代码拍卖会 DP + 排列组合

    1974: [Sdoi2010]auction 代码拍卖会 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 305  Solved: 122[Submit ...

随机推荐

  1. win7 x64安装glpk

    下载glpk,下载地址:http://ftp.gnu.org/gnu/glpk/

  2. Linux 搭建 squid 代理服务器 三种模式

    CentOS 6.7 squid 代理服务器 一般有两张或以上网卡,一张链接公网,访问外网资源,一张位于局域网. 代理服务器可以提供文件缓存.复制和地址过滤等服务,充分利用有限的出口带宽,加快内部主机 ...

  3. swift -从相册中选择照片并上传

    选择本地图片并上传是应用开发中一个比较常见的功能.        原文出自:www.hangge.com  转载请保留原文链接:http://www.hangge.com/blog/cache/det ...

  4. 添物不花钱学javaEE--CSS

    CSS是什么? CSS: Cascading Style Sheets CSS官方网址: https://www.w3.org/TR/2011/REC-CSS2-20110607/ 其实仔细研究这个就 ...

  5. noip模拟赛 终末

    分析:举个例子就能发现:偶数位上的数都必须是0,奇数位上的数可以取0~k-1,这就是一个标准的数位dp了. 这编译器......数组越界了竟然不报错. #include <cstdio> ...

  6. bzoj 1005 [HNOI2008] 明明的烦恼 (prufer编码)

    [HNOI2008]明明的烦恼 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 5907  Solved: 2305[Submit][Status][Di ...

  7. 51 nod 1007 正整数分组 (简单01背包) && csu 1547: Rectangle

    http://www.51nod.com/onlineJudge/questionCode.html#problemId=1007&noticeId=15020 求出n个数的和sum,然后用s ...

  8. Ubuntu 16.04创建Swap分区或增加Swap分区容量(转)

    要在Ubuntu中要创建Swap分区主要有如下2种方式: 一.传统创建方式 一般情况下,我们都会使用dd命令来预先创建交换分区文件,然后再用/dev/zero将该文件的内容全部置零,创建时还将用到bs ...

  9. Ubuntu 16.04升级Linux内核为4.7.0最快的方法

    升级内容有很多好处,比如支持最新硬件驱动,使系统更安装等.但是升级内容也会带来一些问题,比如一些软件的兼容性问题,从而出现一些莫名其妙的问题等,所以升级时要慎重考虑. 升级方法: 下载脚本: http ...

  10. 原则 principles

    1.找到对的人来讨论问题. 2.把工作分配给对的人才能把事情做对. 3.