zoukankan      html  css  js  c++  java
  • LightOJ

    链接:

    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
    ", Ans[rr-rl]);
        }
    
        return 0;
    }
    
  • 相关阅读:
    pyCharm报错"your evaluation license has expired, pycharm will now exit"解决方法(实测)
    (转)python selenium-webdriver 元素操作之鼠标和键盘事件
    转载:PICT使用教程(设计测试用例工具)
    转载:小米电视怎么安装爱奇艺
    关于VMware 15搭建MacOS 10.14后无法播放在线视频和客户端视频的问题
    Reference resources
    centos7 启用iptables
    disabling IPv6 name/address support: Address family not supported by protocol
    rngd.service 启动失败的处理
    mdadm Raid5 /dev/md0 lost a disk and recovery from another machine
  • 原文地址:https://www.cnblogs.com/YDDDD/p/12019182.html
Copyright © 2011-2022 走看看