题目大概就是求一个n个不同的数能构造出几种形态的二叉排序树。

和另一道经典题目n个结点二叉树不同形态的数量一个递推解法,其实这两个问题的解都是是卡特兰数。

  • dp[n]表示用n个数的方案数
  • 转移就枚举第几个数作为根,然后分成左右两子树,左右两子树的方案数的乘积就是这个数作根的方案数

另外就是题目得先找到[1,1e10]的perfect power,总共102230个;输入的区间[a,b],b-a>=1e6,也就是最多perfect power的个数大概就在a=1,b=1000001范围内,1110个perfect power。

 #include<cstdio>
#include<cstring>
#include<set>
#include<algorithm>
using namespace std;
long long pow(long long x,int y){
long long res=;
for(int i=; i<y; ++i) res*=x;
return res;
}
long long pownum[];
int pn;
int getCnt(long long a,long long b){
return (upper_bound(pownum,pownum+pn,b)-pownum)-(lower_bound(pownum,pownum+pn,a)-pownum);
}
long long d[];
int main(){
for(long long i=; i*i<=10000000000L; ++i){
for(int j=; ; ++j){
if(pow(i,j)>10000000000L) break;
pownum[pn++]=pow(i,j);
}
}
sort(pownum,pownum+pn);
pn=unique(pownum,pownum+pn)-pownum;
d[]=d[]=;
for(int i=; i<; ++i){
for(int j=; j<=i; ++j){
d[i]+=d[j-]*d[i-j];
d[i]%=;
}
}
d[]=;
long long a,b;
int t;
scanf("%d",&t);
for(int cse=; cse<=t; ++cse){
scanf("%lld%lld",&a,&b);
printf("Case %d: %lld\n",cse,d[getCnt(a,b)]);
}
return ;
}

LightOJ1170 - Counting Perfect BST(卡特兰数)的更多相关文章

  1. light oj1170 - Counting Perfect BST卡特兰数

    1170 - Counting Perfect BST BST is the acronym for Binary Search Tree. A BST is a tree data structur ...

  2. 1170 - Counting Perfect BST

    1170 - Counting Perfect BST   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 3 ...

  3. LightOJ - 1170 - Counting Perfect BST(卡特兰数)

    链接: https://vjudge.net/problem/LightOJ-1170 题意: BST is the acronym for Binary Search Tree. A BST is ...

  4. LightOj 1170 - Counting Perfect BST (折半枚举 + 卡特兰树)

    题目链接: http://www.lightoj.com/volume_showproblem.php?problem=1170 题目描述: 给出一些满足完美性质的一列数(x > 1 and y ...

  5. HDOJ/HDU 1133 Buy the Ticket(数论~卡特兰数~大数~)

    Problem Description The "Harry Potter and the Goblet of Fire" will be on show in the next ...

  6. 【高精度练习+卡特兰数】【Uva1133】Buy the Ticket

    Buy the Ticket Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  7. Buy the Ticket(卡特兰数+递推高精度)

    Buy the Ticket Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...

  8. Buy the Ticket HDU 1133 卡特兰数应用+Java大数

    Problem Description The "Harry Potter and the Goblet of Fire" will be on show in the next ...

  9. [LeetCode系列]卡特兰数(Catalan Number) 在求解独特二叉搜寻树(Unique Binary Search Tree)中的应用分析

    本文原题: LeetCode. 给定 n, 求解独特二叉搜寻树 (binary search trees) 的个数. 什么是二叉搜寻树? 二叉查找树(Binary Search Tree),或者是一棵 ...

随机推荐

  1. crontab报错

    $mail 选择对应的数字查看邮件 File "/opt/app/python/tv-cms-for-ch/current/scripts/sync_cms_for_ch.py", ...

  2. 一起做RGB-D SLAM (1)

    前言 2016.11 更新 增加了对16.04的支持. 整理了过时的代码. SLAM,即Simultaneous Localization and Mapping,中文译作同时定位与地图创建,是近几十 ...

  3. nginx学习(二):初识配置文件

    nginx的配置文件默认在nginx安装目录中的conf子目录中,主配置文件为nginx.conf, root@mgmserver conf]# pwd/usr/local/nginx/conf一.配 ...

  4. HDU3345广搜 (P,E,T,#)

    War chess is hh's favorite game:In this game, there is an N * M battle map, and every player has his ...

  5. Linux MySQL差异备份技巧

    MSSQL差异备份使用技巧 15 Apr 2013 所谓的差异备份,就是只备份最近一次备份之后到此次备份之前所增加的那一部分数据.打个比方我第N次备份后数据库存放的内容是ABCD,然后我第N+1次 备 ...

  6. JS的trim()方法

    去除字符串左右两端的空格,在vbscript里面可以轻松地使用 trim.ltrim 或 rtrim,但在js中却没有这3个内置方法,需要手工编写.下面的实现方法是用到了正则表达式,效率不错,并把这三 ...

  7. ubuntu搭建lamp环境

    首先安装软件: sudo apt-get install apache2 sudo apt-get install php5 sudo apt-get install mysql-server sud ...

  8. Java性能优化权威指南-读书笔记(三)-JVM性能调优-内存占用

    新生代.老年代.永久代的概念不多说,这三个空间中任何一个不能满足内存分配请求时,就会发生垃圾收集. 新生代不满足内存分配请求时,发生Minor GC,老年代.永久代不满足内存分配请求时,发生Full ...

  9. 将Excel文件.xls导入SQL Server 2005

    SQL2005 Microsoft SQL Server Management Studio Express管理器里,右键单击一个数据库,指向“任务”,再单击“导入数据”或“导出数据”中没有这个选项, ...

  10. javascript reduce map函数方法

    retduce: 对数组中的所有元素调用指定的回调函数.该回调函数的返回值为累积结果,并且此返回值在下一次调用该回调函数时作为参数提供. 语法     array1.reduce(callbackfn ...