天天看點

CF Good Bye 2015 B- New Year and Old Property(CF611B)

http://codeforces.com/contest/611/problem/B

B. New Year and Old Property time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output

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.

Sample test(s) input

5 10
      

output

2
      

input

2015 2015
      

output

1
      

input

100 105
      

output

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 and 1010 = 10102. Two of them (1012 and 1102) have the described property.

題意:給你兩個數a,b,讓你算出來從a到b一共b-a+1個數中一共有幾個數的二進制中隻有一個0(比如5的二進制101,6的二進制110都是)。

思路:一開始想的比較複雜想要找到最小的大于等于a的二進制隻有一個零的數,然後不停的将0後移,如果0到了最後一個則進位,直到大于b為止,後來看了别人的代碼,算了算時間複雜度,發現直接把所有的二進制隻有一位零的數周遊一遍也不會逾時間...

我的代碼:

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<queue>
#include<math.h>

using namespace std;

unsigned long long INF=0x8000000000000000;
//-9223372036854775808

void pr(long long n)
{
long long i,k,x[100];
for(i=0;n!=0;i++)// 判斷條件 n!=0
{
x[i]=n%2;
n=n/2;
}
for(k=i-1;k!=(-1);k--)// 判斷條件 k!=-1
printf("%d",x[k]);// 輸出x[k],不是x[i]
}

int main()
{
    long long a,b,temp;
    int ans=0;
    cin>>a>>b;
    int n=0,l,s;
    temp=a;

    while(1)
    {
        if(temp&INF)
        {
            s=n;
            break;
        }
        temp=temp<<1;
        n++;
    }
    temp=temp<<1;
    n++;
    while(1 && n<=64)
    {
        if(!(temp&INF))
        {
            l=n;
            break;
        }
        temp=temp<<1;
        n++;
    }
    if(n<63)
    {
        temp=temp<<1;
        n++;
        while(n<64)
        {
            if(!(temp&INF))
            {
                a+=(1LL<<(63-n));
            }
            temp=temp<<1;
            n++;
        }
    }
    
    else if(n==64)
    {
        a+=(1LL<<(64-s));
        a-=(1LL<<(63-s));
        l=s;
        s--;
    }

    while(1)
    {
        if(a<=b)
        {
            ans++;
        }
        else if(a>b) break;

        if(l<63)
        {
            a+=(1LL<<(63-l));
            l++;
            a-=(1LL<<(63-l));
        }
        else if(l<=63)
        {
            a++;
            a+=(1LL<<(64-s));
            a-=(1LL<<(63-s));
            l=s;
            s--;
        }
    }
    cout<<ans<<endl;
    return 0;
}
           

别人的代碼:

#include <iostream>
#include <cstdio>
#include <queue>
#include <stack>
#include <map>

using namespace std;

long long a,b,rs,x;
int main(){
    cin >> a >> b;
    for(int i = 1; i < 63; i++)
        for(int j = 0; j < i-1; j++)
        {
            x = ((1LL << i) - 1 - (1LL << j));
            if(a <= x && x <=b ) rs++;
        }
    cout<<rs;
    return 0;
}