hnu Counting ones 统计1-n 二进制中1的个数
Counting ones |
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB |
Total submit users: 18, Accepted users: 16 |
Problem 13030 : No special judgement |
Problem description |
Carl is right now the happiest child in the world: he has just learned this morning what the bi- nary system is. He learned, for instance, that the binary representation of a positive integer k is a string anan-1 ... a1a0 where each ai is a binary digit 0 or 1, starting with an = 1, and such that k = Σai × 2i(i from 0 to n). It is really nice to see him turning decimal numbers into binary numbers, and then adding and even multiplying them. Caesar is Carl's older brother, and he just can't stand to see his little brother so happy. So he has prepared a challenge: "Look Carl, I have an easy question for you: I will give you two integers A and B, and you have to tell me how many 1's there are in the binary representation of all the integers from A to B, inclusive. Get ready". Carl agreed to the challenge. After a few minutes, he came back with a list of the binary representation of all the integers from 1 to 100. "Caesar, I'm ready". Caesar smiled and said: "Well, let me see, I choose A = 1015 and B = 1016. Your list will not be useful". Carl hates loosing to his brother so he needs a better solution fast. Can you help him? |
Input |
A single line that contains two integers A and B (1 ≤ A ≤ B ≤ 1016). |
Output |
Output a line with an integer representing the total number of digits 1 in the binary representation of all the integers from A to B, inclusive. |
Sample Input |
Sample input 1 |
Sample Output |
Sample output 1 |
Problem Source |
ICPC Latin American Regional 2013 |
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
using namespace std;
typedef long long LL; LL ans[];
LL fac[];
LL a[],alen;
void init()
{
fac[]=ans[]=;
fac[]=ans[]=;
for(int i=;i<=;i++)
{
fac[i] = *fac[i-]+ans[i-]-;
ans[i]=ans[i-]*;
}
}
void get(LL n)
{
alen = ;
while(n)
{
a[++alen]=(n&);
n=n>>;
}
}
LL solve()
{
LL sum =;
LL k = ;
for(int i=alen;i>=;i--)
{
if(a[i]==)
{
sum = sum+fac[i]+k*ans[i];
k++;
}
}
return sum;
}
int main()
{
LL n,m;
init();
while(scanf("%I64d%I64d",&n,&m)>)
{
get(m);
LL sum = solve();
get(n-);
sum = sum-solve();
printf("%I64d\n",sum);
}
return ;
}
hnu Counting ones 统计1-n 二进制中1的个数的更多相关文章
- 统计无符号整数二进制中1的个数(Hamming weight)
1.问题来源 之所以来记录这个问题的解法,是因为在在线编程中经常遇到,比如编程之美和京东的校招笔试以及很多其他公司都累此不疲的出这个考题.看似简单的问题,背后却隐藏着很多精妙的解法.查找网上资料,才知 ...
- leetcode 338. Counting Bits,剑指offer二进制中1的个数
leetcode是求当前所有数的二进制中1的个数,剑指offer上是求某一个数二进制中1的个数 https://www.cnblogs.com/grandyang/p/5294255.html 第三种 ...
- 剑指Offer:二进制中1的个数
题目:输入一个整数,输出该数二进制表示中1的个数. // 二进制中1的个数 #include <stdio.h> int wrong_count_1_bits(int n) // 错误解法 ...
- 剑指Offer面试题:9.二进制中1的个数
一.题目:二进制中1的个数 题目:请实现一个函数,输入一个整数,输出该数二进制表示中1的个数.例如把9表示成二进制是1001,有2位是1.因此如果输入9,该函数输出2. 二.可能引起死循环的解法 一个 ...
- 1513:二进制中1的个数 @jobdu
题目1513:二进制中1的个数 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:1341 解决:455 题目描述: 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 输入: ...
- 基于visual Studio2013解决面试题之0410计算二进制中1的个数
题目
- Algorithm --> 二进制中1的个数
行文脉络 解法一——除法 解法二——移位 解法三——高效移位 解法四——查表 扩展问题——异或后转化为该问题 对于一个字节(8bit)的变量,求其二进制“1”的个数.例如6(二进制0000 0110) ...
- [PHP]算法-二进制中1的个数的PHP实现
二进制中1的个数: 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 思路: 1.右移位运算>> 和 与运算& 2.先移位个然后再与1 &运算为1的就是1 ...
- 《剑指offer》 二进制中1的个数
本题来自<剑指offer> 二进制中1的个数 题目: 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 思路: 两种思路: 第一种:对n进行左移,检测最后一位是否为1,但考 ...
- 剑指offer编程题Java实现——面试题10二进制中1的个数
题目: 请实现一个函数,输入一个整数,输出该整数二进制表示中1的个数.例如,把9表示成二进制是1001,有2位是1,该函数输出2解法:把整数减一和原来的数做与运算,会把该整数二进制表示中的最低位的1变 ...
随机推荐
- jsp页面指令
JSP中共有三个指令: (1)page: 用于定义JSP文件中的全局属性 (2)include: 用于在JSP页面中包含另外一个文件的内容 (3)taglib: 此指令能够让用户自定义新的标签 第三个 ...
- web server与app server有什么不同
简单来说,web服务器提供页面给浏览器,而app服务器提供客户端可以调用的接口.具体而言,我们可以说: Web服务器处理HTTP请求,而app服务器基于多种不同的协议,处理应用程序的逻辑问题. 以下将 ...
- paper 89:视频图像去模糊常用处理方法
随着“平安城市”的广泛建设,各大城市已经建有大量的视频监控系统,虽然监控系统己经广泛地存在于银行.商场.车站和交通路口等公共场所,但是在公安工作中,由于设备或者其他条件的限制,案情发生后的图像回放都存 ...
- EBS fnd_global.apps_initialize
原型:fnd_global.apps_initialize(user_ID, Responsibility_id ...
- mysql取出现在的时间戳和时间时间戳转成人类看得懂的时间
mysql取出现在的时间戳和时间时间戳转成人类看得懂的时间,我们在mysql里面他封装了一个内置的时间戳转化的函数,比如我们现在的时间戳是:1458536709 ,"%Y-%m-%d&quo ...
- Eclipse字符集设置方式
默认的字符集是GBK 1.windows->Preferences...打开"首选项"对话框,左侧导航树,导航到general->Workspace,右侧Text fi ...
- 记在centos中连接无线网络的一次过程
1. 首先, 你的系统要能驱动无限网卡, 要是人品好的话, 系统已经自带了你的网卡的驱动程序. 不然就要先搞定无线网卡的驱动再说. 不然后面的步骤也就没必要了. 2. 看一下你的无线网卡叫什么: iw ...
- OpenStack 的NAT解决办法
原因 iptables中的nat表会对数据进行封包换目标,源地址,在我们的系统中是不需要的.所以我们做了如下操作 操作 /etc/nova/nova.conf的 1 2 #routing_source ...
- Java 的局部变量和成员变量
在Java语言中没有全局变量 分析各种变量的作用域的最简单方法是以花括号为界, 1.在类体中定义的是成员变量,成员变量会被默认初始化 2.在方法中定义的是局部变量,局部变量不会被默认初始化
- Android上传头像代码,相机,相册,裁剪
activity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout ...