链接:

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. [转帖]B树索引、位图索引和散列索引

    B树索引.位图索引和散列索引   https://blog.csdn.net/huashanlunjian/article/details/84460436 索引在数据结构上可以分为三种B树索引.位图 ...

  2. [转帖]央行推出数字货币DCEP:基于区块链技术、将取代现钞

    央行推出数字货币DCEP:基于区块链技术.将取代现钞 天天快报的内容. 密码财经 2019-10-29 18:15 关注   前不久的10月23日,Facebook的首席执行官扎克伯格在美国国会听证会 ...

  3. Delphi 将视频 Base64 字符串转换为视频二进制文件

    var Bytes: TBytes; Stream: TBytesStream; begin with System.NetEncoding.TBase64Encoding.Create do try ...

  4. python学习-62 类属性的增 删 该 查

    类属性 1.类属性 类属性又称为静态变量,或者是静态数据.这些数据是与它们所属的类对象绑定的,不依赖于任何类实例. 2.增 删 改 查 class zoo: country = 'china' def ...

  5. tensorflow-简单的神经网络

    本次笔记是关于tensorflow1的代码,由于接触不久没有跟上2.0版本,这个代码是通过简单的神经网络做一个非线性回归任务,(如果用GPU版本的话第一次出错就重启) import tensorflo ...

  6. Unity的学习笔记(向量点乘和叉乘)

    public class AngleTest : MonoBehaviour { public GameObject cube; //主要的物体 public GameObject radCube; ...

  7. .net core使用ocelot---第八篇 Consul

    简介 .net core使用ocelot---第一篇 简单使用   .net core使用ocelot---第二篇 身份验证使用  .net core使用ocelot---第三篇 日志记录  .net ...

  8. kubernetes第二章--集群搭建

  9. npm 安装 react-devtools

    由于不能科学的上网.网上看资料装上了这个插件,装的过程有点坑.记录一下,希望能帮到和我一样的新手. 1.第一步,克隆下远程仓库的东西. 桌面右键,git-bash.然后输入: git clone ht ...

  10. 如何使用API的方式消费SAP Commerce Cloud的订单服务

    最近Jerry在做一个微信和SAP Commerce Cloud集成的项目,需要在微信里调用后者的Restful API进行订单创建和读取.以前Jerry对SAP Commerce Cloud知之甚少 ...