链接:

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. Java线程的等待与唤醒完整示例代码

    项目结构: 资源类: 输入线程:  输出线程: 测试: 人妖问题发生: 线程安全问题的解决方法: 调用Object的wait()和notify()方法时需注意:必须是锁对象方可调用,否则将抛出无效的监 ...

  2. NEST 多IndexType与分页

    /// <summary> /// POST /_all/employee/_search?typed_keys=true /// </summary> public void ...

  3. 【洛谷 P2597】 [ZJOI2012]灾难(LCA)

    题目链接 考虑建一棵树,使一个生物灭绝时他的子树都会灭绝,显然这样答案就是以每个点为根的子树大小-1. 为什么原图不是一棵树,因为一个生物可能会以多个生物为食,所以按拓扑序来建树,把每个遍历到的点的父 ...

  4. Coldfusion Sql查询分组输出

    <cfoutput query="myQry" group="date"> #date# <cfoutput> #detail# < ...

  5. C++线程同步之临界区

    #include <iostream> #include <windows.h> using namespace std; CRITICAL_SECTION cs; // Lo ...

  6. CRM, C4C和SAP Hybris的数据库层设计

    SAP的product都是DB provider无关的. CRM大家都很熟悉了,application developer最多用Open SQL直接操作表. Netweaver里支持的DB provi ...

  7. 【hadoop】MapReduce分布式计算框架原理

    PS:实操部分就省略了哈,准备最近好好看下理论这块,其实我是比较懒得哈!!! <?>MapReduce的概述 MapReduce是一种计算模型,进行大数据量的离线计算.MapReduce实 ...

  8. 解决spring boot1.5以上版本@ConfigurationProperties提示“Spring Boot Configuration Annotation Processor not.."

    Springboot1.5以上版本,在使用 @ConfigurationProperties注解的时候会提示“Spring Boot Configuration Annotation Processo ...

  9. git如何删除已经提交的文件夹

    在上传项目到github时,忘记忽略了某个文件夹.idea,就直接push上去了, 最后意识到了此问题,决定删除掉远程仓库中的.idea文件夹 删除前: 删除后: 在github上只能删除仓库,却无法 ...

  10. Python如何打印文字对应的索引

    用python编写一个简单的小程序:将文字对应的索引打印出来. test=input('>>>') print(test) l=len(test) print(l) r=range( ...