Description

One day, Implus gets interested in binary addition and binary carry. He will transfer all decimal digits to binary digits to make the addition. Not as clever as Gauss, to make the addition from a to b, he will add them one by one from a to b in order. For example, from 1 to 3 (decimal digit), he will firstly calculate 01 (1)+10 (2), get 11,then calculate 11+11 (3),lastly 110 (binary digit), we can find that in the total process, only 2 binary carries happen. He wants to find out that quickly. Given a and b in decimal, we transfer into binary digits and use Implus's addition algorithm, how many carries are there?
 

Input

Two integers a, b(0<=a<=b<1000000000), about 100000 cases, end with EOF.
 

Output

One answer per line.

题目大意:给两个数a、b,转成二进制,从a + (a+1) + (a+2) + ... + b,一共需要进位多少次。

思路:拆成二进制来看的话,考虑第一位,假设从1~b有x1个数的第一位是1,从1~a-1有x2个数的第一位是1,那么从a~b就有(x1-x2)个数第一位是1,那么在第一位需要进位的次数就为(x1-x2)/2。假设从a~b有x3个数第二位是1,那么在第二位需要进位的次数就为((x1-x2)/2 + x3)/2。以此类推。

至于算1~x有多少个第p位是1的方法,我做的比较奇葩……比如第二位,从1~x都是重复001100110011……那么就会有x/4个0011,总共就有x/4*4/2个1……之后看x%4之后还有多少个数,如果大于4的一半,就加上x%4-4(比如00110011001,要把最后的1加回去)。

PS:前面的除号都是整除。所以那个除了又乘是不能约掉的。

代码(1140MS):

 #include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
typedef long long LL; int a, b;
LL ans, carry, now; inline LL get(LL x, int p) {
++x;
LL v = 1LL << p, ret = ;
ret += x / v * v / ;
x %= v;
if(x > v / ) ret += x - v / ;
return ret;
} int main() {
//while(cin>>a>>b) cout<<get(a, b)<<endl;
while(scanf("%d%d", &a, &b) != EOF) {
carry = ans = ;
now = ;
while(1LL<<now <= b || carry > ) {
carry += get(b, now) - get(a - , now);
ans += carry >> ;
carry >>= ;
++now;
}
cout<<ans<<endl;
}
}

HDU 4588 Count The Carries(数学统计)的更多相关文章

  1. HDU 4588 Count The Carries 数学

    Count The CarriesTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/ ...

  2. HDU 4588 Count The Carries 计算二进制进位总数

    点击打开链接 Count The Carries Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java ...

  3. HDU 4588 Count The Carries (数学,计数)

    题意:给定两个十进制数,求二进制中,从x加到y的二进制进了多少位. 析:把这些数字的二进制纵向罗列出来,然后一位一位的把和加起来,最终得到总的进位数.从1到x,第i位上1的总数是x左移i+1位再右移i ...

  4. HDU 4588 Count The Carries(找规律,模拟)

    题目 大意: 求二进制的a加到b的进位数. 思路: 列出前几个2进制,找规律模拟. #include <stdio.h> #include <iostream> #includ ...

  5. hdu 4588 Count The Carries

    思路:容易发现二进制表示的数的最低位规律是01010101……:接着是001100110011……:接着是:0000111100001111…… 这样我们发现每一位的循环节是2^(i+1),前2^i是 ...

  6. HDU 4588 Count The Carries 数位DP || 打表找规律

    2013年南京邀请赛的铜牌题...做的非常是伤心.另外有两个不太好想到的地方.. ..a 能够等于零,另外a到b的累加和比較大.大约在2^70左右. 首先说一下解题思路. 首先统计出每一位的1的个数, ...

  7. pandas学习(常用数学统计方法总结、读取或保存数据、缺省值和异常值处理)

    pandas学习(常用数学统计方法总结.读取或保存数据.缺省值和异常值处理) 目录 常用数学统计方法总结 读取或保存数据 缺省值和异常值处理 常用数学统计方法总结 count 计算非NA值的数量 de ...

  8. pandas(5):数学统计——描述性统计

    Pandas 可以对 Series 与 DataFrame 进行快速的描述性统计,方便快速了解数据的集中趋势和分布差异.源Excel文件descriptive_statistics.xlsx: 一.描 ...

  9. HDU 4472 Count(数学 递归)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4472 Problem Description Prof. Tigris is the head of ...

随机推荐

  1. 数据流管理:redux

    redux和react是两个独立的库,所以redux并不是非用不可,是在Flux框架的基础上改进的一个框架,所以一鸣惊人 redux的三大基本原则 唯一的数据源(single source of tr ...

  2. 自动化维护任务 – Automated Maintenance Task (转)

    1. Oracle有三个已定义好的automated maintenance tasks. Automatic Optimizer Statistics Collection—用于收集各种数据库对象的 ...

  3. NPOI操作excel(通过获取批注信息给excel动态赋值)

    private string fileName = null; //文件名 private IWorkbook workbook = null; private FileStream fs = nul ...

  4. Python函数中参数类型

    在学习Python函数的时候,函数本身的定义和调用并不是很复杂,但是函数的参数类型和用法的确有些复杂.在此做一个小结,加深理解. Python参数的定义 负责给函数提供一些必要的数据或信息,以保证函数 ...

  5. rest_framework--RESTful规范

    #####RESTful规范##### 一.什么是restful restful其实就是一种软件架构风格,跟技术毫无关系.是一种面向资源编程的方法. 说起面向资源编程,我想起了之前了解到的面向过程编程 ...

  6. HTML5新标签的兼容性处理

    普通浏览器 普通不支持HTML5新标签的浏览器 -- 能正常解析,但会当初成 inline 元素对待 在不支持HTML5新标签的浏览器里,会将这些新的标签解析成行内元素(inline)对待,所以我们只 ...

  7. 表单转换为JSON

    $.fn.serializeObject = function () { var o = {}; var a = this.serializeArray(); $.each(a, function ( ...

  8. 用HANA STADIO 开发ABAP程序

    Help-->Install New Software-->ADD NAME: hana_on_mars Location: https://tools.hana.ondemand.com ...

  9. CSS3--j惊艳到你的新前端

    一.css3的选择器 1. 父子选择器 直接关系 .box>.com 2. 兄弟选择器 相邻关系 .box+.com <span>hello</span> <p&g ...

  10. Angular : IOC的方式:依赖注入

    依赖注入 @Component, @Injectable 可以允许别的声明在providers里面的Service等注入到被这两个装饰器装饰的类中 Service等可以被声明在app-module.t ...