CodeForces 611B New Year and Old Property【DFS&&模拟位运算】

来源:互联网 发布:淘宝与京东 编辑:程序博客网 时间:2024/05/29 19:39

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
0
input
72057594000000000 72057595000000000
output
26
Note

In the first sample Limak's interval contains numbers 510 = 1012610 = 1102710 = 1112810 = 10002910 = 10012 and1010 = 10102. Two of them (1012 and 1102) have the described property.


开始做时,没弄清题意,写的好麻烦也没弄出来,搜的题解模拟位运算和dfs


#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;__int64 a,b,sum;int rec[70];__int64 solve(int n){    __int64 s=0;    for(int i=1;i<=n;++i)        s+=(__int64)rec[i]<<(i-1);    return s;}int main(){    while(~scanf("%I64d%I64d",&a,&b))    {        for(int i=1;i<=64;++i)            rec[i]=1;        for(int i=2;i<63;++i)        {            for(int j=1;j<i;++j)            {                rec[j]=0;                __int64 ans=solve(i);                rec[j]=1;                if(ans>=a&&ans<=b)                    sum++;            }        }        printf("%I64d\n",sum);    }    return 0;}

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;__int64 a,b,sum;void dfs(__int64 n,int sign){    if(n>b)        return ;    if(n>=a&&n<=b&&sign)        sum++;    if(!sign)        dfs(n*2,1);    dfs(n*2+1,sign);}int main(){    while(~scanf("%I64d%I64d",&a,&b))    {        sum=0;        dfs(1,0);        printf("%I64d\n",sum);    }    return 0;}


0 0
原创粉丝点击