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. 浅析for in 和for的区别

    区别一: for in是javascript 1.0 中发布的. for each in是作为E4X标准的一部分在javascript 1.6中发布的,而它不是ECMAScript标准的一部分. 这将 ...

  2. nginx反向代理、让代理节点记录客户端真实IP

    环境:根据http://www.cnblogs.com/zzzhfo/p/6032095.html环境配置 在web01或web02上查看用户访问日志 先客户端访问 [root@web_backup ...

  3. Redis学习笔记三:多机数据库的实现

    1.复制 执行slaveof命令或者设置slaveof选项,让一个服务器去复制另外一个服务器. 旧版复制功能的实现(Redis 2.8 之前的版本) 复制功能分为同步和命令传播两个操作. 同步(syn ...

  4. Android应用性能优化

    整理自http://androidperformance.com的几篇博客 代码内存优化-Java篇 避免创建不必须的对象,虽然GC可以回收不用的对象,但为对象分配内存和回收它们同样是需要消耗资源的. ...

  5. JavaScript深入浅出2-表达式和运算符

    慕课网教程视频地址:Javascript深入浅出 表达式是指能计算出值的任何可用程序单元 原始表达式:常量.直接量 3.14,“test” 关键字 null,this 变量 i,k,j 表达式含:原始 ...

  6. Android四种基本布局(LinearLayout \ RelativeLayout \ FrameLayout \ TableLayout)

    ------------------------------------------LinearLayout---------------------------------------------- ...

  7. am335x 虚拟机环境变量的设置及注释

    我用的还是老的Linux3.2.0的内核,只是借用了TI am335x SDK 3.0里面的工具. 首先下载TI官方的SDK,上一章已经安装了一个Ubuntu14.04的虚拟机. TI 最新的SDK下 ...

  8. super用法和继承中的构造方法

    当new出来一个对象的时候,  this是只想对象本身. 在存在继承关系时, 在子类中用super表示引用父类中的东西. 子类的构造过程必须调用父类的构造方法. 子类中包含父类,所以子类中一定要先调用 ...

  9. vb.net 控件(包括字体)随窗体按比例缩放

    Public Class frmDl Dim x As Single = 0 Dim y As Single = 0 Private Sub frmDl_Load(ByVal sender As Sy ...

  10. NDK学习三: 纯手工编译Hello World

      1.配置环境变量 添加make工具path环境变量: E:\Android\android-ndk-r10b\prebuilt\windows-x86_64\bin     2.编写Hello W ...