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变 ...
随机推荐
- session 实现保存用户信息
index.jsp <body> <div style="margin: 0 auto; width: 500px; text-align: center;"&g ...
- [原创]java WEB学习笔记64:Struts2学习之路--主题
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- poj: 2159
简单题,看起来很凶 #include <iostream> #include <stdio.h> #include <string> #include <st ...
- 【皇甫】☀PPT里的小玩意
第三次写博客了,感觉写的蛮有趣的,在写的同时,回顾了知识点,又上手操作了一遍,印象更加深刻了,尽管今天写的和那些像JAVA啦,HTML啦,C#啦,没多大关系(个人理解),但确实我们经常能用到的.比如说 ...
- CCF真题之数列分段
201509-1 数列分段 问题描述 给定一个整数数列,数列中连续相同的最长整数序列算成一段,问数列中共有多少段? 输入格式 输入的第一行包含一个整数n,表示数列中整数的个数. 第二行包含n个整数a ...
- struts配置请求后缀,将.action改为.do、.doaction_2015.01.04
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "- ...
- HashMap存储数据赋值javabean简单示例
package com.shb.web; import java.util.HashMap; import java.util.Iterator; import java.util.Map; /** ...
- paper 92:Lena与图像处理
如果你是个数字图像处理技术的研究人员,或这方面的工程师, 想必对这位戴有羽状帽饰的美女不会感觉陌生.我第一次在国际知名的学术性刊物上见到这位美女时,不禁叹为天人,毕竟在这样严肃的学术期刊上,还从未出现 ...
- paper 7:支持向量机系列四:Outliers —— 介绍支持向量机使用松弛变量处理 outliers 方法。
在最开始讨论支持向量机的时候,我们就假定,数据是线性可分的,亦即我们可以找到一个可行的超平面将数据完全分开.后来为了处理非线性数据,使用 Kernel 方法对原来的线性 SVM 进行了推广,使得非线性 ...
- 夺命雷公狗---微信开发51----网页授权(oauth2.0)获取用户基本信息接口(1)
如果用户在微信客户端访问第三方网页,公众号可以通过微信网页授权机制,来获取用户基本信息,从而实现业务逻辑. 一般我们用来“数据采集”,“市场调查”,“投票”,只要授权了第三方网页,微信用户无需注册就可 ...