坑爹的,,组合数模板,,,

6132 njczy2010 1412 Accepted 5572 MS 50620 KB C++ 1844 B 2014-10-02 21:41:15

J - 2-3 Trees

Time Limit: 12000/6000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others)

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

题解转自:http://acdream.info/topic?tid=3623

J题:问你一棵有l个叶子的2-3叉树有多少种拓扑结构,结果对r取余。。。 2-3叉树的定义为所有非叶子节点要么有2个儿子,要么有3个儿子,并且所有叶子到根的距离相等。。。 (关于那个O(logn)。。。其实这句话完全是废话,貌似好多人被这句废话给坑到了。。大O只是一个标记,表示渐进的意思,就是说这种树的高度和叶子数n成渐进对数关系)

J题:首先预处理出前2500行的组合数(杨辉三角递推即可,别忘了取余),然后初始化dp[1]=1,若只有根,也有一种情况
然后进行dp,注意到一个性质,2-3树的所有叶子都在同一层,于是就可以通过排列组合来计算叶子的方法数。
于是就转移到了上一层的情况,一层一层记忆化上去,或者递推下来= =。。反正是单组数据~~~

注意用 lld。。。

 #include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<string>
//#include<pair> #define N 5005
#define M 15
#define mod 10000007
//#define p 10000007
#define mod2 100000000
#define ll long long
#define LL long long
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; ll l,r;
ll dp[N];
ll C[N/][N/]; void ini()
{
// memset(C,0,sizeof(C));
int i,j;
for(i=; i<=l/; ++i)
{
C[i][] = ;
C[i][i] = ;
for(j=; j<=l/; ++j){
C[i][j] = (C[i-][j] + C[i-][j-]) % r;
} }
} void solve()
{
ll i;
ll num;
ll st;
memset(dp,,sizeof(dp));
dp[]=;
dp[]=;
dp[]=dp[]=;
for(i=;i<=l;i++){
st=;
if(i%==){
st=;
}
for(;*st<=i;st+=){
num=st+(i-*st)/;
dp[i]=(dp[i]+(C[num][st]*dp[num])%r)%r;
}
}
} void out()
{
//for(int i=1;i<=l;i++) printf(" i=%d dp=%d\n",i,dp[i]);
printf("%lld\n",dp[l]);
} int main()
{
// freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
//scanf("%d",&T);
// for(int ccnt=1;ccnt<=T;ccnt++)
// while(T--)
while(scanf("%lld%lld",&l,&r)!=EOF)
{
//if(n==0 && m==0 ) break;
//printf("Case %d: ",ccnt);
ini();
solve();
out();
} return ;
}

Andrew Stankevich's Contest (21) J dp+组合数的更多相关文章

  1. [Andrew Stankevich's Contest#21] Lempel-Ziv Compression

    Time Limit: 20000/10000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others)     Special Judge ...

  2. Contest 20140708 testB dp 组合数

    testB 输入文件: testB.in  输出文件testB.out 时限3000ms 问题描述: 定义这样一个序列(a1,b1),(a2,b2),…,(ak,bk)如果这个序列是方序列的话必须满足 ...

  3. 【模拟ACM排名】ZOJ-2593 Ranking (Andrew Stankevich’s Contest #5)

    真心是道水题,但找bug找的我想剁手了/(ㄒoㄒ)/~~ 注意几个坑点, 1.输入,getline(cin); / gets(); 一行输入,注意前面要加getchar();   输入运行记录的时候可 ...

  4. Andrew Stankevich&#39;s Contest (1)

    Andrew Stankevich's Contest (1) 打一半出门了,回来才补完了...各种大数又不能上java..也是蛋疼无比 A:依据置换循环节非常easy得出要gcd(x, n) = 1 ...

  5. 2016-2017 ACM-ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred) J dp 背包

    J. Bottles time limit per test 2 seconds memory limit per test 512 megabytes input standard input ou ...

  6. acdream:Andrew Stankevich Contest 3:Two Cylinders:数值积分

    Two Cylinders Special JudgeTime Limit: 10000/5000MS (Java/Others)Memory Limit: 128000/64000KB (Java/ ...

  7. [CSP-S模拟测试]:长寿花(DP+组合数)

    题目描述 庭院里有一棵古树.圣诞节到了,我想给古树做点装饰,给他一个惊喜.他会不会喜欢呢?这棵树可以分为$n$层,第$i$层有$a_i$个防治装饰品的位置,有$m$种颜色的装饰品可供选择.为了能让他喜 ...

  8. 2019 牛客暑期多校 G subsequence 1 (dp+组合数)

    题目:https://ac.nowcoder.com/acm/contest/885/G 题意:给你两个串,要求上面哪个串的子序列的值大于下面这个串的值的序列个数,不含前导零 思路:我们很容易就可以看 ...

  9. hdu----(5045)Contest(数位dp)

    Contest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

随机推荐

  1. 动态规划初步--最长上升子序列(LIS)

    一.问题 有一个长为n的数列 a0,a1,a2...,an-1a.请求出这个序列中最长的上升子序列的长度和对应的子序列.上升子序列指的是对任意的i < j都满足ai < aj的子序列. 二 ...

  2. PAT (Advanced Level) Practise - 1098. Insertion or Heap Sort (25)

    http://www.patest.cn/contests/pat-a-practise/1098 According to Wikipedia: Insertion sort iterates, c ...

  3. layui和jquery冲突:Syntax error, unrecognized expression: +

    问题 layui创建table数据表格,但点击第二页时控制台报错,错误信息如下: 解决方法 https://fly.layui.com/jie/24224/ http://www.layui.com/ ...

  4. ios之UIWebView(1)

    UIWebView可以让你创建一个网页浏览器,类似safari,而不是在程序中启动safsri哦.是不是觉得很棒呢?废话少说,切入正题. 一.创建UIWebView [java] view plain ...

  5. Instance Methods are Curried Functions in Swift

    An instance method in Swift is just a type method that takes the instance as an argument and returns ...

  6. (26)zabbix脚本报警介质自定义(钉钉)

    zabbix机器人告警配置 首先在钉钉中创建一个群然后设置群机器人添加自定义机器人(webhook...) 添加后复制其中的webhook地址到报警脚本dingding.py中的webhook=... ...

  7. nxlog安装配置

    Nxlog安装配置文档 任     帅 1.安装nxlog,全部默认即可. 如果拷贝直接安装,没有拷贝可以下载.下载链接: https://nxlog.co/system/files/products ...

  8. PHP开发中涉及到emoji表情的几种处理方法!

    emoji表情 处理 一般Mysql表设计时,都是用UTF8字符集的.把带有emoji的昵称字段往里面insert一下就没了,整个字段变成了空字符串.这是怎么回事呢? 原来是因为Mysql的utf8字 ...

  9. The US in understimating Huawei, says founder Ren zhengfei

    Huawei Founder Ren Zhengfei has downplayed the impact of the US executive order that cripple Huawei' ...

  10. web开发框架之DRF

    RESTful架构解释: # Representational State Transfer 表现层状态转化 到底什么是RESTFul架构: 如果一个架构符合REST原则,就称它为RESTful架构. ...