http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1770

这是这次BSG白山极客挑战赛的B题。设p(i, j)表示节点个数为i,高度为j的AVL树的个数。

那么,对于1 <= k <= i-1

p[i][j] += p[k][j-1]*p[i-1-k][j-1]%MOD;

p[i][j] += p[k][j-2]*p[i-1-k][j-1]%MOD;

p[i][j] += p[k][j-1]*p[i-1-k][j-2]%MOD;

但是这样模拟是n^3的复杂度。显然是不行的。但是j和k的范围是会被i约束的。于是我优化了j那一层,本地就能跑得很快了。设置了一个from和to表示j这一维跑的范围,那么每次这一次j的最小值就是下一次from,这一次j的最大值就是下一次的to。如此即可。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <string>
#define LL long long
#define MOD 1000000007 using namespace std; const int maxN = ;
LL p[maxN][maxN]; void init()
{
int from, to, tfrom, tto;
memset(p, , sizeof(p));
p[][] = ;
p[][] = ;
from = ; to = ;
for (int i = ; i < maxN; ++i)
{
tfrom = to;
tto = from;
to++;
for (int j = from; j <= to; ++j)
{
for (int k = ; k < i; ++k)
{
p[i][j] += p[k][j-]*p[i--k][j-]%MOD;
if (j > )
{
p[i][j] += p[k][j-]*p[i--k][j-]%MOD;
p[i][j] += p[k][j-]*p[i--k][j-]%MOD;
}
p[i][j] %= MOD;
if (p[i][j])
{
tfrom = min(tfrom, j);
tto = max(tto, j);
}
}
}
from = tfrom;
to = tto;
}
//cout << "OK"<<endl;
} int main()
{
//freopen("test.in", "r", stdin);
init();
int n;
while (scanf("%d", &n) != EOF)
{
int ans;
LL t = ;
for (int i = ; i <= n; ++i) t = (t+p[n][i])%MOD;
ans = t;
printf("%d\n", ans);
}
return ;
}

ACM学习历程—51NOD 1412 AVL树的种类(递推)的更多相关文章

  1. 51nod 1412 AVL树的种类(dp)

    题目链接:51nod 1412 AVL树的种类 开始做的时候把深度开得过小了结果一直WA,是我天真了.. #include<cstdio> #include<cstring> ...

  2. 51nod 1412 AVL树的种类(经典dp)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1412 题意: 思路: 经典dp!!!可惜我想不到!! $dp[i][k] ...

  3. 51nod 1412 AVL树的种类

    非常简单的一道题,一眼题 枚举左儿子大小,再枚举深度即可 复杂度$O(n^2 log n)$ #include <cstdio> #include <cstring> #inc ...

  4. ACM学习历程—SNNUOJ 1116 A Simple Problem(递推 && 逆元 && 组合数学 && 快速幂)(2015陕西省大学生程序设计竞赛K题)

    Description Assuming a finite – radius “ball” which is on an N dimension is cut with a “knife” of N- ...

  5. ACM学习历程——HDU4814 Golden Radio Base(数学递推) (12年成都区域赛)

    Description Golden ratio base (GRB) is a non-integer positional numeral system that uses the golden ...

  6. 51nod 1412 AVL数的种类(DP

    题意给了n个节点 问AVL树的种类 卧槽 真的好傻 又忘记这种题可以打表了  就算n^3 也可以接受的 树的深度不大 那么转移方程很明显了 dp[i][j]   代表的是节点为n深度为j的树的种类 k ...

  7. ACM学习历程—51NOD 1685 第K大区间2(二分 && 树状数组 && 中位数)

    http://www.51nod.com/contest/problem.html#!problemId=1685 这是这次BSG白山极客挑战赛的E题. 这题可以二分答案t. 关键在于,对于一个t,如 ...

  8. ACM学习历程—HDU5700 区间交(树状数组 && 前缀和 && 排序)

    http://acm.hdu.edu.cn/showproblem.php?pid=5700 这是这次百度之星初赛2B的第五题.省赛回来看了一下,有这样一个思路:对于所有的区间排序,按左值排序. 然后 ...

  9. ACM学习历程—51NOD 1770数数字(循环节)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1770 这是这次BSG白山极客挑战赛的A题.由于数字全部相同,乘上b必然会 ...

随机推荐

  1. Java应对Flash XSS攻击

    问题引出: 今天公司派出安全任务,说是要解决一个Flash XSS攻击,一看顿时傻眼,都没听说过.而且flash已经淘汰了,根本没研究过flash,搜了资料才开始慢慢开始工作. 要求: 1.过滤URL ...

  2. mongodb 的Cursor 作为 stream 的时候,读出来的数据数字开头的key没法访问(又踩了一个坑)

    mongdb 用Cursor 读取数据的时候,直接用流读出来的数据key是数字开头的话,就是独具不到,用Object.keys() 把所有的key 打印出来的话如下:怎么会是这样的呢? 查看了一下文档 ...

  3. PHP练习题一

    目录:1.如何使用php导入导出csv?2.php接收POST数据的方式有哪些?3.如何让json_encode()不转义斜杠?我在做服务器返回一些数据时需要返回一些地址,但是默认的json_code ...

  4. AtCoder Regular Contest 095

    AtCoder Regular Contest 095 C - Many Medians 题意: 给出n个数,求出去掉第i个数之后所有数的中位数,保证n是偶数. \(n\le 200000\) 分析: ...

  5. js 执行跨域刷新页面

    主要代码: 注意这段代码 是子页面中添加的也就是弹出的那个页面刷新父页面 <script type="text/javascript"> function shuaxi ...

  6. RENOUNCEMENT

    I must not think of thee;and,tired yet syrong,I shun the thought that lurks in all delight--The thou ...

  7. PHPAdmin的安装和配置

    phpadmin是用于管理mysql数据库的一个产品,,毕竟很多数据库服务器不能够公开连接,所以只能够使用http的方式来进行连接管理.     下载phpadmin( http://xj-http. ...

  8. 换个思维,boot结合vue做后台管理

    可以添加,可以删除.动态的添加数据. 不用操作dom,只要操作json数据即可. <form class="form-horizontal addForm" id=" ...

  9. LeetCode第[17]题(Java):Letter Combinations of a Phone Number

    题目:最长公共前缀 难度:EASY 题目内容: Given a string containing digits from 2-9 inclusive, return all possible let ...

  10. js进阶---12-10、jquery绑定事件和解绑事件是什么

    js进阶---12-10.jquery绑定事件和解绑事件是什么 一.总结 一句话总结:on和off. 1.jquery如何给元素绑定事件? on方法 22 $('#btn1').on('click', ...