Count The Carries - HDU 4588 水题

73 篇文章 0 订阅

Count The Carries

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


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
 


题意:从l加到r二进制进位一共发生多少次。

思路:预算出每个二进制位上共有多少个1,假设有n个1,那么这个位上的进位会发生n/2次。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
ll L,R;
int T,t,n,m;
ll num[110],POW[45];
ll solve(ll ret,ll p)
{
    int i,j;
    ll k,ans;
    ret++;
    k=ret/p;

    ans=k*p/2;
    ret=ret-k*p;;
    if(ret>p/2)
      ans+=ret-p/2;
    return ans;
}
int main()
{
    int i,j,k;
    ll ans,ret;
    POW[1]=2;
    for(i=2;i<=40;i++)
       POW[i]=POW[i-1]*2;
    while(~scanf("%I64d%I64d",&L,&R))
    {
        memset(num,0,sizeof(num));
        for(i=1;i<=40;i++)
           num[i]=solve(R,POW[i])-solve(L-1,POW[i]);
        ans=0;
        for(i=1;i<=100;i++)
        {
            ret=num[i]/2;
            num[i+1]+=ret;
            ans+=ret;
        }
        printf("%I64d\n",ans);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值