链接:

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. C++ 制作一个“测运”小游戏-rand()函数的应用

    游戏说明: 游戏名:Lucky Guy 玩法说明:有2种模式可以选择,一种是一直选择数字,直到抽到炸弹为止.另一种是在0~9个数字中进行选择,有5个炸弹,最高分为5,抽到炸弹即游戏结束.游戏结束后,可 ...

  2. Android--Google Map API V2使用

    一.获取API Key 1.先获取SHA-1 fingerprint 数字证书是有两种,一种是debug,还有release.前者只能用于测试:后者才可以用于实际产品. debug:在命令行中输入命令 ...

  3. MAC 添加Jmeter环境变量

    vim ./bash_profile JMETER_HOME=/Users/finup/apache-jmeter-5.1.1 CLASSPATH=$JAVA_HOME/lib/tools.jar:$ ...

  4. windows 查看端口占用以及解决办法

    windows 下查看所有端口程序1 netstat -ano 查看所有的端口占用情况2 netstat -ano|findstr "443" 查看端口为443的程序占用情况3 t ...

  5. ActiveX的AssemblyInof.cs文件 IObjectSafety  接口

    ActiveX的AssemblyInof.cs文件 IObjectSafety  接口 [Guid("D4176A17-2A33-4903-8F37-9EBDD7CAFFD3"), ...

  6. 1 集群状态、增删改查、全量替换、强制创建、设置单个index的分片数副本数

    检查集群健康状态,可以看集群颜色.(黄色:primary shard都正常,replica不正常) GET /_cat/health?v 列出集群所有index GET /_cat/indices?v ...

  7. linux ubuntu-16.04-配置java1.8和Tomcat8

    前言 第一次使用linux ubuntu16.04 服务器,所以做一下常用配置的记录. JDK 1.创建存放jdk的目录 一般在usr/local下创建一个java文件夹 cd /usr/local ...

  8. C++线程同步之事件(生产者与消费者问题)

    #include <windows.h> #include <stdio.h> HANDLE g_hSet = NULL; HANDLE g_hClear = NULL; HA ...

  9. [转]关于ORA-00979 不是 GROUP BY 表达式错误的解释

    转自:https://www.cnblogs.com/vigarbuaa/archive/2012/06/25/2561225.html ORA-00979 不是 GROUP BY 表达式”这个错误, ...

  10. FreeRTOS中断测试

    configMAX_SYSCALL_INTERRUPT_PRIORITY 高于此优先级的中断,不能被禁止 #ifdef __NVIC_PRIO_BITS #define configPRIO_BITS ...