zoukankan      html  css  js  c++  java
  • 18113 Secret Book of Kungfu 按位DFS

    http://acm.scau.edu.cn:8000/uoj/mainMenu.html

    18113 Secret Book of Kungfu

    该题有题解

    时间限制:1000MS  内存限制:65535K
    提交次数:0 通过次数:0

    题型: 编程题   语言: 不限定

     

    Description

        Uncle Big is acknowledged as a master of kung fu. But he doesn't think so. He thinks that there are many other unknown 
    experts in the world. So Uncle Big devotes all his life to find the highest realm of kung fu.
        During a chance, Uncle Big discover a secret book of kungfu in a cave. But the book is broken and losts many pages. What's 
    worst, there's only some numbers writted on the book. After studying the book years and years, Uncle Big discover that all 
    this numbers have a common feature that, the decimal notation of a number is a substring of its binary notation. So Uncle 
    Big want to restore the book, in order to better understand it. Before doing this, Uncle Big has to know how many such numbers 
    between l and r (inclusive). 
        But now Uncle Big is so exciting because of this discovering, and has gone to race boat. Can you help him?
    




    输入格式

        The input file begins with an integer T (T <= 10000) in one row indicating the number of test case. Then T test cases 
    follows.
        Each test case contains one line with two non-negetive integer l and r (l <= r <= 1000000000000000(1e15) ).
    
    



    输出格式

        For each test case, print one line with a integer as answer.



     

    输入样例

    1
    1 1000



     

    输出样例

    8



     

    提示

    "1", "0", "10", "01", "101" are the substring of "101", but "11", "00" are not.

    考虑按位DFS后再暴力判断,然后发现总数只有283个,记得加上0,就284个。打个表二分查找就好。

    dfs数字的话,一般就是dfs(cur * 10 + 0),这个数位进位后,然后加上想要的数字

    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #define IOS ios::sync_with_stdio(false)
    using namespace std;
    #define inf (0x3f3f3f3f)
    typedef long long int LL;
    
    #include <iostream>
    #include <sstream>
    #include <vector>
    #include <set>
    #include <map>
    #include <queue>
    #include <string>
    LL L, R;
    int ans;
    char str_bin[222];
    char num[222];
    int lenstr;
    void bin(LL n) {
        if (n / 2) bin(n / 2);
        str_bin[++lenstr] = n % 2 + '0';
    }
    LL biao[500 + 20];
    int lenbiao;
    void dfs(LL cur) {
        if (cur >= L && cur <= R) {
            lenstr = 0;
            bin(cur);
            int k = 0;
            LL t = cur;
            while (t / 10 > 0) {
                num[++k] = t % 10 + '0';
                t /= 10;
            }
            num[++k] = t + '0';
            num[k + 1] = '';
            str_bin[lenstr + 1] = '';
    //         cout << str_bin + 1 << endl;
    
            reverse(num + 1, num + 1 + k);
    //        cout << num + 1 << endl;
    //        cout << endl;
            char *p = strstr(str_bin + 1, num + 1);
            if (p != NULL)
                biao[++lenbiao] = cur;
        }
        if (cur > R) return;
        dfs(cur * 10);
        dfs(cur * 10 + 1);
    }
    void work() {
        cin >> L >> R;
        if (L > R) swap(L, R);
        int posR = lower_bound(biao + 1, biao + 1 + lenbiao, R) - biao;
        int posL = lower_bound(biao + 1, biao + 1 + lenbiao, L) - biao;
        if (biao[posL] == L && biao[posR] == R) {
            cout << posR - posL + 1 << endl;
        } else if (biao[posL] == L && biao[posR] != R) {
            cout << posR - posL << endl;
        } else if (biao[posL] != L && biao[posR] == R) {
            cout << posR - posL + 1 << endl;
        } else {
            cout << posR - posL << endl;
        }
    }
    void init() {
        biao[++lenbiao] = 0;
        L = 0;
        R = 1e15L;
        dfs(1);
        sort(biao + 1, biao + 1 + lenbiao);
    }
    int main() {
    #ifdef local
        freopen("data.txt","r",stdin);
    #endif
        IOS;
        init();
        int t;
        cin >> t;
        while (t--) work();
        return 0;
    }
    View Code
  • 相关阅读:
    5G网络逐渐普及TSINGSEE青犀视频云边端架构网页视频实时互动直播系统又将如何发展?
    【开发记录】TSINGSEE青犀视频云边端架构Visual Studio 2017自建WebRTC中peerconnection_client编译报无法解析错误
    安防视频云服务平台EasyCVR视频智能分析系统运行控制台报404错误如何排查?
    一对一或一对多音视频通话会议系统可以通过哪些方式实现?
    TSINGSEE青犀视频云边端视频智能分析平台开发VMware下安装Ubuntu系统后无法安装VMwaretools问题解决
    最简单的Windows套接字(Socket)例子(源码,实例)
    KJAVA虚拟机Hack笔记实现MIDP的SLAVE事件模型
    系统程序员成长计划你的数据放在哪里(下)
    使用new实现realloc操作
    KJava虚拟机hack笔记基于GTK的移植
  • 原文地址:https://www.cnblogs.com/liuweimingcprogram/p/6052154.html
Copyright © 2011-2022 走看看