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;
    }
    
  • 相关阅读:
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    你会卖掉自己的网上信息吗?大数据可能根本不属于你
    机器学习——TensorFLow实战房价预测
    数据库运作实践三三之歌(秘制口诀)
    1000行MySQL学习笔记,收藏版!
    吐血整理深度学习入门路线及导航【教学视频+大神博客+书籍整理】+【资源页】(2019年已经最后一个月了,你还不学深度学习吗???)
    Ubuntu Snap 简述
    参数传递
  • 原文地址:https://www.cnblogs.com/YDDDD/p/12019182.html
Copyright © 2011-2022 走看看