• 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
  • 相关阅读:
    Vpython简单例子
    我在读的书:《ACM图灵奖:19662006(第三版)计算机发展史的缩影》
    可惜啊,没见到姚期智~~
    The Sounds of Music 观后感
    终于在博客园申请开通博客了
    【引用】Python open读写文件实现脚本
    在Python中使用中文
    Discovery:深入理解计算机系统 (Ver.2) 中文版
    [导入]一个都不能少:全面认识IE插件
    [导入]午间心情
  • 原文地址:https://www.cnblogs.com/liuweimingcprogram/p/6052154.html
Copyright © 2020-2023  润新知