zoukankan      html  css  js  c++  java
  • New Year and Old Property :dfs

    题目描述:

    Limak is a little polar bear. He has recently learnt about the binary system. He noticed that the passing year has exactly one zero in its representation in the binary system — 201510 = 111110111112. Note that he doesn't care about the number of zeros in the decimal representation.

    Limak chose some interval of years. He is going to count all years from this interval that have exactly one zero in the binary representation. Can you do it faster?

    Assume that all positive integers are always written without leading zeros.

    Input

    The only line of the input contains two integers a and b (1 ≤ a ≤ b ≤ 1018) — the first year and the last year in Limak's interval respectively.

    Output

    Print one integer – the number of years Limak will count in his chosen interval.

    Sample test(s)

    input
    5 10
    output
    2
    input
    2015 2015
    output
    1
    input
    100 105
    output
    0
    input
    72057594000000000 72057595000000000
    output
    26

    Note

    In the first sample Limak's interval contains numbers 510 = 1012, 610 = 1102, 710 = 1112, 810 = 10002, 910 = 10012 and1010 = 10102. Two of them (1012 and 1102) have the described property.

    题目大意:求a到b中包含的所有的整数二进制只有一个1的数的个数

    #include <bits/stdc++.h>
    using namespace std;
    long long num=0,a,b;
    void dfs(long long x,int y)//y代表x的二进制中有几个零
    {
     //   cout<<"x:"<<x<<"
    ";
        if(x>b)return;//如果超出范围,就返回
        else if(x>=a&&y==1)//如果在范围里 并且只有一个零 那个数就加一
          {
            num++;
          }
        if(y==0)dfs(x<<1,1);
        //如果是二进制里没有零的话(例:1111111),那么左移一位,最后一位就是零了
        //所以就直接dfs左移,然后y是1
        dfs((x<<1)+1,y);
        //除了零在最后还有 零在中间的
        //所以在上边基础上再加一  (例:10,左移一位加一:101)
    }
     
    int main(){
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
        cin>>a>>b;
        dfs(1,0);
        cout<<num<<endl;
        return 0;
    }
    

      

  • 相关阅读:
    浅谈工作流的作用
    WinForm上播放Flash文件
    C#反序列化 “在分析完成之前就遇到流结尾”
    UML类图详解
    关于C#中Thread.Join()的一点理解
    c# 反射的用法
    C#多线程学习笔记之(abort与join配合使用)
    UML用例图总结
    asp.net 发布到IIS中出现”处理程序“PageHandlerFactoryIntegrated”在其模块列表中有一个错误模块“ManagedPipelineHandler”“错误的解决方法
    C#序列化和反序列化
  • 原文地址:https://www.cnblogs.com/52dxer/p/10407192.html
Copyright © 2011-2022 走看看