题目大概就是求一个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. 使用python在SAE上搭建一个微信应用,使用有道翻译的api进行在线翻译

    1. 准备,先在使用python一步一步搭建微信公众平台(一)中基本实现自动回复的功能后,接着在有道词典上申请一个key,http://fanyi.youdao.com/openapi?path=da ...

  2. 你真的会用AsyncTask吗?(一)

    一个典型AsyncTask的. view source print? 01 public class DialogTestActivity extends Activity { 02     priv ...

  3. linux 查看系统信息命令(比较全)

    linux 查看系统信息命令是linux初学者必备的基础知识, 这些命令也非常有用, 因为进入linux第一件事就可能是首先查看系统信息, 因此必要的系统的学习一下这些linux系统信息命令还是非常有 ...

  4. 【系统】CentOS、Ubuntu、Debian三个linux比较异同

    CentOS.Ubuntu.Debian三个linux比较异同 2014-07-31 12:58             53428人阅读             评论(6)             ...

  5. 查看Eclipse中的jar包的源代码:jd-gui.exe

    前面搞了很久的使用JAD,各种下载插件,最后配置好了,还是不能用,不知道怎么回事, 想起一起用过的jd-gui.exe这个工具,是各种强大啊!!! 只需要把jar包直接扔进去就可以了,非常清晰,全部解 ...

  6. Google Code Jam 2014 Round 1B Problem B

    二进制数位DP,涉及到数字的按位与操作. 查看官方解题报告 #include <cstdio> #include <cstdlib> #include <cstring& ...

  7. 转MYSQL学习(二) 运算符

    MYSQL中的运算符很多,这一节主要讲MYSQL中有的,而SQLSERVER没有的运算符 安全等于运算符(<=>) 这个操作符和=操作符执行相同的比较操作,不过<=>可以用来判 ...

  8. canva实践小实例 —— 马赛克效果

    前面给大家带来了操作像素的API,此时此刻,我觉得应该配以小实例来进行进一步的说明和演示,以便给大家带来更宽广的视野和灵感,你们看了我的那么多的文章,应该是懂我的风格,废话不多说,进入正题: 这次给大 ...

  9. 【linux】VMware12.0安装

    问题1:安装虚拟机时出现The Microsoft Runtime DLL installer failed to complete 解决: 重新安装,安装失败时不要点击结束,在开始“运行”出输入%t ...

  10. [MAC] mac系统如何显示和隐藏文件

    转载地址: http://www.cnblogs.com/lm3515/archive/2010/12/08/1900271.html 显示Mac隐藏文件的命令:defaults write com. ...