HDU 4588 Count The Carries(数学)

Count The Carries

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 1581    Accepted Submission(s): 559


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

Sample Input
  
  
1 2 1 3 1 4 1 6
 

Sample Output
  
  
0 2 3 6
题目大意:给出两个十进制数a,b,求区间[a,b]上所有二进制数相加的总进位次数。
思路:由于题目数据量很大,所以不可能采取两两相加的形式来计算,而是应该将所有二进制数摞在一起找规律计算。首先给出0——15的二进制表示形式如下
0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 1 1 0 0 1 0 0 0 0 1 0 1 0 0 1 1 0 0 0 1 1 1 0 1 0 0 0 0 1 0 0 1 0 1 0 1 0 0 1 0 1 1 0 1 1 0 0 0 1 1 0 1 0 1 1 1 0 0 1 1 1 1
通过观察我们发现最右边一位依次为010101......,倒数第二位为00110011......,即从右边数第n位,其对应的二进制数依次为2^n个0,2^n个1,2^n个0,2^n个1......然后依次循环下去。而每一位所进的位数等于(这一位1的个数+前一位所进的1的个数)/2,而每一位的1是有周期性出现的。这样思路就找到了,即统计每一位的进位个数并相加,最后的总和就是答案。
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;


typedef long long LL;


LL solve(LL v, LL i) {


  LL ans = 0;
  ans += (v + 1LL) / (1LL << i) * (1LL << (i - 1LL)); //因为算周期时是从0开始算的,所以相应的v也要加1
  ans += max( (v + 1LL) % (1LL << i) - (1LL << (i - 1LL)), 0LL );
  return ans;


}


int main()
{
    LL a,b;
    while (scanf("%I64d %I64d", &a, &b) == 2) {
        LL sum = 0, id = 0;
        for (int i = 1; i <= 62; i++) {
            LL x = solve( max(0LL, a - 1), i ); //分别统计0——(a-1)与0——b中某一位中1的个数,二者的差就是a——b中这一位的1的个数
            LL y = solve(b, i);
            sum += (y - x + id) / 2; //1的个数+进位数
            id = (y - x + id) / 2; //进位数
            if (!x && !y && !id) break;
        }
        printf("%I64d\n",sum);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值