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
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

 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<cstring>
 4 #include<cstdlib>
 5 using namespace std;
 6 typedef long long LL;
 7
 8 LL ans[65];
 9 LL fac[65];
10 LL a[66],alen;
11 void init()
12 {
13     fac[1]=ans[1]=1;
14     fac[2]=ans[2]=2;
15     for(int i=3;i<=60;i++)
16     {
17         fac[i] = 2*fac[i-1]+ans[i-1]-1;
18         ans[i]=ans[i-1]*2;
19     }
20 }
21 void get(LL n)
22 {
23     alen = 0;
24     while(n)
25     {
26         a[++alen]=(n&1);
27         n=n>>1;
28     }
29 }
30 LL solve()
31 {
32     LL sum =0;
33     LL k = 0;
34     for(int i=alen;i>=1;i--)
35     {
36         if(a[i]==1)
37         {
38             sum = sum+fac[i]+k*ans[i];
39             k++;
40         }
41     }
42     return sum;
43 }
44 int main()
45 {
46     LL n,m;
47     init();
48     while(scanf("%I64d%I64d",&n,&m)>0)
49     {
50         get(m);
51         LL sum = solve();
52         get(n-1);
53         sum = sum-solve();
54         printf("%I64d\n",sum);
55     }
56     return 0;
57 }
时间: 2025-01-12 18:43:42

hnu Counting ones 统计1-n 二进制中1的个数的相关文章

统计一个整数二进制中1的个数

输入一个非负整数num,对于每一个i,0<=i<=num,计算i的二进制表示中,有几个'1',例如输入num=5,输出0,1,1,2,1,2. #include <iostream> using namespace std; // 解法1 int countOne(int num) {     int count = 0;     while ( num )     {         // 当最后一位为1时,则加1         if( num & 1 ){      

【C语言】统计一个数二进制中1的个数

//统计一个数二进制中1的个数 #include <stdio.h> int count_one(int num) { int count = 0; while (num) { count++; num = num&(num - 1); //每次消去最后面的一个1,直至没有 } return count; } int main() { printf("%d\n", count_one(12)); printf("%d\n", count_one(

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

leetcode是求当前所有数的二进制中1的个数,剑指offer上是求某一个数二进制中1的个数 https://www.cnblogs.com/grandyang/p/5294255.html 第三种方法,利用奇偶性找规律 class Solution { public: vector<int> countBits(int num) { vector<int> result{0}; for(int i = 1;i <= num;i++){ if(i % 2 == 0) res

第2章 数字之魅——求二进制中1的个数

求二进制中1的个数 问题描述 对于一个字节(8bit)的变量,求其二进制表示中“1”的个数,要求算法的执行效率尽可能地高. [解法一] 可以举一个八位的二进制例子来进行分析.对于二进制操作,我们知道,除以一个2,原来的数字将会减少一个0.如果除的过程中有余,那么就表示当前位置有一个1. 以10 100 010为例: 第一次除以2时,商为1 010 001,余为0. 第二次除以2时,商为101 000,余为1. 因此,可以考虑利用整型数据除法的特点,通过相除和判断余数的值来进行分析.于是有了如下的

ACM:每行输入一个正整数n,找出与它对应的比它大的最小的且它们对应的二进制中1的个数一样多的正整数.

#include<stdio.h> //每行输入一个正整数i,找出与他对应的比它大的最小的正整数且他们的二进制中1的个数一样多. /* 样例输入: 样例输出: 1 2 2 4 3 5 4 8 78 83 0 */ //78的二进制位1001110,有4个1:83比78大且83的二进制位1001011也是4个1. int main() { int count1,count2;//count1统计原数据对应的二进制中1的个数,count2... int a[100];//存输入的数字 int i=

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

题目:输入一个整数,输出该数二进制表示中1的个数. // 二进制中1的个数 #include <stdio.h> int wrong_count_1_bits(int n) // 错误解法: 当n为负数时, n>>=1右移, 最高位补1, 陷入死循环 { int count = 0; while(n) { if( n & 1 ) ++count; n >>= 1; } return count; } int count_1_bits(int n) // 常规解法

[剑指Offer]12.二进制中1的个数

题目 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 思路 把一个整数减去1,再和原整数做与运算,会把整数最右边一个1变成0.那么一个整数的二进制表示中有多少个1,就可以进行多次这样的操作. 代码 /*--------------------------------------- * 日期:2015-07-20 * 作者:SJF0115 * 题目: 12.二进制中1的个数 * 结果:AC * 网址:http://www.nowcoder.com/books/coding-int

二进制中1的个数

题目描述: 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 输入: 输入可能包含多个测试样例. 对于每个输入文件,第一行输入一个整数T,代表测试样例的数量.对于每个测试样例输入为一个整数. .n保证是int范围内的一个整数. 输出: 对应每个测试案例, 输出一个整数,代表输入的那个数中1的个数. 样例输入: 3 4 5 -1 样例输出: 1 2 32 /* 二进制中1的个数 by Rowandjj 2014/7/24 */ #include<stdio.h> #include

【剑指offer】二进制中1的个数

题目描述: 请实现一个函数,输入一个整数,输出该数二进制表示中1的个数.例如把9表示成二进制是1001,有2位是1.因此如果输入9,该函数输出2. 分析描述: 1.对一个整数的二进制形式,要想知道其中1的个数,首先想到的应该就是遍历整个二进制数,用到的方法当然就是移动了(包含左移或右移).例如,用1来跟给定的整数做与运算.如果结果为1,则证明整数的二进制形式中,最右边的一位是1,如果结果是0,则证明整数的二进制形式中,最右边的一位是0. int NumberOf1(int n) { int co

nyoj 数的二进制中1的个数

很有用O(n)内实现三类数字分离,以前大多是分成两类数据,快排中分成两类,还有就是"ab***vvvc" 在O(n)中变成 abvvc****,变成两类划分问题 #include<iostream> #include<string.h> using namespace std; const int N=1000; char c[N]; int len; void swap(char &a,char &b) { //a=a^b; //b=a^b;