• light oj1170


    1170 - Counting Perfect BST

    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.

    Input

    Input starts with an integer T (≤ 20000), denoting the number of test cases.

    Each case of input contains two integers: a and b (1 ≤ a ≤ b ≤ 1010, b - a ≤ 106) as defined in the problem statement.

    Output

    For each case, print the case number and the total number of distinct BSTs that can be formed by the perfect powers between a and b. Output the result modulo 100000007.

    Sample Input

    Output for Sample Input

    4

    1 4

    5 10

    1 10

    1 3

    Case 1: 1

    Case 2: 2

    Case 3: 5

    Case 4: 0

    分析:先筛选出a, b间的完美幂,然后就是求卡特兰数了,既可以用递推,也可以用求逆元的方法。

    代码:

    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<set>
    #include<iostream>
    #include<algorithm>
    #include<queue>
    #include<stack>
    #define N 111111
    #define mod 100000007
    typedef long long ll;
    using namespace std;

    ll num[N];
    ll d[1111];
    int k;

    void init()
    {
    for(ll i = 2; i * i < 10000000000L; ++i)
    {
    for(int j = 2; ; ++j)
    {
    if(pow(i, j) > 10000000000L)
    break;

    num[k++] = pow(i, j);
    }
    }
    sort(num, num + k);

    k = unique(num, num + k) - num;
    /*在STL中unique函数是一个去重函数, unique的功能是去除相邻的重复元素(只保留一个),
    其实它并不真正把重复的元素删除,是把重复的元素移到后面去了,然后依然保存到了原数组中,
    然后 返回去重后最后一个元素的地址,因为unique去除的是相邻的重复元素,所以一般用之前都会要排一下序。
    */
    memset(d, 0, sizeof(d));
    d[0] = 1;
    d[1] = 1;

    for(int i = 2; i < 1111; i++)
    {
    for(int j = 1; j <= i; j++)
    {
    d[i] += d[i - j] * d[j-1];
    d[i] %= mod;
    }
    /*Catalan数的定义

     令h(0)=1,h(1)=1,Catalan数满足递归式:h(n) = h(0)*h(n-1) + h(1)*h(n-2) + ... + h(n-1)*h(0) (n>=2)

     该递推关系的解为:h(n) = C(2n,n)/(n+1),n=0,1,2,3,... (其中C(2n,n)表示2n个物品中取n个的组合数)
    */
    ///卡特兰数应用链接:http://www.cnblogs.com/yaoyueduzhen/p/5456490.html

    }
    }
    int getcnt(ll a, ll b)
    {
    int t1 = upper_bound(num, num+k, b) - num;///返回一个非递减序列中的第一个大于val的位置
    int t2 = lower_bound(num, num+k, a) - num;///返回一个非递减序列中的第一个大于等于值val的位置
    return t1 - t2;
    }
    int main(void)
    {
    int T, cas;

    ll a, b;
    scanf("%d", &T);

    cas = 0;
    init();

    d[0] = 0;
    while(T--)
    {

    scanf("%lld%lld", &a, &b);
    cas++;

    printf("Case %d: %lld ", cas, d[getcnt(a, b)]);
    }
    }

  • 相关阅读:
    给定一个字符串,打印输出有重复的字符和重复的次数,并且按照重复的次数升序输出
    Failed to bind NettyServer on /10.254.4.57:20880, cause: Failed to bind to: /0.0.0.0:20880 配置dubbo遇到的问题
    Feign远程调用,调用方法有返回值,没有返回原方法,Canal监听数据库发生的异常:end of stream when reading header,异常中还有“你的主机中的软件中止了一个已建立的连接”等信息
    idea格式化代码把方法上的文字注释换行的问题
    使用Docker报的错误 docker WARNING: IPv4 forwarding is disabled. Networking will not work
    11月新的开始
    二叉树学习总结
    leetcode刷题日志(200913)637.二叉树的层平均值
    leetcode刷题日志(200909)1512.好数对的数目
    leetcode刷题日志(200908)1480.一维数组动态和
  • 原文地址:https://www.cnblogs.com/dll6/p/7212056.html
Copyright © 2020-2023  润新知