题目链接:

  http://www.lightoj.com/volume_showproblem.php?problem=1170

题目描述:
  给出一些满足完美性质的一列数(x > 1 and y > 1 such that m = xy.) 然后给出一个区间,问在这个区间中的完美数组成的搜索二叉树的个数是多少?
解题思路:

  1,打标算出所有的完美数列中的数字

  2,打表算出卡特兰数列,等着以后用

  3,卡特兰数列递推式:F[N] = F[N-1] * ( 4 * N - 2 ) / ( N + 1 ), 求余的时候牵涉到逆元,用扩展欧几里德或者费马小定理求解逆元

准备到这里就万事大吉了!

    卡特兰数应用

代码:

  

 #include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std; #define LL long long
#define maxn 110100
#define mod 100000007
const LL Max = 1e10;
LL a[maxn], ans[maxn], num; LL Extended_Euclid (LL a, LL b, LL &x, LL &y)
{
//处理 a * b > 0 的情况
if (b == )
{
x = ;
y = ;
return a;
} LL r = Extended_Euclid (b, a%b, x, y), t;
t = x;
x = y;
y = t - a / b * y;
return r;
} void init ()
{
//memset (vis, 0, sizeof(vis));
num = ;
for (LL i=; i<maxn; i++)
{
LL j = i * i;
while (j <= Max)
{
a[num ++] = j;
j *= i;
}
} sort (a, a+ num);
num = unique (a, a+num) - a; ans[] = ;
ans[] = ;
for (LL i=; i<maxn; i++)
{
/// F[N] = F[N-1] * ( 4 * N - 2 ) / ( N + 1 )
LL x, y, r;
r = Extended_Euclid (i+, mod, x, y);
ans[i] = ans[i-] * ( * i - ) % mod * (x % mod + mod ) % mod;
}
} int main ()
{
init (); int T, L = ;
cin >> T;
while (T --)
{
LL x, y;
scanf ("%lld %lld", &x, &y);
x = lower_bound (a, a+num, x) - a;
y = upper_bound (a, a+num, y) - a; printf ("Case %d: %lld\n", L++, ans[y - x]);
}
return ;
}

LightOj 1170 - Counting Perfect BST (折半枚举 + 卡特兰树)的更多相关文章

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

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

  2. 1170 - Counting Perfect BST

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

  3. light oj1170 - Counting Perfect BST卡特兰数

    1170 - Counting Perfect BST BST is the acronym for Binary Search Tree. A BST is a tree data structur ...

  4. LightOj 1076 - Get the Containers (折半枚举好题)

    题目链接: http://www.lightoj.com/volume_showproblem.php?problem=1076 题目描述: 给出n个数,要求分成m段,问这m段中最大的总和,最小是多少 ...

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

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

  6. LightOJ 1235 - Coin Change (IV) (折半枚举)

    题目链接: http://www.lightoj.com/volume_showproblem.php?problem=1235 题目描述: 给出n个硬币,每种硬币最多使用两次,问能否组成K面值? 解 ...

  7. Load Balancing 折半枚举大法好啊

    Load Balancing 给出每个学生的学分.   将学生按学分分成四组,使得sigma (sumi-n/4)最小.         算法:   折半枚举 #include <iostrea ...

  8. CSU OJ PID=1514: Packs 超大背包问题,折半枚举+二分查找。

    1514: Packs Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 61  Solved: 4[Submit][Status][Web Board] ...

  9. NYOJ 1091 超大01背包(折半枚举)

    这道题乍一看是普通的01背包,最最基础的,但是仔细一看数据,发现普通的根本没法做,仔细观察数组发现n比较小,利用这个特点将它划分为前半部分和后半部分这样就好了,当时在网上找题解,找不到,后来在挑战程序 ...

随机推荐

  1. linux以及git和maven常用命令

    maven常用命令: clean install -Dmaven.test.skip -Ptest-lx   (注意:test-lx是pom文件名) 其他 https://www.cnblogs.co ...

  2. feed流,图片在左还是右的区别是

    feed流设计:那些谋杀你时间APP | 人人都是产品经理 http://www.woshipm.com/pd/773523.html

  3. add time to file name

    add time to file name echo 123 > $(date +"%Y%m%d_%H%M%S").now; mv /mnt/mongodb_data/dat ...

  4. ping 和 远程桌面 与防火墙的关系

    现象: 初始化服务器时,人们为了方便,常常关闭防火墙,这样做很危险.我们经常遇到问题是,防火墙一旦打开,ping和远程桌面就会有问题. 有时ping不通,但能远程:但有时ping通,又远程不了:有时都 ...

  5. [Selenium] 操作页面元素等待时间

    WebDriver 在操作页面元素等待时间时,提供2种等待方式:一个为显式等待,一个为隐式等待,其区别在于: 1)显式等待:明确地告诉 WebDriver 按照特定的条件进行等待,条件未达到就一直等待 ...

  6. [yii2]urlmanger

    首先配置下nginx,确保可以不使用index.php来访问 server{ listen 8082; server_name yii2.dev; access_log logs/yii2.acces ...

  7. 指针 * &

    int main() { ; //定义int变量updates int * p_updates; //定义指针p_updates p_updates=&updates;//将updates的地 ...

  8. Python 赋值、浅拷贝和深拷贝

    初学Python,和C++还是有许多不同.直接赋值.浅拷贝和深拷贝,这三种拷贝对象的操作之间还是有许多的区别.Python语言的版本为2.7,在Pycharm中进行实验. 一.直接赋值 用下面的代码来 ...

  9. A - Mike and Fax

    Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Description While ...

  10. DELL R730服务器配置RAID及安装服务器系统 以及域的控制

    https://wenku.baidu.com/view/ad45d85a9ec3d5bbfd0a74d9.html