Good Bye 2015-New Year and Old Property(二进制暴力枚举)
2015-12-31 09:31
381 查看
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.
Sample test(s)
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 and
1010 = 10102. Two of them (1012 and
1102) have the described property.
思路:
这题其实就是暴力题,因为最多才18位,肯定是不会超时的。
AC代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<cstdio>
#include<cmath>
using namespace std;
#define CRL(a) memset(a,0,sizeof(a))
typedef unsigned __int64 ll;
#define T 2000005
#define mod 1000000007
ll m[100],val[T];
int cnt = 0;
void play_table()
{
ll c = 0;
while(c<63)
{
m[c] = ((1LL)<<c);
/*printf("%lld\n",m[c]);*/
c++;
}
/*printf("+---------------+\n");*/
for(int i=2;i<63;++i){
ll tmp = m[i]-1;
for(ll j=0;j<(i-1);++j){
val[cnt++] = tmp^((1LL)<<j);
/* printf("%lld\n",tmp^((1LL)<<j));*/
}
}
/*printf("%d\n",cnt);*/
}
int main()
{
#ifdef zsc
freopen("input.txt","r",stdin);
#endif
play_table();
ll u,v,i;
while(~scanf("%I64d%I64d",&u,&v))
{
ll k = 0;
for(i = 0;i<T;++i){
if(val[i]>=u&&val[i]<=v)k++;
}
printf("%I64d\n",k);
}
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.
Sample test(s)
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.
思路:
这题其实就是暴力题,因为最多才18位,肯定是不会超时的。
AC代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<cstdio>
#include<cmath>
using namespace std;
#define CRL(a) memset(a,0,sizeof(a))
typedef unsigned __int64 ll;
#define T 2000005
#define mod 1000000007
ll m[100],val[T];
int cnt = 0;
void play_table()
{
ll c = 0;
while(c<63)
{
m[c] = ((1LL)<<c);
/*printf("%lld\n",m[c]);*/
c++;
}
/*printf("+---------------+\n");*/
for(int i=2;i<63;++i){
ll tmp = m[i]-1;
for(ll j=0;j<(i-1);++j){
val[cnt++] = tmp^((1LL)<<j);
/* printf("%lld\n",tmp^((1LL)<<j));*/
}
}
/*printf("%d\n",cnt);*/
}
int main()
{
#ifdef zsc
freopen("input.txt","r",stdin);
#endif
play_table();
ll u,v,i;
while(~scanf("%I64d%I64d",&u,&v))
{
ll k = 0;
for(i = 0;i<T;++i){
if(val[i]>=u&&val[i]<=v)k++;
}
printf("%I64d\n",k);
}
return 0;
}
相关文章推荐
- Transformation 能将 Windows XP/Server 2003 操作系统,完美地模拟成 Windows Vista 的软件
- 用javascript和css模拟select的脚本
- mysql binlog二进制日志详解
- 详解C++编程中对二进制文件的读写操作
- 整理C# 二进制,十进制,十六进制 互转
- PHP模拟asp.net的StringBuilder类实现方法
- C#使用ImitateLogin模拟登录百度
- javascript用层模拟可移动的小窗口
- c#二进制逆序方法详解
- JS幻想 读取二进制文件第1/2页
- 使用jscript实现二进制读写脚本代码
- 自编jQuery插件实现模拟alert和confirm
- JS模拟按钮点击功能的方法
- C#二进制序列化实例分析
- JavaScript前端开发之实现二进制读写操作
- PHP模拟asp中response类实现方法
- 模拟Spring的简单实现
- PHP函数篇详解十进制、二进制、八进制和十六进制转换函数说明
- javascript 二进制运算技巧解析
- 如何判断一个整数的二进制中有多少个1