Good Bye 2015B(模拟或者二进制枚举)
2 seconds
256 megabytes
standard input
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.
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.
Print one integer – the number of years Limak will count in his chosen interval.
5 10
2
2015 2015
1
100 105
0
72057594000000000 72057595000000000
26
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(模拟或者二进制枚举)的更多相关文章
- CUGBACM_Summer_Tranning1 二进制枚举+模拟+离散化
整体感觉:这个组队赛收获还挺多的.自从期末考试以后已经有一个多月没有 做过组队赛了吧,可是这暑假第一次组队赛就找回了曾经的感觉.还挺不错的!继续努力!! 改进的地方:这次组队赛開始的时候题目比較难读懂 ...
- UVA 1151二进制枚举子集 + 最小生成树
题意:平面上有n个点(1<=N<=1000),你的任务是让所有n个点连通,为此, 你可以新建一些边,费用等于两个端点的欧几里得距离的平方.另外还有q(0<=q<=8)个套餐(数 ...
- Poj(2784),二进制枚举最小生成树
题目链接:http://poj.org/problem?id=2784 Buy or Build Time Limit: 2000MS Memory Limit: 65536K Total Sub ...
- POJ 2436 二进制枚举+位运算
题意:给出n头牛的得病的种类情况,一共有m种病,要求找出最多有K种病的牛的数目: 思路:二进制枚举(得病处为1,否则为0,比如得了2 1两种病,代号就是011(十进制就是3)),首先枚举出1的个数等于 ...
- hdu 3118(二进制枚举)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3118 思路:题目要求是去掉最少的边使得图中不存在路径长度为奇数的环,这个问题等价于在图中去掉若干条边, ...
- HDU 5025Saving Tang Monk BFS + 二进制枚举状态
3A的题目,第一次TLE,是因为一次BFS起点到终点状态太多爆掉了时间. 第二次WA,是因为没有枚举蛇的状态. 解体思路: 因为蛇的数目是小于5只的,那就首先枚举是否杀死每只蛇即可. 然后多次BFS, ...
- 南阳OJ-91-阶乘之和---二进制枚举(入门)
题目链接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=91 题目大意: 给你一个非负数整数n,判断n是不是一些数(这些数不允许重复使用,且为 ...
- 关于二进制枚举-计蒜客-得到整数X
某君有 n个互不相同的正整数,现在他要从这 n 个正整数之中无重复地选取任意个数,并仅通过加法凑出整数 X.求某君有多少种不同的方案来凑出整数 X. 输入格式 第一行,输入两个整数 n,X(1≤n≤2 ...
- BZOJ1688|二进制枚举子集| 状态压缩DP
Disease Manangement 疾病管理 Description Alas! A set of D (1 <= D <= 15) diseases (numbered 1..D) ...
随机推荐
- javascript高级程序设计---Event对象二
鼠标事件 事件种类 鼠标事件指与鼠标相关的事件,主要有以下一些. (1)click事件 click事件当用户在Element节点.document节点.window对象上,单击鼠标(或者按下回车键)时 ...
- 题目1373:整数中1出现的次数(从1到n整数中1出现的次数)
题目1373:整数中1出现的次数(从1到n整数中1出现的次数) 题目描述: 亲们!!我们的外国友人YZ这几天总是睡不好,初中奥数里有一个题目一直困扰着他,特此他向JOBDU发来求助信,希望亲们能帮帮他 ...
- js字符串与16进制互相转换
// \x65\x76\x61\x6c是否启用\x加密 <script type="text/javascript"> function JavaDe() { var ...
- NFS工作原理及配置文件详解
nfs工作原理流程 如上图所示,当访问程序通过NFS客户端向NFS服务端存取文件时,其请求数据流程如下几点: 1.首先用户访问网站程序,由程序在NFS客户端上发出NFS文件存取功能 ...
- MongoDB 基础知识
一. 基础知识 1. MongoDB是一个文档型的数据库,文档就是一个键值对的有序集合. 例如这样:{"greeting":"hello world"} 2. ...
- UIView的layoutSubviews和drawRect
原文: UIView的layoutSubviews和drawRect UIView的setNeedsDisplay和setNeedsLayout方法.首先两个方法都是异步执行的.setNeedsDis ...
- Apache + Tomcat + mod_jk实现集群服务
Tomcat中的集群原理是通过组播的方式进行节点的查找并使用TCP连接进行会话的复制. 实现效果:用apache 分发请求到tomcat中的对应的项目 环境说明: 操作系统:window xp Jav ...
- Android列表控件ListView详解
ListView绝对可以称得上是Android中最常用的控件之一,几乎所有应用程序都会用到它. 由于手机屏幕空间都比较有限,能够一次性在屏幕上显示的内容并不多,当我们的程序中有大量的数据需要展示的时候 ...
- 关于mysql安全
修改root用户密码: update mysql.user set password=password('new_passwd') where user='root'; flush privilege ...
- 一个iOS 框架介绍:MKNetworkKit
http://blog.csdn.net/kmyhy/article/details/12276287 http://blog.csdn.net/mobailwang/article/details/ ...