1170 - Counting Perfect BST

BST is the acronym for Binary Search Tree. A BST is a tree data structure with the following properties.

i)        Each BST contains a root node and the root may have zero, one or two children. Each of the children themselves forms the root of another BST. The two children are classically referred to as left child and right child.

ii)      The left subtree, whose root is the left children of a root, contains all elements with key values less than or equal to that of the root.

iii)    The right subtree, whose root is the right children of a root, contains all elements with key values greater than that of the root.

An integer m is said to be a perfect power if there exists integer x > 1 and y > 1 such that m = xy. First few perfect powers are {4, 8, 9, 16, 25, 27, 32, 36, 49, 64, 81, 100, 121, 125, 128, 144, ...}. Now given two integer a and b we want to construct BST using all perfect powers between a and b, where each perfect power will form the key value of a node.

Now, we can construct several BSTs out of the perfect powers. For example, given a = 1 and b = 10, perfect powers between a and b are 4, 8, 9. Using these we can form the following five BSTs.

4           4         8          9         9

  \          \      / \      /         /

    8          9   4     9   4         8

      \      /                 \      /

9   8                     8   4

In this problem, given a and b, you will have to determine the total number of BSTs that can be formed using perfect powers between a and b.

Input

Input starts with an integer T (≤ 20000), denoting the number of test cases.

Each case of input contains two integers: a and b (1 ≤ a ≤ b ≤ 1010, b - a ≤ 106) as defined in the problem statement.

Output

For each case, print the case number and the total number of distinct BSTs that can be formed by the perfect powers between a and b. Output the result modulo 100000007.

Sample Input

Output for Sample Input

4

1 4

5 10

1 10

1 3

Case 1: 1

Case 2: 2

Case 3: 5

Case 4: 0

分析:先筛选出a, b间的完美幂,然后就是求卡特兰数了,既可以用递推,也可以用求逆元的方法。

代码:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<set>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#define N 111111
#define mod 100000007
typedef long long ll;
using namespace std;

ll num[N];
ll d[1111];
int k;

void init()
{
for(ll i = 2; i * i < 10000000000L; ++i)
{
for(int j = 2; ; ++j)
{
if(pow(i, j) > 10000000000L)
break;

num[k++] = pow(i, j);
}
}
sort(num, num + k);

k = unique(num, num + k) - num;
/*在STL中unique函数是一个去重函数, unique的功能是去除相邻的重复元素(只保留一个),
其实它并不真正把重复的元素删除,是把重复的元素移到后面去了,然后依然保存到了原数组中,
然后 返回去重后最后一个元素的地址,因为unique去除的是相邻的重复元素,所以一般用之前都会要排一下序。
*/
memset(d, 0, sizeof(d));
d[0] = 1;
d[1] = 1;

for(int i = 2; i < 1111; i++)
{
for(int j = 1; j <= i; j++)
{
d[i] += d[i - j] * d[j-1];
d[i] %= mod;
}
/*Catalan数的定义

 令h(0)=1,h(1)=1,Catalan数满足递归式:h(n) = h(0)*h(n-1) + h(1)*h(n-2) + ... + h(n-1)*h(0) (n>=2)

 该递推关系的解为:h(n) = C(2n,n)/(n+1),n=0,1,2,3,... (其中C(2n,n)表示2n个物品中取n个的组合数)
*/
///卡特兰数应用链接:http://www.cnblogs.com/yaoyueduzhen/p/5456490.html

}
}
int getcnt(ll a, ll b)
{
int t1 = upper_bound(num, num+k, b) - num;///返回一个非递减序列中的第一个大于val的位置
int t2 = lower_bound(num, num+k, a) - num;///返回一个非递减序列中的第一个大于等于值val的位置
return t1 - t2;
}
int main(void)
{
int T, cas;

ll a, b;
scanf("%d", &T);

cas = 0;
init();

d[0] = 0;
while(T--)
{

scanf("%lld%lld", &a, &b);
cas++;

printf("Case %d: %lld\n", cas, d[getcnt(a, b)]);
}
}

light oj1170 - Counting Perfect BST卡特兰数的更多相关文章

  1. 1170 - Counting Perfect BST

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

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

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

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

    题目大概就是求一个n个不同的数能构造出几种形态的二叉排序树. 和另一道经典题目n个结点二叉树不同形态的数量一个递推解法,其实这两个问题的解都是是卡特兰数. dp[n]表示用n个数的方案数 转移就枚举第 ...

  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. 通过例子进阶学习C++(七)CMake项目通过模板库实现约瑟夫环

    本文是通过例子学习C++的第七篇,通过这个例子可以快速入门c++相关的语法. 1.问题描述 回顾一下约瑟夫环问题:n 个人围坐在一个圆桌周围,现在从第 s 个人开始报数,数到第 m 个人,让他出局:然 ...

  2. 网络流 - 最大流构图入门 bzoj 1305

    一次舞会有n个男孩和n个女孩.每首曲子开始时,所有男孩和女孩恰好配成n对跳交谊舞.每个男孩都不会和同一个女孩跳两首(或更多)舞曲.有一些男孩女孩相互喜欢,而其他相互不喜欢(不会“单向喜欢”).每个男孩 ...

  3. DP-直线分割递推

    在 DP  里有一类是直线分割平面的问题 , 也是属于递推 类的 . 一 . 直线分割平面的问题 先考虑第一个小问题 : n 条直线最多可以将平面分割成几部分 ? 想想 最优的分割方法是怎样的呢 ? ...

  4. day6 云道页面 知识点梳理(1)

    关于块级元素.行内元素.行内块元素的梳理 (1)块级元素 特点:   a.可以设置宽高,行高,外边距和内边距   b.块级元素会独占一行    c.宽度默认是容器的100%    d.可以容纳内联元素 ...

  5. Django admin的常用方法

    一.HTTP 1.主页面 http://127.0.0.1:8000/admin/ 2.查询页面 http://127.0.0.1:8000/admin/app01/book/ 3.增加页面 http ...

  6. spring boot部署中executable的系统服务

    首先在pom.xml 中添加spring boot插件,并设置 <plugins> <plugin> <groupId>org.springframework.bo ...

  7. spring boot配置spring-data-jpa的时候报错CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is java.lang.NoSuchMethodError

    org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager f ...

  8. 第二阶段冲刺个人任务——three

    今日任务: 优化统计个人博客结果页面的显示. 昨日成果: 优化作业查询结果,按学号排列.

  9. 玩转Django2.0---Django笔记建站基础十一(一)(音乐网站开发)

    第十一章 音乐网站开发 本章以音乐网站项目为例,介绍Django在实际项目开发中的应用,该网站共分为6个功能模块分别是:网站首页.歌曲排行榜.歌曲播放.歌曲点评.歌曲搜索和用户管理. 11.1 网站需 ...

  10. Scala 学习(7)之「trait (1) 」

    作为接口使用 在 triat 中可以定义抽象方法,就与抽象类中的抽象方法一样,只要不给出方法的具体实现即可 类可以使用 extends 关键字继承 trait,注意,这里不是 implement,而是 ...