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) ...
随机推荐
- C#操作Word的超详细总结
本文中用C#来操作Word,包括: 创建Word: 插入文字,选择文字,编辑文字的字号.粗细.颜色.下划线等: 设置段落的首行缩进.行距: 设置页面页边距和纸张大小: 设置页眉.页码: 插入图片,设置 ...
- 【Android面试】Android面试题集锦 (陆续更新)(最新2012-6-18) eoe上看到的
===============eoeAndroid社区推荐:======================= 1.Android开发新浪面试题[开发者必看哦]下载地址 http://www.eoeand ...
- [Asp.net MVC]Asp.net MVC5系列——添加模型
目录 概述 添加模型 总结 系列文章 [Asp.net MVC]Asp.net MVC5系列——第一个项目 [Asp.net MVC]Asp.net MVC5系列——添加视图 概述 在本节中我们将追加 ...
- 跟着百度学PHP[4]OOP面对对象编程-6-封装性private
所谓封装顾名思义,如同箱子般给封装起来.结合前面的来说就是对属性或者方法,封装后的方法或属性只能有类内部进行调用.外部调用不了. 封装性的好处: 1.信息隐藏 2.http://www.cnblogs ...
- hiho #1223 不等式
#1223 : 不等式 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定n个关于X的不等式,问最多有多少个成立. 每个不等式为如下的形式之一: X < C X ...
- Apache 无法启动
本人是做前端开发的,对后台程序不太熟悉,也就以前学过一点.net.但现在都忘记的差不多了.最近在公司,经理给了我一个管理工具dedecms,我刚开始看的时候完全不懂这是什么东西,之前都没听说过(本人见 ...
- linux shell脚本常用语句
linux shell 指令 诸如-d, -f, -e之类的判断表达式: 文件比较运算符-e filename 如果 filename存在,则为真 [ -e /var/log/syslog ]-d ...
- PHP开发神器——phpstorm
常用快捷键 快捷键 说明 ctrl+j 插入活动代码提示 ctrl+alt+t 当前位置插入环绕代码 alt+insert 生成代码菜单 Shift + Enter 新一行 ctrl+q 查看代码注释 ...
- Bootstrap之表格checkbox复选框全选
效果图: HTML中无需添加额外的一列来表示复选框,而是由JS完成,所以正常的表格布局就行了: <table class="table table-bordered table-hov ...
- [转]Java连接各种数据库的方法
//MySQL: String Driver="com.mysql.jdbc.Driver"; //驱动程序 String URL="jdbc:m ...