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. 硕毕论文_基于 3D 卷积神经网络的行为识别算法研究

    论文标题:基于 3D 卷积神经网络的行为识别算法研究 来源/作者机构情况: 中  国  地  质  大  学(北京),计算机学院,图像处理方向 解决问题/主要思想贡献: 1. 使用张量CP分解的原理, ...

  2. 原生js上传图片

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. vue 接口统一管理

    在外部创建一个API文件夹,然后创建一个API.js  例如 const filmbanner = 'api/billboard/home?__t=1498823077473'; const film ...

  4. [Python3网络爬虫开发实战] 1.8.2-Scrapy的安装

    Scrapy是一个十分强大的爬虫框架,依赖的库比较多,至少需要依赖的库有Twisted 14.0.lxml 3.4和pyOpenSSL 0.14.在不同的平台环境下,它所依赖的库也各不相同,所以在安装 ...

  5. Linux一键安装web环境全攻略phpstudy版

    此教程主要是应对阿里云Linux云服务器ecs的web环境安装,理论上不限于阿里云服务器,此教程对所有Linux云服务器都具有参考价值. 写这篇文章的目的:网上有很多关于Linux一键安装web环境全 ...

  6. 将json格式转为url参数格式的方法(xjl456852整理修改)

    测试页面: <html> <head> <script type="text/javascript" src="jquery-1.11.3. ...

  7. Pycharm下GitHub配置使用

    1.下载并安装git 要连接GitHub,首先git是必不可少的,git的安装的基本使用很简单,这里略过.. 2.如图所示,进入Pycharm的Setting>>> Version ...

  8. Codeforces Round #457 (Div. 2) B

    B. Jamie and Binary Sequence (changed after round) time limit per test 2 seconds memory limit per te ...

  9. POJ训练计划

    POJ训练计划 Step1-500题 UVaOJ+算法竞赛入门经典+挑战编程+USACO 请见:http://acm.sdut.edu.cn/bbs/read.php?tid=5321 一.POJ训练 ...

  10. CAS--CompareAndSwap原理

    1.CAS(Compare-and-Swap),即比较并替换,是一种实现并发算法时常用到的技术,Java并发包中的很多类都使用了CAS技术. CAS需要有3个操作数:内存地址V,旧的预期值A,即将要更 ...