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. Java 加密PDF设置密码并添加水印

    /** * Project Name:XXX * File Name:EncryptLogFile.java * Date:2016-6-12上午11:56:38 * Copyright (c) 20 ...

  2. js获取站点根目录

    function getRootPath(){        var strFullPath=window.document.location.href;        var strPath=win ...

  3. B. Train Seats Reservation 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛

    You are given a list of train stations, say from the station 1 to the station 100. The passengers ca ...

  4. 配置django控制台输出ORM转化的sql

    在setting中配置 LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'handlers': { 'console':{ ' ...

  5. 你的sql查询为什么这么慢?

    做后台开发的程序猿通常需要写各种各样的sql,可很多时候写出来的sql虽然能满足功能性需求,性能上却不尽人意.如果业务复杂,表结构和索引设计又不合理的话,写出来的sql执行时间可能会达到几十甚至上百秒 ...

  6. Percona-Tookit工具包之pt-online-schema-change

      Preface       As we all know,it's really a troublesome thing to DBA in scenario of changing table ...

  7. vue.js 组件-全局组件和局部组件

    这两天学习了Vue.js 感觉组件这个地方知识点挺多的,而且很重要,所以,今天添加一点小笔记. 首先Vue组件的使用有3个步骤,创建组件构造器,注册组件,使用组件3个方面. 代码演示如下: <! ...

  8. springmvc请求数据的流程。

    验证了我说的,从model层中拿来的数据,不管什么类型,都是通过隐含模型,中转,放入request中的.除非你特意把这些数据放到session域中 流程含义解释:(来自网友)(1)HTTP请求到达we ...

  9. 【ISIS(中间系统到中间系统)路由链路状态信息协议初识】

    ISIS单区域的基本配置 一:根据项目需求,考虑到组网的规模和条件,部署ISIS单区域的拓扑图如下: 二:配置 1:首先对RTA进行配置,在系统视图创建ISIS进程:进入ISIS配置视图,指定IS的级 ...

  10. less学习二---变量

    less中声明的变量可以存储css属性值,还可以存储选择器,属性名,url以及@imporant等 变量声明及赋值格式:@variableName : varableValue ; //属性值 //l ...