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.

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

题意:

给出区间(a, b)求区间中转化为二进制后恰好有一个0的数的个数;

方法1:(模拟)这是我最先想到的方法,然而代码比较复杂,还错了好多发,果然我还是一个小渣渣;

代码:

 #include <bits/stdc++.h>
#define ll long long
#define MAXN 100
using namespace std; int get_erjinzhi(ll n, int a[]) //*****将n转化为二进制存储到a数组中
{
int k=, xx[MAXN];
while(n)
{
xx[k++]=n%;
n/=;
}
for(int i=k-, j=; i>=; i--, j++)
a[j]=xx[i];
return k;
} int cmp(int a[], int yy[], int cnt) //*****比较a,yy的大小
{
for(int i=; i<cnt; i++)
{
if(a[i]>yy[i]) return ;
if(a[i]<yy[i]) return ;
}
return ;
} int main(void)
{
ll x, y, ans=;
int a[MAXN], b[MAXN], aa[MAXN], bb[MAXN];
cin >> x >> y;
if(x==&&y==)
{
cout << "" << endl;
return ;
}
if(x==) x++;
int cnt1=get_erjinzhi(x, a);
int cnt2=get_erjinzhi(y, b);
for(int i=; i<cnt1; i++)
{
aa[i]=;
}
aa[]=;
if(cmp(aa, a, cnt1))
{
if(cnt1==cnt2)
{
if(cmp(b, aa, cnt1))
ans++;
}
else ans++;
}
for(int i=; i+<cnt1; i++)
{
swap(aa[i], aa[i+]);
if(cmp(aa, a, cnt1))
{
if(cnt1==cnt2)
{
if(cmp(b, aa, cnt1)) ans++;
}
else ans++;
}
}
for(int i=; i<cnt2; i++)
{
bb[i]=;
}
bb[]=;
int k=;
if(cnt1!=cnt2)
{
while(cmp(b, bb, cnt2)&&k<cnt2)
{
ans++;
swap(bb[k], bb[k+]);
if(cmp(b, bb, cnt2)==)
{
ans++;
break;
}
k++;
}
}
for(int i=cnt1+; i<cnt2; i++)
{
ans+=i-;
}
cout << ans << endl;
return ;
}

方法2:(二进制枚举,看了别人代码才想到的,再一次证明了我是个渣渣)

思路:

二进制运算的姿势一定要摆对,不然得做好久…
举个例子:
10000-1=1111
1111-1=1110
1111-10=1101
1111-100=1011

代码:

 #include <bits/stdc++.h>
#define ll long long
using namespace std; int main(void)
{
ll a, b, ans=;
cin >> a >> b;
for(int i=; i<=; i++)
{
for(int j=; j<i-; j++)
{
ll temp=(1ll<<i)--(1ll<<j);
if(temp>=a && temp<=b) ans++;
}
}
cout << ans << endl;
return ;
}

方法3:(dfs)

 #include <bits/stdc++.h>
#define ll long long
using namespace std; ll ans=, a, b; void dfs(ll x, ll flag)
{
if(x>b) return;
if(x>=a&&x<=b&&flag) ans++;
if(flag==) dfs(x*, );
dfs(x*+, flag);
} int main(void)
{
cin >> a >> b;
dfs(, );
cout << ans << endl;
return ;
}

Good Bye 2015B(模拟或者二进制枚举)的更多相关文章

  1. CUGBACM_Summer_Tranning1 二进制枚举+模拟+离散化

    整体感觉:这个组队赛收获还挺多的.自从期末考试以后已经有一个多月没有 做过组队赛了吧,可是这暑假第一次组队赛就找回了曾经的感觉.还挺不错的!继续努力!! 改进的地方:这次组队赛開始的时候题目比較难读懂 ...

  2. UVA 1151二进制枚举子集 + 最小生成树

    题意:平面上有n个点(1<=N<=1000),你的任务是让所有n个点连通,为此, 你可以新建一些边,费用等于两个端点的欧几里得距离的平方.另外还有q(0<=q<=8)个套餐(数 ...

  3. Poj(2784),二进制枚举最小生成树

    题目链接:http://poj.org/problem?id=2784 Buy or Build Time Limit: 2000MS   Memory Limit: 65536K Total Sub ...

  4. POJ 2436 二进制枚举+位运算

    题意:给出n头牛的得病的种类情况,一共有m种病,要求找出最多有K种病的牛的数目: 思路:二进制枚举(得病处为1,否则为0,比如得了2 1两种病,代号就是011(十进制就是3)),首先枚举出1的个数等于 ...

  5. hdu 3118(二进制枚举)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3118 思路:题目要求是去掉最少的边使得图中不存在路径长度为奇数的环,这个问题等价于在图中去掉若干条边, ...

  6. HDU 5025Saving Tang Monk BFS + 二进制枚举状态

    3A的题目,第一次TLE,是因为一次BFS起点到终点状态太多爆掉了时间. 第二次WA,是因为没有枚举蛇的状态. 解体思路: 因为蛇的数目是小于5只的,那就首先枚举是否杀死每只蛇即可. 然后多次BFS, ...

  7. 南阳OJ-91-阶乘之和---二进制枚举(入门)

    题目链接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=91 题目大意: 给你一个非负数整数n,判断n是不是一些数(这些数不允许重复使用,且为 ...

  8. 关于二进制枚举-计蒜客-得到整数X

    某君有 n个互不相同的正整数,现在他要从这 n 个正整数之中无重复地选取任意个数,并仅通过加法凑出整数 X.求某君有多少种不同的方案来凑出整数 X. 输入格式 第一行,输入两个整数 n,X(1≤n≤2 ...

  9. BZOJ1688|二进制枚举子集| 状态压缩DP

    Disease Manangement 疾病管理 Description Alas! A set of D (1 <= D <= 15) diseases (numbered 1..D) ...

随机推荐

  1. X.Org可能将失去它的域名x.org

    X.Org基金会面临失去它的一个重要资产:单字母域名x.org.过去半年里,围绕着x.org域名的所有权问题,X.Org基金会董事会尝试私下解 决,但未能如愿.域名将于1月19日过期,只剩下不到两周的 ...

  2. JavaScript break跳出多重循环

    多重循环在编程中会经常遇到,那么在JavaScript中如何指定跳出那层的循环呢.其实这也是break的一个用法,下面是一个不错的例子,来自<JavaScript权威指南>,可以参考下: ...

  3. 2016年10月27日--css样式表

    CSS样式表 样式表分类 1.内联样式表 和html联合显示,控制精确,但是可重用性差,冗余多. !doctype html> <html> <head> <met ...

  4. 2016年10月24日--HTML常用标签

    body的属性: bgcolor                页面背景色 background             背景壁纸.图片 text                     文字颜色 t ...

  5. Intellij IDEA使用总结

    查询快捷键CTRL+N   查找类CTRL+SHIFT+N  查找文件CTRL+SHIFT+ALT+N 查 找类中的方法或变量CIRL+B   找变量的来源CTRL+ALT+B  找所有的子类CTRL ...

  6. 教程和工具--用wxPython编写GUI程序的

    wxPython是个很好的GUI库,对底层的C++库进行了封装,调用起来很方便,尤其是操作前台UI界面和后台多线程,两者配合很方便,做GUI程序最难是写界面尤其是布局. 关于wxPython,自己正在 ...

  7. 【GoLang】GoLang 中 make 与 new的区别

    make.new操作 make用于内建类型(map.slice 和channel)的内存分配.new用于各种类型的内存分配. 内建函数new本质上说跟其它语言中的同名函数功能一样:new(T)分配了零 ...

  8. POJ 2352

    ---恢复内容开始--- http://poj.org/problem?id=2352 这是一个树状数组的题目,也是我第一次接触这类的题目,也正是因为之前的一道题,TLE了,超时太严重了,我看disc ...

  9. hdu3709

    枚举+数位dp 注意处理数字为0和1的情况. #include <cstdio> #include <cstring> using namespace std; #define ...

  10. PHP生命周期

    2015-08-19 15:05:30 周三 一篇很好的文章 PHP内核探索 总结一下 1. 模块初始化 MINIT 各个PHP模块/扩展初始化内部变量, 告诉PHP调用自己的函数时, 函数体在哪里( ...