链接:

https://vjudge.net/problem/LightOJ-1170

题意:

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.

思路:

考虑次方数较少,先打出来,每次查询个数。

卡特兰数打表。

代码:

// #include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<vector>
#include<string.h>
#include<set>
#include<queue>
#include<algorithm>
#include<math.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int MOD = 1e8+7;
const int MAXN = 1e6+10; int Ans[MAXN];
LL Val[MAXN];
int cnt; LL PowMod(LL a, LL b)
{
LL res = 1;
while(b)
{
if (b&1)
res = res*a%MOD;
a = a*a%MOD;
b >>= 1;
}
return res;
} void Init()
{
cnt = 0;
for (LL i = 2;i <= 100000;i++)
{
LL tmp = 1LL*i*i;
while (tmp <= 1e10)
{
Val[++cnt] = tmp;
tmp *= i;
}
}
sort(Val+1, Val+1+cnt);
cnt = unique(Val+1, Val+1+cnt)-(Val+1); Ans[0] = 0;
Ans[1] = 1;
for (int i = 2;i < MAXN;i++)
{
// F[n] = F[n-1] * (4 * n - 2) / (n + 1)
LL inv;
inv = PowMod(i+1, MOD-2);
Ans[i] = 1LL*Ans[i-1]*(4*i-2)%MOD*inv%MOD;
}
} int main()
{
// freopen("test.in", "r", stdin);
Init();
int t, time = 0;
scanf("%d", &t);
while(t--)
{
printf("Case %d:", ++time);
LL l, r;
scanf("%lld %lld", &l, &r);
int rl = upper_bound(Val+1, Val+1+cnt, l-1)-Val;
int rr = upper_bound(Val+1, Val+1+cnt, r)-Val;
printf(" %d\n", Ans[rr-rl]);
} return 0;
}

LightOJ - 1170 - 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. LightOj 1170 - Counting Perfect BST (折半枚举 + 卡特兰树)

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

  3. 1170 - Counting Perfect BST

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

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

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

  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. Apache Kafka Connect - 2019完整指南

    今天,我们将讨论Apache Kafka Connect.此Kafka Connect文章包含有关Kafka Connector类型的信息,Kafka Connect的功能和限制.此外,我们将了解Ka ...

  2. #安装memcache

    安装memcache sudo apt-get install memcached sudo apt search php-memcache sudo apt-get install php-memc ...

  3. 手把手带你写一个minishell

    先解释一下Shell : Shell是一个功能为命令行解释器的应用程序,连接了用户和Linux内核,让我们能高效和安全地使用Linux内核. 要写一个minishell,我们要先理解它的过程: 读取输 ...

  4. SpringCloud之Hystrix集群及集群监控turbine

    目的: Hystrix集群及监控turbine Feign.Hystrix整合之服务熔断服务降级彻底解耦 集群后超时设置 Hystrix集群及监控turbine 新建一个springboot工程mic ...

  5. 在springMVC的controller中获取request,response对象的一个方法

    ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttr ...

  6. 关于梯度下降之前需要进行feature scale的记录

    先上吴恩达老师的课件图 1.对于梯度下降而言,学习率很大程度上影响了模型收敛的速度.对于不同规模的特征,如果采用相同的学习率,那么如果学习率适应于scale大的数据,scale较小的数据由于学习率过大 ...

  7. 转:Java接口和抽象类

    转:http://www.cnblogs.com/dolphin0520/p/3811437.html 一.抽象类 在了解抽象类之前,先来了解一下抽象方法.抽象方法是一种特殊的方法:它只有声明,而没有 ...

  8. 关于WPF中的XAML

    XAML全称extensible application markup language(可扩展性标记语言) 可扩展应用程序标记语言(XAML)是一种声明性语言.概括来说,就是为应用程序构建UI.目前 ...

  9. 分享个免费的svn平台

    平时在工作中难免会用到svn,但是要自己搭建一个,未免成本太高,近来,本人接触到一个免费的svn平台(可能大神们早就发现了),个人使用还是足够了. 地址:https://svnbucket.com 相 ...

  10. python3基础之“函数(1)”

    1.type:查看当前字符串的类型 c=' print(type(c),c) b= int(c) print(type(b),b) num=" a=int(num,base=16) prin ...