B. New Year and Old Property
2016-01-05 18:20
351 查看
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 = 10012and 1010 = 10102.
Two of them (1012 and 1102)
have the described property.
解题说明:此题是一道进制转换题,求出一段区间中转换为二进制时只含有1个0的数字个数。可以先找到题目输入数据范围内所有这样的数字,然后判断是否在区间范围内。
#include<cstdio>
#include <cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
long long a[2000];
int main()
{
long long c,b;
int i,j,count;
for ( i = 1;i<=60;i++)
{
for (j = 1;j<i;j++)
{
a[(i-2)*(i-1)/2+j] = (long long)(pow(2,i)-pow(2,j-1)-1);
}
}
scanf("%I64d %I64d",&c,&b);
count=0;
for ( i = 1;i<2000;i++)
{
if (a[i]>=c&&a[i]<=b)
{
count++;
}
}
printf("%d\n",count);
return 0;
}
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 = 10012and 1010 = 10102.
Two of them (1012 and 1102)
have the described property.
解题说明:此题是一道进制转换题,求出一段区间中转换为二进制时只含有1个0的数字个数。可以先找到题目输入数据范围内所有这样的数字,然后判断是否在区间范围内。
#include<cstdio>
#include <cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
long long a[2000];
int main()
{
long long c,b;
int i,j,count;
for ( i = 1;i<=60;i++)
{
for (j = 1;j<i;j++)
{
a[(i-2)*(i-1)/2+j] = (long long)(pow(2,i)-pow(2,j-1)-1);
}
}
scanf("%I64d %I64d",&c,&b);
count=0;
for ( i = 1;i<2000;i++)
{
if (a[i]>=c&&a[i]<=b)
{
count++;
}
}
printf("%d\n",count);
return 0;
}
相关文章推荐
- Apache Commons Lang包比较常用的项目
- Nginx的安装与配置(一)
- Linux 下搭建ftp服务器 指定用户指定目录及其他操作
- Linux学习之用户管理
- CentOS 7 1511升级python到2.7.11后goagent引发的血案
- linux下用户权限修改
- Linux内核中的init_task进程和idle进程
- Linux命令总结_文件的输入与 输出
- Hadoop经典案例Spark实现(一)——通过采集的气象数据分析每年的最高温度
- LINUX6.5 同一网卡多个配置文件时的使用顺序及格式要求
- Hadoop运行任务时一直卡在: INFO mapreduce.Job: Running job
- linux命令记录
- 10分钟教会你Apache Shiro
- iOS应用架构谈(一):架构设计的方法论
- linux grep基本用法
- openstack api users list get token get servers
- linux系统安装完成后需要安装的软件
- RedHat6(CentOS6)下Zabbix2安装
- OpenTSDB PreSplit Table
- Linux命令大总结