zoukankan      html  css  js  c++  java
  • New Year and Old Property 位运算

    New Year and Old Property

    The year 2015 is almost over.

    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.

    Example

    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 = 1012610 = 1102710 = 1112,810 = 10002910 = 10012 and 1010 = 10102. Two of them (1012 and 1102) have the described property.

    题意是给定一个区间,求在这区间内满足二进制只有一个0的数。由于是二进制,自如而然我就想到了位运算,推了下,其实每个满足二进制只有一个0的数

    可以表示为:(1<<i) - 1 - (1<<j) {j < i-1}。

    一开始是用c++写的,不过c++的位运算只是整数型,1<<60就会出错,而这里是长整数型,可能还有其它方法进行长整数型的位运算,可惜我菜菜的,就只能用python写了。

    不多说。见代码!!!

    1 a,b = map(int,raw_input().strip().split())
    2 ans = 0
    3 for i in range(2,63):
    4     for j in range(0,i-1):
    5         x = (1 << i) - 1 - (1 << j)
    6         if(a <= x and x <= b):
    7             ans += 1
    8 print(ans)

     -------------------更新线-----------------------------

    c++可以进行长整数的位运算,只是1变成了1LL.

    ----------------------------------------------------------

  • 相关阅读:
    随机id
    vue关于父组件调用子组件的方法
    ES6——块级作用域
    在vue中引用superMap
    MSSQL备份脚本
    Ubuntu 使用命令导出数据库
    CSS before 中文乱码
    CentOS7 安装linux 网络不联通问题
    SVN update failed问题解决
    JQ实现树形菜单点击高亮
  • 原文地址:https://www.cnblogs.com/xingkongyihao/p/6719644.html
Copyright © 2011-2022 走看看