求a加到b二进制加法有多少次进位。

//求a加到b二进制加法有多少次进位。
//网址:http://219.230.70.12/Local/1739
#include<cstdlib>
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<cstring>
#include<algorithm>
#include<set>
#include<map>
#include<list>
#include<queue>
#include<vector>
#define ULL unsigned long long
#define LL long long
#define UI unsigned int
#define inf 0x7fffffff
#define eps 1e-7
#define N 1009
#define M 20009
using namespace std;
int T,n,m,k,t;
int a[N],b[N],la,lb,x,y;
void fun(int x,int &top,int a[])
{
    int t=1;
    top=0;
    while((x)>=t)
    {
        t*=2;
        a[top]=((x+1)/t)*(t/2);
        a[top]+=max(0,((x+1)%t)-t/2);
        top++;
    }
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("ex.in","r",stdin);
#endif
    int ncase=0;
//    scanf("%d",&T);
    while (scanf("%d%d",&x,&y)!=EOF)
//    while (T--)
    {
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        fun(x-1,la,a);
        fun(y,lb,b);
        int num=0;
        LL ans=0;
        for(int i=0; i<lb||num; i++)
        {
            int t=0;
            if(i<lb)
                t=b[i]-a[i];
            t+=num;
            ans+=t/2;
            num=t/2;
        }
        cout<<ans<<endl;
    }
    return 0;
}

  

posted @ 2013-05-14 19:57  baoff  阅读(355)  评论(0编辑  收藏  举报