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;
    }
    
  • 相关阅读:
    CentOS 7 搭建 LAMP
    CentOS 7 安装 nginx
    ms-sql关联表操作
    在CentOS下自动备份mysql
    Redhat 7 或者 CentOS 7 密码破解
    java环境变量的设置
    CentOS6.6安装及配置vsftpd文件服务器
    Virtualbox虚拟机配置CentOS7.0静态网络
    CentOS6.6安装vmware workstation报错
    CentOS6.6安装virtualbox4.1.44
  • 原文地址:https://www.cnblogs.com/YDDDD/p/12019182.html
Copyright © 2011-2022 走看看