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

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. Android(java)学习笔记151:Android中操作JSON数据(Json和Jsonarray)

    1.Json 和 Xml       JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. JSON采用完全独立于语言的 ...

  2. iPhone Tutorials

    http://www.raywenderlich.com/tutorials This site contains a ton of fun written tutorials – so many t ...

  3. [BZOJ4899]:记忆的轮廓(概率DP)

    题目传送门 题目描述: 通往贤者之塔的路上,有许多的危机. 我们可以把这个地形看做是一颗树,根节点编号为1,目标节点编号为n,其中1-n的简单路径上,编号依次递增, 在[1,n]中,一共有n个节点.我 ...

  4. MYSQL 注射精华

    前言鄙人今天心血来潮突然想写篇文章,鄙人从来没写过文章,如果有错误的地方请多多指教.本文需要有基础的SQL语句知识才可以更好的理解.建议想学习的人多去了解一下SQL语句和编程语言,知己知彼才能百战百胜 ...

  5. bootstrap历练实例: 垂直胶囊式的导航菜单

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  6. shell脚本,当用sed删除某一文件里面的内容时,并追加到同一个文件会出现问题。

    shell脚本,当用sed删除某一文件里面的内容时,并追加到同一个文件会出现问题.因为初始文件和写入文件是一个文件这是失败的.需要追加到另一个文件,然后再用mv进行操作.[root@localhost ...

  7. 【OS_Linux】清空终端屏幕的方法

    使用clear命令或Ctrl+L组合键来清空终端屏幕,它不是真正的清空了,而是给人以错觉,当向上滚动鼠标后发现屏幕的内容依然在. 使用printf '\033c'才真正意义上清空了屏幕,它同DOS下的 ...

  8. RN在设备上运行

    https://facebook.github.io/react-native/docs/running-on-device.html 在发布之前,最好是在真实的设备上测试一下应用.如果是通过crea ...

  9. 如何删除SQL Server 2014连接到服务器中的服务器名称

    查看了网上的一些方法,均是以前版本的处理方法,不过原理都是删除"SqlStudio.bin",通过“Everything”搜索SqlStudio.bin,位置是: C:\Users ...

  10. HUD--2553 N皇后问题

    Problem Description 在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上.你的任务是,对于给定的N, ...