LightOJ1170 - Counting Perfect BST(卡特兰数)
题目大概就是求一个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(卡特兰数)的更多相关文章
- light oj1170 - Counting Perfect BST卡特兰数
1170 - Counting Perfect BST BST is the acronym for Binary Search Tree. A BST is a tree data structur ...
- 1170 - Counting Perfect BST
1170 - Counting Perfect BST PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 3 ...
- LightOJ - 1170 - Counting Perfect BST(卡特兰数)
链接: https://vjudge.net/problem/LightOJ-1170 题意: BST is the acronym for Binary Search Tree. A BST is ...
- LightOj 1170 - Counting Perfect BST (折半枚举 + 卡特兰树)
题目链接: http://www.lightoj.com/volume_showproblem.php?problem=1170 题目描述: 给出一些满足完美性质的一列数(x > 1 and y ...
- HDOJ/HDU 1133 Buy the Ticket(数论~卡特兰数~大数~)
Problem Description The "Harry Potter and the Goblet of Fire" will be on show in the next ...
- 【高精度练习+卡特兰数】【Uva1133】Buy the Ticket
Buy the Ticket Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- Buy the Ticket(卡特兰数+递推高精度)
Buy the Ticket Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- Buy the Ticket HDU 1133 卡特兰数应用+Java大数
Problem Description The "Harry Potter and the Goblet of Fire" will be on show in the next ...
- [LeetCode系列]卡特兰数(Catalan Number) 在求解独特二叉搜寻树(Unique Binary Search Tree)中的应用分析
本文原题: LeetCode. 给定 n, 求解独特二叉搜寻树 (binary search trees) 的个数. 什么是二叉搜寻树? 二叉查找树(Binary Search Tree),或者是一棵 ...
随机推荐
- App架构设计经验谈:服务端接口的设计
App与服务器的通信接口如何设计得好,需要考虑的地方挺多的,在此根据我的一些经验做一些总结分享,旨在抛砖引玉. 安全机制的设计 现在,大部分App的接口都采用RESTful架构,RESTFul最重要的 ...
- python如何安装pip和easy_installer工具
1.在以下地址下载最新的PIP安装文件:http://pypi.python.org/pypi/pip#downloads 2.解压安装 3.下载Windows的easy installer,然后安装 ...
- Python os.system 和 os.popen的区别
(1) os.system # 仅仅在一个子终端运行系统命令,而不能获取命令执行后的返回信息 system(command) -> exit_statusExecute the command ...
- postgresql 锁的定位
今天碰到了一个问题,锁定穷根追底把postgresql的锁研究了一番. 数据库查看锁 可以通过表 pg_locks来查看有哪些锁.sql如下: select a.locktype,a.database ...
- linux文件分割(将大的日志文件分割成小的)
linux文件分割(将大的日志文件分割成小的) linux下文件分割可以通过split命令来实现,可以指定按行数分割和安大小分割两种模式.Linux下文件合并可以通过cat命令来实现,非常简单. 在L ...
- Python: 什么是*args和**kwargs
转自: http://blog.sina.com.cn/s/blog_65a8ab5d0101fglm.html http://blog.csdn.net/chenjinyu_tang/article ...
- Meta Programming
[本文链接] http://www.cnblogs.com/hellogiser/p/meta-programming.html [分析] Template Mataprogram,中文叫模板元编程. ...
- iOS CoreData 的级联删除等操作
关于CoreData 的基本操作在网上有一些中文资料,但是这些资料大多没有涉及CoreData的详细操作,只是简单的演示了最基本用法.像级联删除这种最基本的数据库操作都没有提到.今天在网上看到了一些英 ...
- cenos配置
#修复ifconfig1.查看 /sbin/ifconfig是否存在 echo $PATH2.查看ifconfig命令是否存在ls /sbin |grep ifconfig如果ifconfig命令存在 ...
- 越狱后想禁用Spotlight
如果你的是ios7越狱后不想用Spotlight搜索功能,大老板源的NoSpot ios7可轻松帮你实现.亲测可用……………………