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. csuoj 1335: 高桥和低桥

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1335 1335: 高桥和低桥 Time Limit: 1 Sec  Memory Limit: 1 ...

  2. ruby的在ubuntu上的安装

    apt (Debian or Ubuntu) Debian GNU/Linux and Ubuntu use the apt package manager. You can use it like ...

  3. 通过struts.xml搭建、为属性注入值_2015.01.04

    01:web.xml配置: <?xml version="1.0" encoding="UTF-8"?> <web-app version=& ...

  4. webpack我遇到的一些坑

    我的第一个用于实验webpack的项目是一个拥有多个版本的项目.什么叫多个版本?这个项目对3个语言版本+3个不同城市版本+(移动端  + PC端),也就是3*3*2,18个版本. 我的第一次想法肯定是 ...

  5. 【crunch bang】调整窗口大小

    在终端下,   <super> + 上箭头  == 向上调整大小 <super> + 下箭头(左.右)

  6. 学习K&R时初学者经常遇到的一个问题——EOF

    学习K&R时初学者经常遇到的一个问题——EOF

  7. 【海岛帝国系列赛】No.4 海岛帝国:LYF的太空运输站

    50212228海岛帝国:LYF的太空运输站 [试题描述] 最近,“购物券”WHT在“药师傅”帝国资源大会上提出了“SSTS”太空运输站计划.由于恐怖分子前些日子刚猖狂完,炸毁高楼无数,YSF不得不执 ...

  8. onTouch和onTouchEvent

    public boolean dispatchTouchEvent(MotionEvent event) { if (mOnTouchListener != null && mOnTo ...

  9. Robotium Recorder的初试

    一.安装 资料来自官方 Prerequisites: Install the Java JDK. Install the Android SDK. The ADT bundle with Eclips ...

  10. 常用Git命令

    Git教程:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 一般来说,日常使用只要 ...