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变 ...
随机推荐
- Codeforce Round #227 Div2
这回的看错时间了! 发现理论可以涨分的- -
- winform 控件开发1——复合控件
哈哈是不是丑死了? 做了一个不停变色的按钮,可以通过勾选checkbox停下来,代码如下: 复合控件果然简单呀,我都能学会~ using System; using System.Collection ...
- C++类构造析构调用顺序训练(复习专用)
//对象做函数参数 //1 研究拷贝构造 //2 研究构造函数,析构函数的调用顺序 //总结 构造和析构的调用顺序 #include "iostream" using namesp ...
- spark sql cache
1.几种缓存数据的方法 例如有一张hive表叫做activity 1.CACHE TABLE //缓存全表 sqlContext.sql("CACHE TABLE activity" ...
- C#委托实例
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace we ...
- opencv常用数据结构之:IplImage
typedef struct_IplImage{ int nSize; //IplImage大小 int ID; //版本(=0) int nChannels; //大多 ...
- SSAS的维度表之间的关系只能有一个不能有多个
我们在SSAS中创建维度的时候,有时候可能一个维度需要用到多个表的字段作为维度属性,那么这多个表之间势必存在关联关系,但是切记维度表之间的关联关系有且只能有一个不能有多个,下面我们来看一个例子. 现在 ...
- Java应用程序的打包和发布
Java应用程序的打包和发布 简化Java应用程序的打包和发布 发布Java应用程序时Java提供了一系列打包和发布工具,可以显著的简化发布过程 该文章提供了打包Java code的几种方法,探讨Ja ...
- android提示框
// 对话框 AlertDialog.Builder builder = new Builder(MainActivity.this); builder.setMessage("是否确认删除 ...
- 视频处理控件TVideoGrabber中如何混合多个视频源(2)
在前面的<视频处理控件TVideoGrabber混合多个视频源(1)>一文中,已经对TVideoGrabber中混合视频源做了一些内容的说明,本文将继续前文: >>>TV ...