codeforces611B. New Year and Old Property
2016-05-21 08:15
375 查看
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
output
input
output
input
output
input
output
Note
In the first sample Limak's interval contains numbers 510 = 1012, 610 = 1102, 710 = 1112, 810 = 10002, 910 = 10012 and1010 = 10102.
Two of them (1012 and 1102)
have the described property.
题意:给出两个数求在这个区间中的数用二进制表示下只有1个0的数个个数
解题思路:计算出两个的二进制位数然后对位数在其之间的直接计算边缘的dfs即可
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<list>
#include<vector>
using namespace std;
int Count(long long n){
int cnt=0;
while(n){
n>>=1;
cnt++;
}
return cnt;
}
long long a,b,ans;
void dfs(long long num,bool sign,int cnt,int std){
if(cnt==std){
if(num>=a&&num<=b&&sign)ans++;
return ;
}
if(!sign)dfs(num*2,true,cnt+1,std);
dfs(num*2+1,sign,cnt+1,std);
}
int main()
{
scanf("%lld%lld",&a,&b);
int cnta=Count(a),cntb=Count(b);
if(cnta==cntb){
ans=0;
dfs(1,false,1,cnta);
printf("%lld\n",ans);
}
else {
ans=0;
dfs(1,false,1,cnta);
dfs(1,false,1,cntb);
for(int i=cnta+1;i<=cntb-1;++i){
ans+=i-1;
}
printf("%lld\n",ans);
}
return 0;
}
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 and1010 = 10102.
Two of them (1012 and 1102)
have the described property.
题意:给出两个数求在这个区间中的数用二进制表示下只有1个0的数个个数
解题思路:计算出两个的二进制位数然后对位数在其之间的直接计算边缘的dfs即可
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<list>
#include<vector>
using namespace std;
int Count(long long n){
int cnt=0;
while(n){
n>>=1;
cnt++;
}
return cnt;
}
long long a,b,ans;
void dfs(long long num,bool sign,int cnt,int std){
if(cnt==std){
if(num>=a&&num<=b&&sign)ans++;
return ;
}
if(!sign)dfs(num*2,true,cnt+1,std);
dfs(num*2+1,sign,cnt+1,std);
}
int main()
{
scanf("%lld%lld",&a,&b);
int cnta=Count(a),cntb=Count(b);
if(cnta==cntb){
ans=0;
dfs(1,false,1,cnta);
printf("%lld\n",ans);
}
else {
ans=0;
dfs(1,false,1,cnta);
dfs(1,false,1,cntb);
for(int i=cnta+1;i<=cntb-1;++i){
ans+=i-1;
}
printf("%lld\n",ans);
}
return 0;
}
相关文章推荐
- some new eigrp feature
- .net(c#)中的new关键字详细介绍
- Rails Routes中new、collection、member的区别浅析
- C++表达式new与delete知识详解
- C++中new与delete、malloc与free应用分析
- 基于C# 中可以new一个接口?的问题分析
- 全面解析C++中的new,operator new与placement new
- 邪恶的eval和new Function使用介绍
- javascript new一个对象的实质
- 详解new function(){}和function(){}() 区别分析
- 对new functionName()定义一个函数的理解
- Eval and new funciton not the same thing
- 详解Python中的__init__和__new__
- python中的__init__ 、__new__、__call__小结
- new和malloc的区别深入解析
- C语言new操作的安全性分析
- C++中new的越界访问问题
- C# new和override的区别分析
- Javascript new关键字的玄机 以及其它
- C#中Override关键字和New关键字的用法详解