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
1000000000000000 10000000000000000 Sample input 2
2 12 Sample input 3
9007199254740992 9007199254740992
Sample Output
Sample output 1
239502115812196372 Sample output 2
21 Sample output 3
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. 统计无符号整数二进制中1的个数(Hamming weight)

    1.问题来源 之所以来记录这个问题的解法,是因为在在线编程中经常遇到,比如编程之美和京东的校招笔试以及很多其他公司都累此不疲的出这个考题.看似简单的问题,背后却隐藏着很多精妙的解法.查找网上资料,才知 ...

  2. leetcode 338. Counting Bits,剑指offer二进制中1的个数

    leetcode是求当前所有数的二进制中1的个数,剑指offer上是求某一个数二进制中1的个数 https://www.cnblogs.com/grandyang/p/5294255.html 第三种 ...

  3. 剑指Offer:二进制中1的个数

    题目:输入一个整数,输出该数二进制表示中1的个数. // 二进制中1的个数 #include <stdio.h> int wrong_count_1_bits(int n) // 错误解法 ...

  4. 剑指Offer面试题:9.二进制中1的个数

    一.题目:二进制中1的个数 题目:请实现一个函数,输入一个整数,输出该数二进制表示中1的个数.例如把9表示成二进制是1001,有2位是1.因此如果输入9,该函数输出2. 二.可能引起死循环的解法 一个 ...

  5. 1513:二进制中1的个数 @jobdu

    题目1513:二进制中1的个数 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:1341 解决:455 题目描述: 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 输入: ...

  6. 基于visual Studio2013解决面试题之0410计算二进制中1的个数

     题目

  7. Algorithm --> 二进制中1的个数

    行文脉络 解法一——除法 解法二——移位 解法三——高效移位 解法四——查表 扩展问题——异或后转化为该问题 对于一个字节(8bit)的变量,求其二进制“1”的个数.例如6(二进制0000 0110) ...

  8. [PHP]算法-二进制中1的个数的PHP实现

    二进制中1的个数: 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 思路: 1.右移位运算>> 和 与运算& 2.先移位个然后再与1 &运算为1的就是1 ...

  9. 《剑指offer》 二进制中1的个数

    本题来自<剑指offer> 二进制中1的个数 题目: 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 思路: 两种思路: 第一种:对n进行左移,检测最后一位是否为1,但考 ...

  10. 剑指offer编程题Java实现——面试题10二进制中1的个数

    题目: 请实现一个函数,输入一个整数,输出该整数二进制表示中1的个数.例如,把9表示成二进制是1001,有2位是1,该函数输出2解法:把整数减一和原来的数做与运算,会把该整数二进制表示中的最低位的1变 ...

随机推荐

  1. iis access denied, you do not have permission.

    this kind of problems are usually caused by some IIS configuration issues, like application pool set ...

  2. javascript浏览器对象

    window对象 1.window对象 window对象是BOM的核心,window对象指当前的浏览器窗口 所有JS全局对象.函数以及变量均自动成为window对象的成员 全局变量是window对象的 ...

  3. (七)DAC0832 数模转换芯片的应用 以及运算放大器的学习 01

    DAC0832是8分辨率的D/A转换集成芯片.与微处理器完全兼容.这个DA芯片以其价格低廉.接口简单.转换控制容易等优点,在单片机应用系统中得到广泛的应用.D/A转换器由8位输入锁存器.8位DAC寄存 ...

  4. git操作??

    一直在搞git,但是难度真的很大,我的英语超烂,而申请git账号时全部是英文的,我就拿着翻译有道词典,必应.进行翻译,一个一个单词的往上面打,一张网页能翻译一下午,最后还是不知道应该具体怎么去操作,所 ...

  5. 常用的DC插头公头的尺寸

    2.0*0.6mm:这种应该是用在诺基亚黑白屏那种手机上的充电插头 2.5*0.7mm:这种不知用在哪里 3.5*1.35mm:应该是以前那种小型的磁带机放音机上用的 4.0*1.7mm:已知 ora ...

  6. 关于 ActiveMQ

    今天玩了下 ActiveMQ,希望实现服务器的消息可以通知到各个客户终端. 安装: 1.安装 ActiveMQ 之前必须安装 Java 的 jdk , 可以从此下载:   http://www.ora ...

  7. 【python cookbook】【数据结构与算法】14.对不原生支持比较操作的对象排序

    问题:想在同一个类的实例之间做排序,但是它们并不原生支持比较操作. 解决方案:使用内建的sorted()函数可接受一个用来传递可调用对象的参数key,sorted利用该可调用对象返回的待排序对象中的某 ...

  8. 在Linux中打印函数调用堆栈【原创】

    本人学习笔记,代码参考如下网址 参考http://www.cnblogs.com/dma1982/archive/2012/02/08/2342215.html zhangbh@prolin-srv: ...

  9. Java中的TreeMap、Comparable、Comparator

    我们知道HashMap的存储位置是按照key这个对象的hashCode来存放的,而TreeMap则是不是按照hashCode来存放,他是按照实现的Comparable接口的compareTo这个方法来 ...

  10. html5 canvas 笔记二(添加样式和颜色)

    色彩 Colors fillStyle = color 设置图形的填充颜色. strokeStyle = color 设置图形轮廓的颜色. 透明度 Transparency globalAlpha = ...